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.
This commit is contained in:
parent
21d90e6cd8
commit
af3315b3f1
2 changed files with 8 additions and 1 deletions
|
@ -61,6 +61,13 @@ class Suffix(models.Model):
|
||||||
if old.prefix != self.prefix:
|
if old.prefix != self.prefix:
|
||||||
raise ValueError("Prefix cannot be changed")
|
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)
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -152,7 +152,7 @@ class SuffixCreateView(LoginRequiredMixin, CreateView):
|
||||||
success_url = reverse_lazy("suffix_list")
|
success_url = reverse_lazy("suffix_list")
|
||||||
|
|
||||||
def dispatch(self, request, *args, **kwargs):
|
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 self.handle_no_permission()
|
||||||
return super().dispatch(request, *args, **kwargs)
|
return super().dispatch(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue