bornhack-website/src/villages/models.py
Thomas Steen Rasmussen 00af109e2f
add flake8 and isort to pre-commit config, make flake8 and isort happy (#441)
* add flake8 to pre-commit config, and fixup many things to make flake8 happy

* add isort and sort all imports, add to pre-commit and requirements
2020-02-12 13:10:41 +01:00

61 lines
1.9 KiB
Python

from django.db import models
from django.urls import reverse_lazy
from django.utils.text import slugify
from utils.models import CampRelatedModel, UUIDModel
class Village(UUIDModel, CampRelatedModel):
class Meta:
ordering = ["name"]
unique_together = ("slug", "camp")
contact = models.ForeignKey("auth.User", on_delete=models.PROTECT)
camp = models.ForeignKey("camps.Camp", on_delete=models.PROTECT)
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255, blank=True)
description = models.TextField(
help_text="A descriptive text about your village. Markdown is supported."
)
private = models.BooleanField(
default=False,
help_text="Check if your village is invite only. Leave unchecked to welcome strangers.",
)
deleted = models.BooleanField(default=False)
def __str__(self):
return "%s (%s)" % (self.name, self.camp.title)
def get_absolute_url(self):
return reverse_lazy(
"village_detail", kwargs={"camp_slug": self.camp.slug, "slug": self.slug}
)
def save(self, **kwargs):
if (
not self.pk
or not self.slug
or Village.objects.filter(slug=self.slug).count() > 1
):
slug = slugify(self.name)
if not slug:
slug = "noname"
incrementer = 1
# We have to make sure that the slug won't clash with current slugs
while Village.objects.filter(slug=slug).exists():
if incrementer == 1:
slug = "{}-1".format(slug)
else:
slug = "{}-{}".format("-".join(slug.split("-")[:-1]), incrementer)
incrementer += 1
self.slug = slug
super(Village, self).save(**kwargs)
def delete(self, using=None, keep_parents=False):
self.deleted = True
self.save()