Skip to content

Commit

Permalink
fix(api-notif): Fix order of validators
Browse files Browse the repository at this point in the history
  • Loading branch information
kiblik committed Jul 8, 2024
1 parent 3f9e62b commit 0d7032b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
18 changes: 10 additions & 8 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2952,6 +2952,7 @@ class Meta:
def validate(self, data):
user = None
product = None
template = False

if self.instance is not None:
user = self.instance.user
Expand All @@ -2961,25 +2962,26 @@ def validate(self, data):
user = data.get("user")
if "product" in data:
product = data.get("product")
if "template" in data:
template = data.get("template")

if (
template
and Notifications.objects.filter(template=True).count() > 0
):
msg = "Notification template already exists"
raise ValidationError(msg)
if (
self.instance is None
or user != self.instance.user
or product != self.instance.product
):
notifications = Notifications.objects.filter(
user=user, product=product, template=False,
user=user, product=product, template=template,
).count()
if notifications > 0:
msg = "Notification for user and product already exists"
raise ValidationError(msg)
if (
data.get("template")
and Notifications.objects.filter(template=True).count() > 0
):
msg = "Notification template already exists"
raise ValidationError(msg)

return data


Expand Down
2 changes: 1 addition & 1 deletion unittests/test_apiv2_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def setUp(self):
template=True,
scan_added=['alert', 'slack'],
)
self.assertEqual(r.status_code, 201)
self.assertEqual(r.status_code, 201, r.data)

def create(self, **kwargs):
return self.client.post(reverse('notifications-list'), kwargs, format='json')
Expand Down

0 comments on commit 0d7032b

Please sign in to comment.