euscanwww: fixed homepage and category validation

Accept multiple urls but skip invalid urls, added virtual as accepted
category

Signed-off-by: volpino <fox91@anche.no>
This commit is contained in:
volpino 2012-05-28 18:16:48 +02:00
parent a224cb27cb
commit e535c204b0

View File

@ -1,11 +1,12 @@
from django.db import models from django.db import models
from django.core.validators import RegexValidator, validate_email, URLValidator from django.core.validators import RegexValidator, validate_email, URLValidator
from django.core.exceptions import ValidationError
from djeuscan.managers import PackageManager, VersionLogManager, \ from djeuscan.managers import PackageManager, VersionLogManager, \
EuscanResultManager EuscanResultManager
validate_category = RegexValidator("^\w+?-\w+?$") validate_category = RegexValidator("^(?:\w+?-\w+?)|virtual$")
validate_name = RegexValidator("^\S+?$") validate_name = RegexValidator("^\S+?$")
validate_revision = RegexValidator("^r\d+?$") validate_revision = RegexValidator("^r\d+?$")
validate_url = URLValidator() validate_url = URLValidator()
@ -56,7 +57,7 @@ class Package(models.Model):
category = models.CharField(max_length=128, validators=[validate_category]) category = models.CharField(max_length=128, validators=[validate_category])
name = models.CharField(max_length=128, validators=[validate_name]) name = models.CharField(max_length=128, validators=[validate_name])
description = models.TextField(blank=True) description = models.TextField(blank=True)
homepage = models.TextField(blank=True, validators=[validate_url]) homepage = models.TextField(blank=True)
herds = models.ManyToManyField(Herd, blank=True) herds = models.ManyToManyField(Herd, blank=True)
maintainers = models.ManyToManyField(Maintainer, blank=True) maintainers = models.ManyToManyField(Maintainer, blank=True)
@ -89,6 +90,18 @@ class Package(models.Model):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.full_clean() self.full_clean()
# Clean urls, accept only real urls
urls = []
for url in self.homepages:
try:
validate_url(url)
except ValidationError:
pass
else:
urls.append(url)
self.homepage = " ".join(urls)
super(Package, self).save(*args, **kwargs) super(Package, self).save(*args, **kwargs)
@property @property