add a check if a slug of Fachschaftssitzung exists already
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
from django.core.validators import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.urls import reverse
|
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
|
# 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
|
# Query all posts that have a slug that equals one of the tags
|
||||||
posts1 = Post.objects.filter(
|
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:
|
if len(posts1) > 0:
|
||||||
return posts1.get().image
|
return posts1.get().image
|
||||||
|
|
||||||
@@ -186,11 +188,12 @@ class Post(models.Model):
|
|||||||
"save the post with some defaults"
|
"save the post with some defaults"
|
||||||
if (self.id is None) and (not self.slug):
|
if (self.id is None) and (not self.slug):
|
||||||
self.slug = slugify(self.public_date.date()) + "-" + slugify(self.title)
|
self.slug = slugify(self.public_date.date()) + "-" + slugify(self.title)
|
||||||
|
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
self.tags.set(*re.findall(r'\#([\d\w-]+)', str(self.subtitle)),
|
self.tags.set(
|
||||||
*re.findall(r'\#([\d\w-]+)', str(self.title)))
|
*re.findall(r'\#([\d\w-]+)', str(self.subtitle)),
|
||||||
|
*re.findall(r'\#([\d\w-]+)', str(self.title))
|
||||||
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "Post (%s, %s): %s " % (self.slug, self.public_date.strftime("%d.%m.%Y"), self.title)
|
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 = "Fet Sitzung"
|
||||||
verbose_name_plural = "Fet Sitzungen"
|
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):
|
def save(self, *args, **kwargs):
|
||||||
self.title = "Fachschaftssitzung"
|
self.title = "Fachschaftssitzung"
|
||||||
self.slug = slugify(self.event_start.date()) + "-" + slugify(self.title)
|
self.slug = slugify(self.event_start.date()) + "-" + slugify(self.title)
|
||||||
|
|||||||
Reference in New Issue
Block a user