Skip to content

Commit 4b97ee4

Browse files
committed
Added IdentificationTaskQuerySet.browsable
1 parent bd43138 commit 4b97ee4

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

api/views.py

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -540,42 +540,7 @@ class IdentificationTaskViewSet(RetrieveModelMixin, ListModelMixin, GenericNoMob
540540
lookup_url_kwarg = 'observation_uuid'
541541

542542
def get_queryset(self):
543-
qs = super().get_queryset()
544-
user = self.request.user
545-
546-
view_archived_perm = '%(app_label)s.view_archived_identificationtasks' % {
547-
'app_label': IdentificationTask._meta.app_label,
548-
}
549-
# Exclude archived tasks unless user is a full User and has permission
550-
if not user.has_perm(view_archived_perm):
551-
qs = qs.exclude(status=IdentificationTask.Status.ARCHIVED)
552-
553-
view_perm = '%(app_label)s.view_%(model_name)s' % {
554-
'app_label': IdentificationTask._meta.app_label,
555-
'model_name': IdentificationTask._meta.model_name
556-
}
557-
has_view_perm = user.has_perm(view_perm)
558-
559-
user_role = user
560-
if isinstance(user, User):
561-
user_role = UserStat.objects.filter(user=user).first()
562-
has_role_view_perm = user_role and user_role.has_role_permission_by_model(action='view', model=IdentificationTask, country=None)
563-
564-
if has_view_perm or has_role_view_perm:
565-
return qs
566-
567-
if isinstance(user, User):
568-
# If user is a regular User, filter by their own tasks
569-
qs_annotated = qs.annotated_by(users=[user])
570-
else:
571-
qs_annotated = qs.none()
572-
573-
# Filter by countries if user has region-specific permissions
574-
countries = user_role.get_countries_with_permissions(action='view', model=IdentificationTask) if user_role else []
575-
if countries:
576-
return qs_annotated | qs.filter(report__country__in=countries)
577-
578-
return qs_annotated
543+
return super().get_queryset().browsable(user=self.request.user)
579544

580545
@extend_schema(
581546
request=None,

tigacrafting/managers.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import timedelta
2-
from typing import Optional, List
2+
from typing import Optional, List, Union
33

44
from django.conf import settings
55
from django.contrib.auth.models import User
@@ -309,6 +309,48 @@ def _assignable(self, state: bool = True) -> QuerySet:
309309
)
310310
)
311311

312+
# OTHER QUERYSETS
313+
def browsable(self, user: Union[User, 'tigaserver_app.TigaUser']) -> QuerySet:
314+
from tigacrafting.models import IdentificationTask, UserStat
315+
316+
view_archived_perm = '%(app_label)s.view_archived_identificationtasks' % {
317+
'app_label': IdentificationTask._meta.app_label,
318+
}
319+
qs = self
320+
# Exclude archived tasks unless user is a full User and has permission
321+
if not user.has_perm(view_archived_perm):
322+
qs = qs.exclude(status=IdentificationTask.Status.ARCHIVED)
323+
324+
view_perm = '%(app_label)s.view_%(model_name)s' % {
325+
'app_label': IdentificationTask._meta.app_label,
326+
'model_name': IdentificationTask._meta.model_name
327+
}
328+
has_view_perm = user.has_perm(view_perm)
329+
330+
user_role = user
331+
if isinstance(user, User):
332+
try:
333+
user_role = user.userstat
334+
except UserStat.DoesNotExist:
335+
user_role = None
336+
has_role_view_perm = user_role and user_role.has_role_permission_by_model(action='view', model=IdentificationTask, country=None)
337+
338+
if has_view_perm or has_role_view_perm:
339+
return qs
340+
341+
if isinstance(user, User):
342+
# If user is a regular User, filter by their own tasks
343+
qs_annotated = qs.annotated_by(users=[user])
344+
else:
345+
qs_annotated = qs.none()
346+
347+
# Filter by countries if user has region-specific permissions
348+
countries = user_role.get_countries_with_permissions(action='view', model=IdentificationTask) if user_role else []
349+
if countries:
350+
return qs_annotated | qs.filter(report__country__in=countries)
351+
352+
return qs_annotated
353+
312354
IdentificationTaskManager = models.Manager.from_queryset(IdentificationTaskQuerySet)
313355

314356
class ExpertReportAnnotationQuerySet(models.QuerySet):

0 commit comments

Comments
 (0)