add a check if a slug of Fachschaftssitzung exists already

This commit is contained in:
2020-08-28 00:25:29 +00:00
parent 41a75f16f4
commit 3c697e3e93

View File

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