diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-01-10 00:26:07 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2017-01-10 00:26:07 +0100 |
commit | c0f14049777002bf0849f04dabc99a6bc66de295 (patch) | |
tree | 777c2f1bcfc1cde0056a64b04d0ee322948ade56 /ishtar_common/widgets.py | |
parent | 229a5559e9933ea76020963ca3778906d39279d4 (diff) | |
parent | 1191cb323ca087ea05d5f58acb555b8e2d266801 (diff) | |
download | Ishtar-c0f14049777002bf0849f04dabc99a6bc66de295.tar.bz2 Ishtar-c0f14049777002bf0849f04dabc99a6bc66de295.zip |
Merge branch 'master' into v0.9
Conflicts:
archaeological_files/migrations/0014_auto__add_field_file_requested_operation_type__add_field_file_organiza.py
Diffstat (limited to 'ishtar_common/widgets.py')
-rw-r--r-- | ishtar_common/widgets.py | 52 |
1 files changed, 40 insertions, 12 deletions
diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index 1183836bc..ddbfa91bf 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -19,12 +19,15 @@ # See the file COPYING for details. +import logging + from django import forms from django.conf import settings -from django.core.urlresolvers import reverse +from django.core.urlresolvers import reverse, NoReverseMatch from django.db.models import fields from django.forms import ClearableFileInput -from django.forms.widgets import flatatt +from django.forms.widgets import flatatt, \ + CheckboxSelectMultiple as CheckboxSelectMultipleBase from django.template import Context, loader from django.template.defaultfilters import slugify from django.utils.encoding import smart_unicode @@ -36,6 +39,8 @@ from django.utils.translation import ugettext_lazy as _ from ishtar_common import models +logger = logging.getLogger(__name__) + reverse_lazy = lazy(reverse, unicode) @@ -56,6 +61,20 @@ class Select2Multiple(forms.SelectMultiple): return super(Select2Multiple, self).render(name, value, attrs, choices) +class CheckboxSelectMultiple(CheckboxSelectMultipleBase): + """ + Fix initialization bug. + Should be corrected on recent Django version. + TODO: test and remove (test case: treatment type not keep on modif) + """ + def render(self, name, value, attrs=None, choices=()): + if type(value) in (str, unicode): + value = value.split(',') + if type(value) not in (list, tuple): + value = [value] + return super(CheckboxSelectMultiple, self).render(name, value, attrs, + choices) + class MultipleAutocompleteField(forms.MultipleChoiceField): def __init__(self, *args, **kwargs): @@ -322,7 +341,6 @@ class JQueryTown(forms.TextInput): @classmethod def encode_source(cls, source): - encoded_src = '' if isinstance(source, list): encoded_src = JSONEncoder().encode(source) elif isinstance(source, str) \ @@ -515,7 +533,8 @@ class JQueryJqGrid(forms.RadioSelect): def __init__(self, source, form, associated_model, attrs={}, table_cols='TABLE_COLS', multiple=False, multiple_cols=[2], new=False, new_message="", source_full=None, - multiple_select=False, sortname="__default__"): + multiple_select=False, sortname="__default__", + col_prefix=''): """ JQueryJqGrid widget init. @@ -531,6 +550,7 @@ class JQueryJqGrid(forms.RadioSelect): :param source_full: url to get full listing :param multiple_select: :param sortname: column name (model attribute) to use to sort + :param col_prefix: prefix to remove to col_names """ super(JQueryJqGrid, self).__init__(attrs=attrs) self.source = source @@ -546,13 +566,15 @@ class JQueryJqGrid(forms.RadioSelect): self.new, self.new_message = new, new_message self.source_full = source_full self.sortname = sortname + self.col_prefix = col_prefix + if self.col_prefix and not self.col_prefix.endswith('__'): + self.col_prefix += "__" def get_cols(self, python=False): jq_col_names, extra_cols = [], [] col_labels = {} - if hasattr(self.associated_model, self.table_cols + '_LBL'): - col_labels = getattr(self.associated_model, - self.table_cols + '_LBL') + if hasattr(self.associated_model, 'COL_LABELS'): + col_labels = self.associated_model.COL_LABELS for col_names in getattr(self.associated_model, self.table_cols): field_verbose_names = [] field_verbose_name, field_name = "", "" @@ -563,24 +585,26 @@ class JQueryJqGrid(forms.RadioSelect): keys = col_name.split('__') if '.' in col_name: keys = col_name.split('.') - f_name = '' for key in keys: if hasattr(field, 'rel') and field.rel: field = field.rel.to try: field = field._meta.get_field(key) field_verbose_name = field.verbose_name - f_name = field.name except (fields.FieldDoesNotExist, AttributeError): if hasattr(field, key + '_lbl'): - f_name = key field_verbose_name = getattr(field, key + '_lbl') else: continue if field_name: field_name += "__" - field_name += f_name + if col_name.startswith(self.col_prefix): + field_name += col_name[len(self.col_prefix):] + else: + field_name += col_name field_verbose_names.append(unicode(field_verbose_name)) + if not field_name: + field_name = "__".join(col_names) if field_name in col_labels: jq_col_names.append(unicode(col_labels[field_name])) elif col_names and col_names[0] in col_labels: @@ -618,7 +642,11 @@ class JQueryJqGrid(forms.RadioSelect): col_idx = col_idx and ", ".join(col_idx) or "" dct['encoding'] = settings.ENCODING or 'utf-8' - dct['source'] = unicode(self.source) + try: + dct['source'] = unicode(self.source) + except NoReverseMatch: + logger.warning('Cannot resolve source for {} widget'.format( + self.form)) if unicode(self.source_full) and unicode(self.source_full) != 'None': dct['source_full'] = unicode(self.source_full) |