-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨(dashboard) add signals on delivery point creation
Implemented a signal to automatically create a Consent object when a new DeliveryPoint is saved. Added corresponding tests to ensure functionality and updated related Pipfile dependencies. Centralized consent end date logic in a new app settings file (CONSENT_END_DATE) and updated all related signals, factories, and tests to use it. Updated documentation and code comments to reflect these changes.
- Loading branch information
Showing
16 changed files
with
319 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""Dashboard consent app settings.""" | ||
|
||
from django.conf import settings | ||
|
||
# CONSENT_NUMBER_DAYS_END_DATE allows to calculate the end date of a consent period by | ||
# adding a number of days to the current date. | ||
# If the value is None, the end date of the period will correspond to the last day of | ||
# the current year | ||
# More details on the calculation in the function: `utils.calculate_consent_end_date()` | ||
# ie: | ||
# CONSENT_NUMBER_DAYS_END_DATE = 90 will return the current date + 90 days. | ||
# CONSENT_NUMBER_DAYS_END_DATE = None will return 2024-12-31 23:59:59. | ||
CONSENT_NUMBER_DAYS_END_DATE = getattr(settings, "CONSENT_NUMBER_DAYS_END_DATE", None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Dashboard consent app signals.""" | ||
|
||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
|
||
from apps.core.models import DeliveryPoint | ||
|
||
from .models import Consent | ||
|
||
|
||
@receiver(post_save, sender=DeliveryPoint, dispatch_uid="handle_new_delivery_point") | ||
def handle_new_delivery_point(sender, instance, created, **kwargs): | ||
"""Signal triggered after a new DeliveryPoint is saved. | ||
Create a new Consent object for the delivery point. | ||
""" | ||
if created: | ||
Consent.objects.create(delivery_point=instance) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""Dashboard consent signals tests.""" | ||
|
||
import pytest | ||
|
||
from apps.consent.models import Consent | ||
from apps.consent.utils import calculate_consent_end_date | ||
from apps.core.factories import DeliveryPointFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_signal_triggered_on_delivery_point_creation(): | ||
"""Tests if the signal is triggered when creating a DeliveryPoint.""" | ||
assert Consent.objects.count() == 0 | ||
|
||
# Create new delivery point | ||
delivery_point = DeliveryPointFactory() | ||
|
||
# check if consent was created with signals after delivery_point creation. | ||
assert Consent.objects.count() == 1 | ||
|
||
consent = Consent.objects.first() | ||
assert consent.delivery_point == delivery_point | ||
expected_date = calculate_consent_end_date() | ||
assert consent.end.replace(microsecond=0) == expected_date.replace(microsecond=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 calculate_consent_end_date | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_calculate_consent_end_date_with_days(): | ||
"""Test `calculate_consent_end_date` function when days argument is provided.""" | ||
days = 10 | ||
end_date = calculate_consent_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_calculate_consent_end_date_without_days(): | ||
"""Test `calculate_consent_end_date` function when days argument is not provided.""" | ||
end_date = calculate_consent_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) |
Oops, something went wrong.