|
1 | 1 | from datetime import timedelta |
2 | | -from typing import Optional, List |
| 2 | +from typing import Optional, List, Union |
3 | 3 |
|
4 | 4 | from django.conf import settings |
5 | 5 | from django.contrib.auth.models import User |
@@ -309,6 +309,48 @@ def _assignable(self, state: bool = True) -> QuerySet: |
309 | 309 | ) |
310 | 310 | ) |
311 | 311 |
|
| 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 | + |
312 | 354 | IdentificationTaskManager = models.Manager.from_queryset(IdentificationTaskQuerySet) |
313 | 355 |
|
314 | 356 | class ExpertReportAnnotationQuerySet(models.QuerySet): |
|
0 commit comments