From 9a63b07c1270f47c0409bf463a4197570fef530c Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Sun, 22 Apr 2018 14:22:42 +0200 Subject: Refactoring of query owns (refs #4060) --- archaeological_files/models.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'archaeological_files/models.py') 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 @@ -444,11 +444,29 @@ class File(ClosedItem, BaseHistorizedItem, OwnPerms, ValueGetter, return '00' 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) -- cgit v1.2.3