From af3315b3f1007a846221e4f9e7e33e1107cfea51 Mon Sep 17 00:00:00 2001 From: Kumi Date: Sat, 22 Jun 2024 19:38:13 +0200 Subject: [PATCH] feat: enforce maximum suffix limit for users Added validation in both the model and view to enforce a limit on the number of suffixes a user can create based on their maximum suffixes property. This ensures that users cannot exceed their allowed number of suffixes and maintains system integrity. --- freedoi/resolver/models.py | 7 +++++++ freedoi/resolver/views.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/freedoi/resolver/models.py b/freedoi/resolver/models.py index e5c73cc..4b00873 100644 --- a/freedoi/resolver/models.py +++ b/freedoi/resolver/models.py @@ -61,6 +61,13 @@ class Suffix(models.Model): if old.prefix != self.prefix: raise ValueError("Prefix cannot be changed") + else: + if ( + Suffix.objects.filter(prefix=self.prefix).count() + >= self.owner.maximum_suffixes + ): + raise ValueError("Maximum number of suffixes reached") + super().save(*args, **kwargs) diff --git a/freedoi/resolver/views.py b/freedoi/resolver/views.py index 6167d44..f1fa4c8 100644 --- a/freedoi/resolver/views.py +++ b/freedoi/resolver/views.py @@ -152,7 +152,7 @@ class SuffixCreateView(LoginRequiredMixin, CreateView): success_url = reverse_lazy("suffix_list") def dispatch(self, request, *args, **kwargs): - if request.user.suffix_set.count() >= request.user.suffix_limit: + if request.user.suffix_set.count() >= request.user.maximum_suffixes: return self.handle_no_permission() return super().dispatch(request, *args, **kwargs)