diff options
author | Matthew Lemon <lemon@matthewlemon.com> | 2020-10-11 15:47:21 +0100 |
---|---|---|
committer | Matthew Lemon <lemon@matthewlemon.com> | 2020-10-11 15:47:21 +0100 |
commit | f69d29b53b6273d1b1d72cbcce4195e42788d556 (patch) | |
tree | b011549b86cf9993c7efc05947f78825f97a3aa9 /ctrack | |
parent | abd1b4926bc3aa1d0daba5bc5594a385898dd753 (diff) |
constraint added to CAFSingleDateEvent
Diffstat (limited to '')
-rw-r--r-- | ctrack/register/models.py | 58 | ||||
-rw-r--r-- | ctrack/register/tests/test_event_models.py | 44 |
2 files changed, 79 insertions, 23 deletions
diff --git a/ctrack/register/models.py b/ctrack/register/models.py index 12247e8..06c0959 100644 --- a/ctrack/register/models.py +++ b/ctrack/register/models.py @@ -71,8 +71,12 @@ class EventBase(AuditableEventBase): null=True, help_text="URL only - do not try to drag a file here.", ) - comments = models.TextField(max_length=1000, blank=True, null=True, - help_text="Use this to provide further detail about the event.") + comments = models.TextField( + max_length=1000, + blank=True, + null=True, + help_text="Use this to provide further detail about the event.", + ) class Meta: abstract = True @@ -107,7 +111,7 @@ class CAFMixin(models.Model): class SingleDateTimeEvent(EventBase, ThirdPartyEventMixin, SingleDateTimeEventMixin): AVAILABLE_TYPES = [ (EventType.PHONE_CALL.name, "Phone Call"), - (EventType.VIDEO_CALL.name, "Video Call") + (EventType.VIDEO_CALL.name, "Video Call"), ] type_descriptor = models.CharField(max_length=50, choices=AVAILABLE_TYPES) @@ -116,9 +120,7 @@ class SingleDateTimeEvent(EventBase, ThirdPartyEventMixin, SingleDateTimeEventMi class MeetingEvent(EventBase, ThirdPartyEventMixin, SingleDateTimeEventMixin): - AVAILABLE_TYPES = [ - (EventType.MEETING.name, "Meeting") - ] + AVAILABLE_TYPES = [(EventType.MEETING.name, "Meeting")] type_descriptor = models.CharField(max_length=50, choices=AVAILABLE_TYPES) @@ -128,9 +130,17 @@ class CAFSingleDateEvent(EventBase, CAFMixin, SingleDateMixin): ] type_descriptor = models.CharField(max_length=50, choices=AVAILABLE_TYPES) + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["date", "type_descriptor"], name="unique_caf_for_date" + ) + ] + # OLD CODE BELOW + class EngagementType(models.Model): """ Examples here are Phone, Email, Letter, Site visit, Meeting, Audit, Inspection, etc. @@ -162,28 +172,46 @@ class EngagementEvent(models.Model): type = models.ForeignKey(EngagementType, on_delete=models.CASCADE) short_description = models.CharField( - max_length=50, help_text="Short description of the event. Use Comments field for full detail." + max_length=50, + help_text="Short description of the event. Use Comments field for full detail.", ) participants = models.ManyToManyField(Person, blank=True) user = models.ForeignKey(get_user_model(), on_delete=models.SET(get_sentinel_user)) date = models.DateTimeField() - end_date = models.DateTimeField(blank=True, null=True, help_text="Should be used for periodic events.") - document_link = models.URLField(max_length=1000, blank=True, null=True, - help_text="URL only - do not try to drag a file here.") + end_date = models.DateTimeField( + blank=True, null=True, help_text="Should be used for periodic events." + ) + document_link = models.URLField( + max_length=1000, + blank=True, + null=True, + help_text="URL only - do not try to drag a file here.", + ) response_date_requested = models.DateField(blank=True, null=True) response_received = models.DateField(blank=True, null=True) related_caf = models.ForeignKey( - "caf.CAF", blank=True, on_delete=models.CASCADE, null=True, - help_text="If the event relates to a CAF, refer to it here." + "caf.CAF", + blank=True, + on_delete=models.CASCADE, + null=True, + help_text="If the event relates to a CAF, refer to it here.", + ) + comments = models.TextField( + max_length=1000, + blank=True, + null=True, + help_text="Use this to provide further detail about the event.", ) - comments = models.TextField(max_length=1000, blank=True, null=True, - help_text="Use this to provide further detail about the event.") def days_to_response_due(self) -> Optional[Dict[int, str]]: if self.response_date_requested: today = std_date.today() diff = self.response_date_requested - today - return dict(days=diff.days, descriptor=_style_descriptor(diff.days), day_str=_day_string(diff.days)) + return dict( + days=diff.days, + descriptor=_style_descriptor(diff.days), + day_str=_day_string(diff.days), + ) else: return None diff --git a/ctrack/register/tests/test_event_models.py b/ctrack/register/tests/test_event_models.py index d00744b..132f076 100644 --- a/ctrack/register/tests/test_event_models.py +++ b/ctrack/register/tests/test_event_models.py @@ -1,14 +1,19 @@ import datetime import pytest +from django.db import IntegrityError -from ctrack.register.models import MeetingEvent, EventType, SingleDateTimeEvent, CAFSingleDateEvent +from ctrack.register.models import ( + MeetingEvent, + EventType, + SingleDateTimeEvent, + CAFSingleDateEvent, +) pytestmark = pytest.mark.django_db def test_caf_initial_caf_received(user, caf): - uname = user.name now = datetime.datetime.now() e = CAFSingleDateEvent.objects.create( type_descriptor="CAF_INITIAL_CAF_RECEIVED", @@ -16,11 +21,31 @@ def test_caf_initial_caf_received(user, caf): short_description="CAF received for X Company", date="2020-10-10", comments="Nice comments for this event", - user=user + user=user, ) assert e.created_date.day == now.day +def test_cannot_add_two_caf_initial_caf_received_events_on_same_date(user, caf): + CAFSingleDateEvent.objects.create( + type_descriptor="CAF_INITIAL_CAF_RECEIVED", + related_caf=caf, + short_description="CAF received for X Company", + date="2020-10-10", + comments="Nice comments for this event", + user=user, + ) + with pytest.raises(IntegrityError): + CAFSingleDateEvent.objects.create( + type_descriptor="CAF_INITIAL_CAF_RECEIVED", + related_caf=caf, + short_description="CAF received for X Company", + date="2020-10-10", + comments="Nice comments for this event", + user=user, + ) + + def test_event_type_enum(): assert EventType.MEETING.name == "MEETING" assert EventType.PHONE_CALL.name == "PHONE_CALL" @@ -33,7 +58,10 @@ def test_event_type_enum(): assert EventType.CAF_PEER_REVIEW_PERIOD.name == "CAF_PEER_REVIEW_PERIOD" assert EventType.CAF_VALIDATION_PERIOD.name == "CAF_VALIDATION_PERIOD" assert EventType.CAF_VALIDATION_SIGN_OFF.name == "CAF_VALIDATION_SIGN_OFF" - assert EventType.CAF_VALIDATION_RECORD_EMAILED_TO_OES.name == "CAF_VALIDATION_RECORD_EMAILED_TO_OES" + assert ( + EventType.CAF_VALIDATION_RECORD_EMAILED_TO_OES.name + == "CAF_VALIDATION_RECORD_EMAILED_TO_OES" + ) def test_meeting_event(person, user): @@ -45,7 +73,7 @@ def test_meeting_event(person, user): datetime="2020-10-10T15:00", comments="Nice comments", location="Harvey's House", - user=user + user=user, ) e.participants.add(person) assert len(e.participants.all()) == 1 @@ -65,7 +93,7 @@ def test_single_date_event(person, user): datetime="2020-10-10T15:00", comments="Comments on phone call", # location is optional - user=user + user=user, ) phone_event.participants.add(person) assert phone_event.type_descriptor == "Phone Call" @@ -78,7 +106,7 @@ def test_single_date_event(person, user): datetime="2020-10-10T15:00", comments="Comments on video call", # location is optional - user=user + user=user, ) email.participants.add(person) assert email.type_descriptor == "Video Call" @@ -90,7 +118,7 @@ def test_single_date_event(person, user): datetime="2020-10-10T15:00", comments="Comments on email", # location is optional - user=user + user=user, ) email.participants.add(person) assert email.type_descriptor == "Email" |