diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-12-08 20:40:25 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-12-08 20:41:50 +0100 |
commit | 7207f11fca59b23065bf259a32b595e8a9deec0b (patch) | |
tree | c45e123eeb6d1cc7043f304d7133ecb23e98c7c4 | |
parent | a675244a70b1721aff0048ebb671480fb40a7f67 (diff) | |
download | Ishtar-7207f11fca59b23065bf259a32b595e8a9deec0b.tar.bz2 Ishtar-7207f11fca59b23065bf259a32b595e8a9deec0b.zip |
Improve JQtables when dealing with multiple foreign keys
-rw-r--r-- | ishtar_common/models.py | 2 | ||||
-rw-r--r-- | ishtar_common/templatetags/window_tables.py | 10 | ||||
-rw-r--r-- | ishtar_common/views.py | 4 | ||||
-rw-r--r-- | ishtar_common/widgets.py | 15 |
4 files changed, 20 insertions, 11 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 2bb96d11d..69bb9b935 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -416,7 +416,7 @@ class GeneralType(Cached, models.Model): for value in initial: try: pk = int(value) - except ValueError: + except (ValueError, TypeError): continue if pk in type_pks: continue diff --git a/ishtar_common/templatetags/window_tables.py b/ishtar_common/templatetags/window_tables.py index 566d1dfbc..377b6e435 100644 --- a/ishtar_common/templatetags/window_tables.py +++ b/ishtar_common/templatetags/window_tables.py @@ -17,7 +17,7 @@ from archaeological_operations.models import OperationSource, Operation from archaeological_context_records.models import ContextRecord, \ ContextRecordSource, RecordRelations as CRRecordRelations from archaeological_finds.models import Find, FindSource, \ - FindUpstreamTreatments, FindDownstreamTreatments + FindUpstreamTreatments, FindDownstreamTreatments, FindTreatments register = template.Library() @@ -53,16 +53,20 @@ ASSOCIATED_MODELS['finds_upstreamtreatments'] = ( FindUpstreamTreatments, 'get-upstreamtreatment', '') ASSOCIATED_MODELS['finds_downstreamtreatments'] = ( FindDownstreamTreatments, 'get-downstreamtreatment', '') +ASSOCIATED_MODELS['treatments'] = ( + FindTreatments, 'get-treatment', '') @register.simple_tag(takes_context=True) def dynamic_table_document( context, caption, associated_model, key, value, - table_cols='TABLE_COLS', output='html', large=False): + table_cols='TABLE_COLS', output='html', large=False, + col_prefix=''): if not table_cols: table_cols = 'TABLE_COLS' model, url, url_full = ASSOCIATED_MODELS[associated_model] - grid = JQueryJqGrid(None, None, model, table_cols=table_cols) + grid = JQueryJqGrid(None, None, model, table_cols=table_cols, + col_prefix=col_prefix) source = unicode(reverse_lazy(url)) source_full = unicode(reverse_lazy(url_full)) if url_full else '' source_attrs = mark_safe('?submited=1&{}={}'.format(key, value)) diff --git a/ishtar_common/views.py b/ishtar_common/views.py index c2bfd9bda..6426fef8f 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -1009,9 +1009,9 @@ def get_item(model, func_name, default_name, extra_request_keys=[], # foreign key may be divided by "." or "__" for tc in table_col: if '.' in tc: - tab_cols.append(tc.split('.')[-1]) + tab_cols += tc.split('.') elif '__' in tc: - tab_cols.append(tc.split('__')[-1]) + tab_cols += tc.split('__') else: tab_cols.append(tc) k = "__".join(tab_cols) diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index 7696d67da..7611a08df 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -529,7 +529,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. @@ -545,6 +546,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 @@ -560,6 +562,9 @@ 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 = [], [] @@ -577,23 +582,23 @@ 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) |