From dcfe64a0a7deb3b99f05234c5ae549a57cd0539f Mon Sep 17 00:00:00 2001 From: xenua Date: Sun, 19 Jun 2022 17:33:55 +0200 Subject: [PATCH] add validation --- leftists/models.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/leftists/models.py b/leftists/models.py index 35d94b6..12752d2 100644 --- a/leftists/models.py +++ b/leftists/models.py @@ -1,3 +1,4 @@ +from django.core.exceptions import ValidationError from django.db import models from django.utils.translation import gettext_lazy as _ from xenua.django.models import RandomSlugPKMixin @@ -18,12 +19,19 @@ class Domain(models.Model): return cls.objects.get(fqdn__iexact=r.get_host()) +def validate_location(loc): + if "/" in loc: + raise ValidationError( + _('location must not contain a slash') + ) + + class ShortLink(RandomSlugPKMixin, models.Model): class Meta: unique_together = ('domain', 'location') domain = models.ForeignKey(Domain, related_name='links', on_delete=models.CASCADE) - location = models.CharField(_("short link"), max_length=100) + location = models.CharField(_("short link"), max_length=100, validators=[validate_location]) to = models.URLField(_("redirect to"), max_length=2500) click_count = models.IntegerField(default=0)