Skip to content

Commit

Permalink
fixup! ✨(dashboard) add signals on delivery point creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ssorin committed Dec 19, 2024
1 parent 3995116 commit a51396d
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 25 deletions.
13 changes: 0 additions & 13 deletions src/dashboard/apps/consent/app_settings.py

This file was deleted.

2 changes: 1 addition & 1 deletion src/dashboard/apps/consent/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from apps.auth.factories import UserFactory
from apps.core.factories import DeliveryPointFactory

from .app_settings import CONSENT_END_DATE
from .models import Consent
from .settings import CONSENT_END_DATE


class ConsentFactory(factory.django.DjangoModelFactory):
Expand Down
14 changes: 14 additions & 0 deletions src/dashboard/apps/consent/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Dashboard consent app settings."""

from django.conf import settings

from .utils import get_end_date

# `CONSENT_END_DATE` determines the default end date for the period consents
# (`datetime` format).
# This value is calculated dynamically via `apps.consent.utils.get_end_date()`
# By default `apps.consent.utils.get_end_date()` return the last day of current year.
# You can specify the number of days to add to the current date to calculate the end
# date.
# ie: `get_end_date(90)` will return the current date + 90 days.
CONSENT_END_DATE = getattr(settings, "CONSENT_END_DATE", get_end_date())
2 changes: 1 addition & 1 deletion src/dashboard/apps/consent/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

from apps.core.models import DeliveryPoint

from .app_settings import CONSENT_END_DATE
from .models import Consent
from .settings import CONSENT_END_DATE


@receiver(post_save, sender=DeliveryPoint)
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/apps/consent/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from django.utils import formats

from apps.consent import AWAITING, REVOKED, VALIDATED
from apps.consent.app_settings import CONSENT_END_DATE
from apps.consent.models import Consent
from apps.consent.settings import CONSENT_END_DATE
from apps.core.factories import DeliveryPointFactory


Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/apps/consent/tests/test_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import pytest

from apps.consent.app_settings import CONSENT_END_DATE
from apps.consent.models import Consent
from apps.consent.settings import CONSENT_END_DATE
from apps.core.factories import DeliveryPointFactory


Expand Down
33 changes: 33 additions & 0 deletions src/dashboard/apps/consent/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Dashboard consent utils tests."""

from datetime import datetime, timedelta, timezone

import pytest

from apps.consent.utils import get_end_date


@pytest.mark.django_db
def test_get_end_date_with_days():
"""Test `get_end_date` function when days argument is provided."""
days = 10
end_date = get_end_date(days=days).replace(microsecond=0)
expected_date = datetime.now().replace(tzinfo=timezone.utc) + timedelta(days=days)
assert end_date == expected_date.replace(microsecond=0)


@pytest.mark.django_db
def test_get_end_date_without_days():
"""Test `get_end_date` function when days argument is not provided."""
end_date = get_end_date().replace(microsecond=0)
current_year = datetime.now().year
expected_date = datetime(
year=current_year,
month=12,
day=31,
hour=23,
minute=59,
second=59,
tzinfo=timezone.utc,
)
assert end_date == expected_date.replace(microsecond=0)
21 changes: 21 additions & 0 deletions src/dashboard/apps/consent/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Dashboard consent app utils."""

from datetime import datetime, timedelta, timezone


def get_end_date(days: int | None = None) -> datetime:
"""Get the end date of the consent period.
Returns a specific end date based on the number of days provided,
or the default end of the year date if no argument is passed.
Parameters:
days (int | None): An optional number of days to calculate the future date.
If None, the function defaults to the end of the current year.
"""
if days:
return datetime.now().replace(tzinfo=timezone.utc) + timedelta(days=days)

return datetime.now().replace(
month=12, day=31, hour=23, minute=59, second=59, tzinfo=timezone.utc
)
12 changes: 6 additions & 6 deletions src/dashboard/apps/core/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,12 +178,12 @@ def test_count_validated_consents():
ConsentFactory(delivery_point=dl, status=VALIDATED)

# create awaiting consents for entity1 in past period
ConsentFactory(delivery_point=dl,
status=AWAITING,
start=timezone.now() - timedelta(days=300),
end=timezone.now() - timedelta(days=270),
)

ConsentFactory(
delivery_point=dl,
status=AWAITING,
start=timezone.now() - timedelta(days=300),
end=timezone.now() - timedelta(days=270),
)

assert (
Consent.active_objects.filter(
Expand Down
15 changes: 14 additions & 1 deletion src/dashboard/dashboard/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from django.utils.translation import gettext_lazy as _
from sentry_sdk.integrations.django import DjangoIntegration

from apps.consent.utils import get_end_date

# is pytest running
TEST = os.environ.get("TEST", False)

Expand Down Expand Up @@ -174,7 +176,18 @@
send_default_pii=True,
)

# Debug-toolbar
## Consent app

# `CONSENT_END_DATE` determines the default end date for the period consents
# (`datetime` format).
# This value is calculated dynamically via `apps.consent.utils.get_end_date()`
# By default `apps.consent.utils.get_end_date()` return the last day of current year.
# You can specify the number of days to add to the current date to calculate the end
# date.
# ie: `get_end_date(90)` will return the current date + 90 days.
CONSENT_END_DATE = get_end_date()

## Debug-toolbar

# Despite the `DEBUG` being set to `False`, for some tests,
# pytest seems to consider `DEBUG` to be `True`.
Expand Down
2 changes: 1 addition & 1 deletion src/dashboard/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class HomeConfig(AppConfig):

### apps.consent.signals.handle_new_delivery_point()
There is a signal on the creation of a `delivery point` (`apps.core.models.DeliveryPoint`).
This signal allows the creation of the `consent` (`apps.consent.models.Consent`)
This signal allows the creation of a `consent` (`apps.consent.models.Consent`)
corresponding to the `delivery_point`.

## License
Expand Down

0 comments on commit a51396d

Please sign in to comment.