-
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
258 additions
and
137 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,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()) |
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 app signals.""" | ||
|
||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from django.utils import timezone | ||
|
||
from apps.core.models import DeliveryPoint | ||
|
||
from .models import Consent | ||
from .settings import CONSENT_END_DATE | ||
|
||
|
||
@receiver(post_save, sender=DeliveryPoint) | ||
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, | ||
start=timezone.now(), | ||
end=CONSENT_END_DATE, | ||
) |
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,23 @@ | ||
"""Dashboard consent signals tests.""" | ||
|
||
import pytest | ||
|
||
from apps.consent.models import Consent | ||
from apps.consent.settings import 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 | ||
assert consent.end == CONSENT_END_DATE |
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 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) |
Oops, something went wrong.