diff options
| -rw-r--r-- | archaeological_context_records/models.py | 34 | ||||
| -rw-r--r-- | archaeological_files/models.py | 24 | ||||
| -rw-r--r-- | archaeological_finds/models_finds.py | 43 | ||||
| -rw-r--r-- | archaeological_operations/models.py | 34 | ||||
| -rw-r--r-- | ishtar_common/models.py | 28 | 
5 files changed, 105 insertions, 58 deletions
| diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 996304231..07429e606 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -429,17 +429,13 @@ class ContextRecord(BulkUpdatedItem, BaseHistorizedItem,      @classmethod      def get_query_owns(cls, ishtaruser): -        profile = ishtaruser.current_profile -        town_ids = [] -        if profile: -            town_ids = [town['pk'] -                        for town in profile.query_towns.values('pk').all()] -        return (Q(operation__scientist=ishtaruser.person) | -                Q(operation__in_charge=ishtaruser.person) | -                Q(operation__collaborators__pk=ishtaruser.person.pk) | -                Q(history_creator=ishtaruser.user_ptr) | -                Q(operation__towns__pk__in=town_ids)) \ -            & Q(operation__end_date__isnull=True) +        q = cls._construct_query_own( +            'operation__', Operation._get_query_owns_dicts(ishtaruser) +        ) | cls._construct_query_own('', [ +            {'history_creator': ishtaruser.user_ptr}, +            {'operation__end_date__isnull': True} +        ]) +        return q      @classmethod      def get_owns(cls, user, menu_filtr=None, limit=None, @@ -720,11 +716,13 @@ class ContextRecordSource(Source):          return self.context_record      @classmethod -    def get_query_owns(cls, user): -        return ( -            Q(context_record__operation__scientist=user.ishtaruser.person) | -            Q(context_record__operation__in_charge=user.ishtaruser.person) | -            Q(context_record__operation__collaborators__pk= -              user.ishtaruser.person.pk)) \ -               & Q(context_record__operation__end_date__isnull=True) +    def get_query_owns(cls, ishtaruser): +        q = cls._construct_query_own( +            'context_record__operation__', +            Operation._get_query_owns_dicts(ishtaruser) +        ) | cls._construct_query_own('', [ +            {'context_record__history_creator': ishtaruser.user_ptr}, +            {'context_record__operation__end_date__isnull': True} +        ]) +        return q diff --git a/archaeological_files/models.py b/archaeological_files/models.py index 36f28c2e8..5f4150695 100644 --- a/archaeological_files/models.py +++ b/archaeological_files/models.py @@ -445,10 +445,28 @@ class File(ClosedItem, BaseHistorizedItem, OwnPerms, ValueGetter,          return self.towns.all()[0].numero_insee[:2]      @classmethod +    def _get_query_owns_dicts(cls, ishtaruser): +        profile = ishtaruser.current_profile +        town_ids = [] +        if profile: +            town_ids = [town['pk'] +                        for town in profile.query_towns.values('pk').all()] +        return [ +            { +                'in_charge': ishtaruser.person, +                'history_creator': ishtaruser.user_ptr, +                'towns__pk__in': town_ids +            }, +            { +                'end_date__isnull': True +            } +        ] + +    @classmethod      def get_query_owns(cls, ishtaruser): -        return (Q(history_creator=ishtaruser.user_ptr) | -                Q(in_charge__ishtaruser=ishtaruser)) \ -            & Q(end_date__isnull=True) +        return cls._construct_query_own( +            '', cls._get_query_owns_dicts(ishtaruser) +        )      def is_active(self):          return not bool(self.end_date) diff --git a/archaeological_finds/models_finds.py b/archaeological_finds/models_finds.py index 7155f3906..40059faa6 100644 --- a/archaeological_finds/models_finds.py +++ b/archaeological_finds/models_finds.py @@ -37,7 +37,7 @@ from ishtar_common.models import GeneralType, HierarchicalType, ImageModel, \      HistoricalRecords, OwnPerms, Source, Person, Basket, post_save_cache, \      ValueGetter, get_current_profile, IshtarImage -from archaeological_operations.models import AdministrativeAct +from archaeological_operations.models import AdministrativeAct, Operation  from archaeological_context_records.models import ContextRecord, Dating  from ishtar_common.models import PRIVATE_FIELDS, SpatialReferenceSystem, \ @@ -1094,21 +1094,14 @@ class Find(BulkUpdatedItem, ValueGetter, BaseHistorizedItem, ImageModel,      @classmethod      def get_query_owns(cls, ishtaruser): -        profile = ishtaruser.current_profile -        town_ids = [] -        if profile: -            town_ids = [town['pk'] -                        for town in profile.query_towns.values('pk').all()] -        return ( -            Q(base_finds__context_record__operation__scientist= -              ishtaruser.person) | -            Q(base_finds__context_record__operation__in_charge= -              ishtaruser.person) | -            Q(base_finds__context_record__operation__collaborators__pk= -              ishtaruser.person.pk) | -            Q(history_creator=ishtaruser.user_ptr) | -            Q(base_finds__context_record__operation__towns__pk__in=town_ids) -        ) & Q(base_finds__context_record__operation__end_date__isnull=True) +        q = cls._construct_query_own( +            'base_finds__context_record__operation__', +            Operation._get_query_owns_dicts(ishtaruser) +        ) | cls._construct_query_own('', [ +            {'history_creator': ishtaruser.user_ptr}, +            {'base_finds__context_record__operation__end_date__isnull': True} +        ]) +        return q      @classmethod      def get_owns(cls, user, menu_filtr=None, limit=None, @@ -1497,15 +1490,15 @@ class FindSource(Source):      @classmethod      def get_query_owns(cls, ishtaruser): -        return (Q(find__base_finds__context_record__operation__scientist= -                  ishtaruser.person) | -                Q(find__base_finds__context_record__operation__in_charge= -                  ishtaruser.person) | -                Q( -            find__base_finds__context_record__operation__collaborators__pk= -            ishtaruser.person.pk)) \ -               & Q( -            find__base_finds__context_record__operation__end_date__isnull=True) +        q = cls._construct_query_own( +            'find__base_finds__context_record__operation__', +            Operation._get_query_owns_dicts(ishtaruser) +        ) | cls._construct_query_own('', [ +            {'find__history_creator': ishtaruser.user_ptr}, +            {'find__base_finds__context_record__operation__end_date__isnull': +             True} +        ]) +        return q  class Property(LightHistorizedItem): diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 9655ca387..b52477995 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -697,19 +697,30 @@ class Operation(ClosedItem, BaseHistorizedItem, ImageModel, OwnPerms,          return round(float(self.cost) / self.surface, 2)      @classmethod -    def get_query_owns(cls, ishtaruser): +    def _get_query_owns_dicts(cls, ishtaruser):          profile = ishtaruser.current_profile          town_ids = []          if profile:              town_ids = [town['pk']                          for town in profile.query_towns.values('pk').all()] -        return ( -            Q(in_charge=ishtaruser.person) | -            Q(scientist=ishtaruser.person) | -            Q(collaborators__pk=ishtaruser.person.pk) | -            Q(history_creator=ishtaruser.user_ptr) | -            Q(towns__pk__in=town_ids) -               ) & Q(end_date__isnull=True) +        return [ +            { +                'in_charge': ishtaruser.person, +                'scientist': ishtaruser.person, +                'collaborators__pk': ishtaruser.person.pk, +                'history_creator': ishtaruser.user_ptr, +                'towns__pk__in': town_ids +            }, +            { +                'end_date__isnull': True +            } +        ] + +    @classmethod +    def get_query_owns(cls, ishtaruser): +        return cls._construct_query_own( +            '', cls._get_query_owns_dicts(ishtaruser) +        )      def is_active(self):          return not bool(self.end_date) @@ -1074,10 +1085,9 @@ class OperationSource(Source):      @classmethod      def get_query_owns(cls, ishtaruser): -        return (Q(operation__in_charge=ishtaruser.person) | -                Q(operation__scientist=ishtaruser.person) | -                Q(operation__collaborators__pk=ishtaruser.person.pk)) \ -               & Q(operation__end_date__isnull=True) +        return cls._construct_query_own( +            'operation__', Operation._get_query_owns_dicts(ishtaruser) +        )  class ActType(GeneralType): diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 2ebe07961..c14635a51 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -364,6 +364,34 @@ class OwnPerms(object):                  items = [(i, cls.get_short_menu_class(i.pk)) for i in items]          return items +    @classmethod +    def _get_query_owns_dicts(cls, ishtaruser): +        """ +        List of query own dict to construct the query. +        Each dict are join with an AND operator, each dict key, values are +        joined with OR operator +        """ +        return [] + +    @classmethod +    def _construct_query_own(cls, prefix, dct_list): +        q = None +        for subquery_dict in dct_list: +            subquery = None +            for k in subquery_dict: +                subsubquery = Q(**{prefix + k: subquery_dict[k]}) +                if subquery: +                    subquery |= subsubquery +                else: +                    subquery = subsubquery +            if not subquery: +                continue +            if q: +                q &= subquery +            else: +                q = subquery +        return q +  class Cached(object):      slug_field = 'txt_idx' | 
