Skip to content

Commit

Permalink
Allow subscriptions on account with same start date if different end
Browse files Browse the repository at this point in the history
ref #711
  • Loading branch information
rlskoeser committed Dec 9, 2020
1 parent 614f5f2 commit b0ac51b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion mep/accounts/models.py
Expand Up @@ -464,7 +464,8 @@ def validate_unique(self, *args, **kwargs):
# (can't use unique_together because of multi-table inheritance)

# adapted from https://stackoverflow.com/questions/7366363/adding-custom-django-model-validation
qs = Subscription.objects.filter(start_date=self.start_date,
qs = Subscription.objects.filter(
start_date=self.start_date, end_date=self.end_date,
account=self.account, subtype=self.subtype)

# if current work is already saved, exclude it from the queryset
Expand Down
19 changes: 17 additions & 2 deletions mep/accounts/tests/test_accounts_models.py
Expand Up @@ -695,11 +695,26 @@ def test_validate_unique(self):
# creating new subscription for same account & date should error
with pytest.raises(ValidationError):
subscr = Subscription(account=self.account,
start_date=self.subscription.start_date)
start_date=self.subscription.start_date)
subscr.validate_unique()

# same account + date with different subtype should be fine
# creating new subscription for same account with same
# start date AND end date should error
with pytest.raises(ValidationError):
subscr = Subscription(account=self.account,
start_date=self.subscription.start_date,
end_date=self.subscription.end_date)
subscr.validate_unique()

# same account, type, start date but different end date is valid
subscr = Subscription(account=self.account,
start_date=self.subscription.start_date,
end_date=datetime.date(1931, 3, 6))
subscr.validate_unique()

# same account + date with different subtype should be fine
subscr = Subscription(
account=self.account,
start_date=self.subscription.start_date, subtype='ren')
subscr.validate_unique()

Expand Down

0 comments on commit b0ac51b

Please sign in to comment.