Skip to content

Commit

Permalink
✨(dashboard) Move consent management and queryset files.
Browse files Browse the repository at this point in the history
- move functions to models (from querysets.py, permissions_utils.py, consent_management.py)
- remove old modules (querysets.py, permissions_utils.py, consent_management.py and test_querysets.py)
- add test
  • Loading branch information
ssorin committed Dec 2, 2024
1 parent fb6bdda commit e284f51
Show file tree
Hide file tree
Showing 8 changed files with 561 additions and 420 deletions.
37 changes: 0 additions & 37 deletions src/dashboard/apps/consent/consent_management.py

This file was deleted.

103 changes: 102 additions & 1 deletion src/dashboard/apps/consent/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
"""Dashboard consent app models."""

from django.core.exceptions import PermissionDenied
from django.db import models
from django.db.models import Count, QuerySet, TextField
from django.db.models.functions import Cast
from django.utils import timezone
from django.utils.translation import gettext_lazy as _

from apps.auth.models import DashboardUser as User
from apps.core.models import DashboardBase, DeliveryPoint
from apps.core.models import DashboardBase, DeliveryPoint, Entity


class Consent(DashboardBase):
Expand Down Expand Up @@ -59,3 +62,101 @@ def save(self, *args, **kwargs):
if self.status == self.REVOKED:
self.revoked_at = timezone.now()
return super(Consent, self).save(*args, **kwargs)

@classmethod
def get_awaiting(cls, user, selected_entity=None):
"""Retrieves all awaiting consents or consents for a selected entity for a user.
Parameters:
- user (User): The user for whom the consents should be retrieved.
- selected_entity (Entity, optional): An optional entity qs. If provided,
consents will be filtered by this entity.
Returns:
QuerySet: A queryset of Consent objects that match the filter criteria, ordered
by entity and start.
"""
queryset_filters = {}
if selected_entity:
if selected_entity.user_has_perms(user):
queryset_filters["delivery_point__entity"] = selected_entity
else:
raise PermissionDenied()
else:
related_entities = Entity.get_by_user(user)
if related_entities:
queryset_filters["delivery_point__entity__in"] = related_entities
else:
queryset_filters["delivery_point__entity__users"] = user

return cls.objects.filter(
**queryset_filters,
status=cls.AWAITING,
delivery_point__is_active=True,
start__lte=timezone.now(),
end__gte=timezone.now(),
).order_by("delivery_point__entity", "start")

@staticmethod
def count_by_entity(user, status: str):
"""Counts and returns the number of consents for a given user for each entity.
Parameters:
- user (User): The user for whom to count consents.
- status (str): The status of the consents to be counted. Defaults VALIDATED.
"""
queryset_filters = {}
related_entities = Entity.get_by_user(user)
if related_entities:
queryset_filters["delivery_points__entity__in"] = related_entities
else:
queryset_filters["delivery_points__entity__users"] = user

return Entity.objects.filter(
**queryset_filters,
delivery_points__is_active=True,
delivery_points__consent__status=status,
delivery_points__consent__start__lte=timezone.now(),
delivery_points__consent__end__gte=timezone.now(),
).annotate(dcount=Count("delivery_points"))

@classmethod
def count_validated_by_entity(cls, user):
"""Counts the number of validated consents associated with a given entity."""
return cls.count_by_entity(user, cls.VALIDATED)

@classmethod
def count_awaiting_by_entity(cls, user):
"""Counts the number of validated consents associated with a given entity."""
return cls.count_by_entity(user, cls.AWAITING)

@classmethod
def update_consents(cls, status: str, selected_ids: list[str], user: User):
"""Updates the consent status to VALIDATED for the given list of consent IDs."""
return cls.objects.filter(id__in=selected_ids).update(
status=status,
created_by=user,
)

@classmethod
def update_validated(cls, selected_ids: list[str], user: User):
"""Updates the consent status to VALIDATED for the given list of consent IDs."""
return cls.update_consents(Consent.VALIDATED, selected_ids, user)

@classmethod
def update_awaiting(cls, selected_ids: list[str], user: User, consents: QuerySet):
"""Updates the consent status to AWAITING for the given list of consent IDs.
Parameters:
consents (QuerySet): A QuerySet of Consent objects to be processed.
selected_ids (list): A list of IDs for which the consent status should not be
updated.
"""
base_ids = list(
consents.annotate(str_id=Cast("id", output_field=TextField())).values_list(
"str_id", flat=True
)
)
awaiting_ids = [item for item in base_ids if item not in selected_ids]

return cls.update_consents(Consent.AWAITING, awaiting_ids, user)
25 changes: 0 additions & 25 deletions src/dashboard/apps/consent/permissions_utils.py

This file was deleted.

83 changes: 0 additions & 83 deletions src/dashboard/apps/consent/querysets.py

This file was deleted.

Loading

0 comments on commit e284f51

Please sign in to comment.