diff options
Diffstat (limited to 'ishtar_common/views.py')
| -rw-r--r-- | ishtar_common/views.py | 47 | 
1 files changed, 35 insertions, 12 deletions
| diff --git a/ishtar_common/views.py b/ishtar_common/views.py index d89e35bee..f5471583c 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -19,6 +19,7 @@  from tidylib import tidy_document as tidy +from copy import copy  import csv  import cStringIO as StringIO  import datetime @@ -342,7 +343,7 @@ HIERARCHIC_FIELDS = ['periods', 'period', 'unit', 'material_types',  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=[], own_table_cols=None): +             specific_perms=[], own_table_cols=None, relation_types_prefix={}):      """      Generic treatment of tables      """ @@ -406,11 +407,19 @@ def get_item(model, func_name, default_name, extra_request_keys=[],          except ValueError:              return HttpResponse('[]', mimetype='text/plain') -        relation_types = set() -        for k in request_items: -            if k.startswith('relation_types_'): -                relation_types.add(request_items[k]) -                continue +        # manage relations types +        my_rtypes_prefix = copy(relation_types_prefix) +        if 'relation_types' not in my_rtypes_prefix: +            my_rtypes_prefix['relation_types'] = '' +        relation_types = {} +        for rtype_key in my_rtypes_prefix: +            relation_types[my_rtypes_prefix[rtype_key]] = set() +            for k in request_items: +                if k.startswith(rtype_key): +                    relation_types[my_rtypes_prefix[rtype_key]].add( +                        request_items[k]) +                    continue +          for k in request_keys:              val = request_items.get(k)              if not val: @@ -509,14 +518,26 @@ def get_item(model, func_name, default_name, extra_request_keys=[],              alt_dct.update(or_req)              query = query | Q(**alt_dct) -        if relation_types: +        for rtype_prefix in relation_types: +            vals = list(relation_types[rtype_prefix]) +            if not vals: +                continue              alt_dct = { -                'right_relations__relation_type__pk__in': list(relation_types)} +                rtype_prefix + 'right_relations__relation_type__pk__in': vals}              for k in dct:                  val = dct[k] -                if k == 'year': -                    k = 'year__exact' -                alt_dct['right_relations__right_record__' + k] = val +                if rtype_prefix: +                    # only get conditions related to the object +                    if rtype_prefix not in k: +                        continue +                    # tricky: reconstruct the key to make sense - remove the +                    # prefix from the key +                    k = k[0:k.index(rtype_prefix)] + k[ +                        k.index(rtype_prefix) + len(rtype_prefix):] +                if k.endswith('year'): +                    k += '__exact' +                alt_dct[rtype_prefix + 'right_relations__right_record__' + k] =\ +                    val              if not dct:                  # fake condition to trick Django (1.4): without it only the                  # alt_dct is managed @@ -529,7 +550,9 @@ def get_item(model, func_name, default_name, extra_request_keys=[],                      val = or_req[j]                      if j == 'year':                          j = 'year__exact' -                    altor_dct['right_relations__right_record__' + j] = val +                    altor_dct[ +                        rtype_prefix + 'right_relations__right_record__' + j] =\ +                        val                  query = query | Q(**altor_dct)          if own: | 
