diff --git a/fet2020/posts/models.py b/fet2020/posts/models.py index 87014f8b..2b2a2dcf 100644 --- a/fet2020/posts/models.py +++ b/fet2020/posts/models.py @@ -1,4 +1,5 @@ from django.contrib.auth.models import User +from django.core.validators import ValidationError from django.db import models from django.db.models import Q from django.urls import reverse @@ -168,7 +169,8 @@ class Post(models.Model): # TODO: Explain why this image is selected on save of the image # Query all posts that have a slug that equals one of the tags posts1 = Post.objects.filter( - slug__in=self.tags.names()).filter(image__isnull=False)[0:1].all() + slug__in=self.tags.names() + ).filter(image__isnull=False)[0:1].all() if len(posts1) > 0: return posts1.get().image @@ -186,11 +188,12 @@ class Post(models.Model): "save the post with some defaults" if (self.id is None) and (not self.slug): self.slug = slugify(self.public_date.date()) + "-" + slugify(self.title) - super().save(*args, **kwargs) - self.tags.set(*re.findall(r'\#([\d\w-]+)', str(self.subtitle)), - *re.findall(r'\#([\d\w-]+)', str(self.title))) + self.tags.set( + *re.findall(r'\#([\d\w-]+)', str(self.subtitle)), + *re.findall(r'\#([\d\w-]+)', str(self.title)) + ) def __str__(self): return "Post (%s, %s): %s " % (self.slug, self.public_date.strftime("%d.%m.%Y"), self.title) @@ -262,6 +265,12 @@ class FetMeeting(Event): verbose_name = "Fet Sitzung" verbose_name_plural = "Fet Sitzungen" + def clean(self): + slug = slugify(self.event_start.date()) + "-" + slugify("Fachschaftssitzung") + + if Post.objects.filter(slug=slug).count() != 0: + raise ValidationError(_('Es existiert bereits eine Sitzung mit demselben Datum.')) + def save(self, *args, **kwargs): self.title = "Fachschaftssitzung" self.slug = slugify(self.event_start.date()) + "-" + slugify(self.title)