diff options
| author | Étienne Loks <etienne.loks@proxience.com> | 2015-10-06 00:31:12 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@proxience.com> | 2015-10-06 00:31:12 +0200 | 
| commit | 12b517daa7aca2ca475a654ecca99202ecbe9f23 (patch) | |
| tree | a5bed9314cdb19324f84e9a208f3e36c4322ec21 | |
| parent | 02a8369f8211c7b98993d899f0ba62262e2761de (diff) | |
| download | Ishtar-12b517daa7aca2ca475a654ecca99202ecbe9f23.tar.bz2 Ishtar-12b517daa7aca2ca475a654ecca99202ecbe9f23.zip | |
Specific context record table for operation window
| -rw-r--r-- | archaeological_context_records/models.py | 3 | ||||
| -rw-r--r-- | archaeological_context_records/urls.py | 3 | ||||
| -rw-r--r-- | archaeological_context_records/views.py | 14 | ||||
| -rw-r--r-- | archaeological_operations/templates/ishtar/sheet_operation.html | 2 | ||||
| -rw-r--r-- | ishtar_common/templatetags/window_tables.py | 8 | ||||
| -rw-r--r-- | ishtar_common/views.py | 92 | ||||
| -rw-r--r-- | ishtar_common/widgets.py | 11 | 
7 files changed, 95 insertions, 38 deletions
| diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index d86d4aaa9..8c80d81ed 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -110,6 +110,9 @@ class ContextRecord(BaseHistorizedItem, OwnPerms, ShortMenuItem):                    'label', 'unit']      if settings.COUNTRY == 'fr':          TABLE_COLS.insert(1, 'operation.code_patriarche') +    TABLE_COLS_FOR_OPE = [ +        'label', ['parcel.section', 'parcel.parcel_number'], 'unit', +        'datings.period', 'description']      external_id = models.CharField(_(u"External ID"), blank=True, null=True,                                     max_length=120)      parcel = models.ForeignKey(Parcel, verbose_name=_(u"Parcel"), diff --git a/archaeological_context_records/urls.py b/archaeological_context_records/urls.py index 7e7adb937..098542fa0 100644 --- a/archaeological_context_records/urls.py +++ b/archaeological_context_records/urls.py @@ -55,6 +55,9 @@ urlpatterns += patterns(          'revert_contextrecord', name='revert-contextrecord'),      url(r'get-contextrecord/(?P<type>.+)?$', 'get_contextrecord',          name='get-contextrecord'), +    url(r'get-contextrecord-for-ope/(?P<type>.+)?$', +        'get_contextrecord_for_ope', +        name='get-contextrecord-for-ope'),      url(r'get-contextrecord-full/(?P<type>.+)?$',          'get_contextrecord', name='get-contextrecord-full',          kwargs={'full': True}), diff --git a/archaeological_context_records/views.py b/archaeological_context_records/views.py index 483630b3c..d64adc2a2 100644 --- a/archaeological_context_records/views.py +++ b/archaeological_context_records/views.py @@ -43,6 +43,20 @@ get_contextrecord = get_item(          'parcel_1': 'operation__parcels__parcel_number',          'label': 'label__icontains'      },) +get_contextrecord_for_ope = get_item( +    models.ContextRecord, +    'get_contextrecord', 'contextrecord', +    extra_request_keys={ +        'parcel__town': 'parcel__town__pk', +        'operation__year': 'operation__year__contains', +        'operation__code_patriarche': 'operation__code_patriarche', +        'operation__operation_code': 'operation__operation_code', +        'datings__period': 'datings__period__pk', +        'parcel_0': 'operation__parcels__section', +        'parcel_1': 'operation__parcels__parcel_number', +        'label': 'label__icontains' +    }, +    own_table_cols=models.ContextRecord.TABLE_COLS_FOR_OPE)  get_contextrecordsource = get_item(      models.ContextRecordSource,      'get_contextrecordsource', 'contextrecordsource', diff --git a/archaeological_operations/templates/ishtar/sheet_operation.html b/archaeological_operations/templates/ishtar/sheet_operation.html index daeea7c40..9d22657c3 100644 --- a/archaeological_operations/templates/ishtar/sheet_operation.html +++ b/archaeological_operations/templates/ishtar/sheet_operation.html @@ -96,7 +96,7 @@  {% trans "Context records" as context_records %}  {% if item.context_record.count %} -{% dynamic_table_document context_records 'context_records' 'operation' item.pk %} +{% dynamic_table_document context_records 'context_records_for_ope' 'operation' item.pk 'TABLE_COLS_FOR_OPE' %}  {% endif %}  {% comment %} diff --git a/ishtar_common/templatetags/window_tables.py b/ishtar_common/templatetags/window_tables.py index 36d9dd236..0b744c1e5 100644 --- a/ishtar_common/templatetags/window_tables.py +++ b/ishtar_common/templatetags/window_tables.py @@ -28,6 +28,9 @@ try:          ContextRecordSource      ASSOCIATED_MODELS['context_records'] = (ContextRecord, 'get-contextrecord',                                              'get-contextrecord-full') +    ASSOCIATED_MODELS['context_records_for_ope'] = ( +        ContextRecord, +        'get-contextrecord-for-ope', 'get-contextrecord-full')      ASSOCIATED_MODELS['context_records_docs'] = (ContextRecordSource,                                                   'get-contextrecordsource', '')  except: @@ -41,9 +44,10 @@ except:  @register.inclusion_tag('ishtar/blocks/window_tables/dynamic_documents.html') -def dynamic_table_document(caption, associated_model, key, value): +def dynamic_table_document(caption, associated_model, key, value, +                           table_cols='TABLE_COLS'):      model, url, url_full = ASSOCIATED_MODELS[associated_model] -    grid = JQueryJqGrid(None, None, model) +    grid = JQueryJqGrid(None, None, model, table_cols=table_cols)      source = unicode(reverse_lazy(url))      source_full = unicode(reverse_lazy(url_full)) if url_full else ''      source_attrs = '?{}={}'.format(key, value) diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 81f394bbb..10c62d465 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -303,7 +303,7 @@ PRIVATE_FIELDS = ('id', 'history_modifier', 'order')  def get_item(model, func_name, default_name, extra_request_keys=[],               base_request={}, bool_fields=[], reversed_bool_fields=[],               dated_fields=[], associated_models=[], relative_session_names={}, -             specific_perms=[]): +             specific_perms=[], own_table_cols=None):      """      Generic treatment of tables      """ @@ -441,9 +441,12 @@ def get_item(model, func_name, default_name, extra_request_keys=[],          q = request_items.get('sidx')          # table cols -        table_cols = full and [field.name for field in model._meta.fields -                               if field.name not in PRIVATE_FIELDS] \ -            or model.TABLE_COLS +        if own_table_cols: +            table_cols = own_table_cols +        else: +            table_cols = full and [field.name for field in model._meta.fields +                                   if field.name not in PRIVATE_FIELDS] \ +                or model.TABLE_COLS          # manage sort tables          manual_sort_key = None          order = request_items.get('sord') @@ -461,9 +464,11 @@ def get_item(model, func_name, default_name, extra_request_keys=[],                  orders.append(sign + k)              items = items.order_by(*orders)          elif q: -            for k in table_cols: -                if k.endswith(q): -                    manual_sort_key = k +            for ke in table_cols: +                if type(ke) in (list, tuple): +                    ke = ke[0] +                if ke.endswith(q): +                    manual_sort_key = ke                      break              if not manual_sort_key and model._meta.ordering:                  orders = [sign + k for k in model._meta.ordering] @@ -494,33 +499,53 @@ def get_item(model, func_name, default_name, extra_request_keys=[],              items = [item.get_previous(old) for item in items]          for item in items:              data = [item.pk] -            for k in table_cols: -                vals = [item] -                for ky in k.split('.'): -                    new_vals = [] -                    for val in vals: -                        if hasattr(val, 'all'):  # manage related objects -                            val = list(val.all()) -                            for v in val: -                                new_vals.append(getattr(v, ky)) -                        elif val: -                            new_vals.append(getattr(val, ky)) -                    vals = new_vals -                # manage last related objects -                if vals and hasattr(vals[0], 'all'): -                    new_vals = [] -                    for val in vals: -                        new_vals += list(val.all()) -                    vals = new_vals -                data.append(", ".join([format_val(v) for v in vals]) or u"") +            for keys in table_cols: +                if type(keys) not in (list, tuple): +                    keys = [keys] +                my_vals = [] +                for k in keys: +                    vals = [item] +                    for ky in k.split('.'): +                        new_vals = [] +                        for val in vals: +                            if hasattr(val, 'all'):  # manage related objects +                                val = list(val.all()) +                                for v in val: +                                    new_vals.append(getattr(v, ky)) +                            elif val: +                                new_vals.append(getattr(val, ky)) +                        vals = new_vals +                    # manage last related objects +                    if vals and hasattr(vals[0], 'all'): +                        new_vals = [] +                        for val in vals: +                            new_vals += list(val.all()) +                        vals = new_vals +                    if not my_vals: +                        my_vals = [format_val(v) for v in vals] +                    else: +                        new_vals = [] +                        for idx, v in enumerate(vals): +                            new_vals.append("{}{}{}".format( +                                my_vals[idx], settings.JOINT, format_val(v))) +                        my_vals = new_vals[:] +                data.append(", ".join(my_vals) or u"")              datas.append(data)          if manual_sort_key:              # +1 because the id is added as a first col -            idx_col = table_cols.index(manual_sort_key) + 1 -            datas = sorted(datas, key=lambda x: x[idx_col]) -            if sign == '-': -                datas = reversed(datas) -            datas = list(datas)[start:end] +            idx_col = None +            if manual_sort_key in table_cols: +                idx_col = table_cols.index(manual_sort_key) + 1 +            else: +                for idx, col in enumerate(table_cols): +                    if type(col) in (list, tuple) and \ +                            manual_sort_key in col: +                        idx_col = idx + 1 +            if idx_col is not None: +                datas = sorted(datas, key=lambda x: x[idx_col]) +                if sign == '-': +                    datas = reversed(datas) +                datas = list(datas)[start:end]          link_template = "<a class='display_details' href='#' "\                          "onclick='load_window(\"%%s\")'>%s</a>" % \                          (unicode(_("Details"))) @@ -537,7 +562,10 @@ def get_item(model, func_name, default_name, extra_request_keys=[],                  res = {'id': data[0], 'link': lnk}                  for idx, value in enumerate(data[1:]):                      if value: -                        res[table_cols[idx].split('.')[-1]] = value +                        table_col = table_cols[idx] +                        if type(table_col) in (list, tuple): +                            table_col = table_col[0] +                        res[table_col.split('.')[-1]] = value                  rows.append(res)              data = json.dumps({                  "records": items_nb, diff --git a/ishtar_common/widgets.py b/ishtar_common/widgets.py index 6ffb84084..509907034 100644 --- a/ishtar_common/widgets.py +++ b/ishtar_common/widgets.py @@ -528,22 +528,27 @@ class JQueryJqGrid(forms.RadioSelect):          col_names, extra_cols = [], []          for field_name in getattr(self.associated_model, self.table_cols):              field = self.associated_model +            if type(field_name) in (list, tuple): +                field_name = field_name[0] +            field_verbose_names = []              keys = field_name.split('.')              field_verbose_name = ""              for key in keys: -                if hasattr(field, 'rel'): +                if hasattr(field, 'rel') and field.rel:                      field = field.rel.to                  try:                      field = field._meta.get_field(key)                      field_verbose_name = field.verbose_name                      field_name = field.name -                except fields.FieldDoesNotExist: +                except (fields.FieldDoesNotExist, AttributeError):                      if hasattr(field, key + '_lbl'):                          field_name = key                          field_verbose_name = getattr(field, key + '_lbl')                      else:                          continue -            col_names.append(u'"%s"' % field_verbose_name) +            field_verbose_names.append(unicode(field_verbose_name)) +            col_names.append(u'"%s"' % settings.JOINT.join( +                [f for f in field_verbose_names if f]))              extra_cols.append(self.COL_TPL % {'idx': field_name})          col_names = col_names and ", ".join(col_names) or ""          extra_cols = extra_cols and ", ".join(extra_cols) or "" | 
