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:
Kumi 2024-06-22 19:38:13 +02:00
parent 21d90e6cd8
commit af3315b3f1
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 8 additions and 1 deletions

View file

@ -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)

View file

@ -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)