bornhack-website/src/info/models.py

94 lines
3.0 KiB
Python
Raw Normal View History

import reversion
from django.core.exceptions import ValidationError
from django.db import models
from utils.models import CampRelatedModel
2017-03-07 23:24:14 +00:00
class InfoCategory(CampRelatedModel):
class Meta:
2019-06-16 12:32:24 +00:00
ordering = ["weight", "headline"]
2016-12-28 23:15:13 +00:00
verbose_name_plural = "Info Categories"
headline = models.CharField(
2019-06-16 12:32:24 +00:00
max_length=100, help_text="The headline of this info category"
)
anchor = models.SlugField(
2018-03-04 14:56:11 +00:00
help_text="The HTML anchor to use for this info category."
)
weight = models.PositiveIntegerField(
2019-06-16 12:32:24 +00:00
help_text="Determines sorting/ordering. Heavier categories sink to the bottom. Categories with the same weight are ordered alphabetically. Defaults to 100.",
2018-03-04 14:56:11 +00:00
default=100,
)
team = models.ForeignKey(
2019-06-16 12:32:24 +00:00
"teams.Team",
help_text="The team responsible for this info category.",
on_delete=models.PROTECT,
2019-06-16 12:32:24 +00:00
related_name="info_categories",
)
def clean(self):
2019-06-16 12:32:24 +00:00
if InfoItem.objects.filter(
category__team__camp=self.camp, anchor=self.anchor
).exists():
# this anchor is already in use on an item, so it cannot be used (must be unique on the page)
raise ValidationError(
2019-06-16 12:32:24 +00:00
{"anchor": "Anchor is already in use on an info item for this camp"}
)
@property
def camp(self):
return self.team.camp
2019-06-16 12:32:24 +00:00
camp_filter = "team__camp"
2016-12-28 23:15:13 +00:00
def __str__(self):
2019-06-16 12:32:24 +00:00
return "%s (%s)" % (self.headline, self.camp)
2016-12-28 23:15:13 +00:00
# We want to have info items under version control
@reversion.register()
2017-03-07 23:24:14 +00:00
class InfoItem(CampRelatedModel):
class Meta:
2019-06-16 12:32:24 +00:00
ordering = ["weight", "headline"]
unique_together = (("anchor", "category"), ("headline", "category"))
category = models.ForeignKey(
2019-06-16 12:32:24 +00:00
"info.InfoCategory", related_name="infoitems", on_delete=models.PROTECT
)
2019-06-16 12:32:24 +00:00
headline = models.CharField(max_length=100, help_text="Headline of this info item.")
2019-06-16 12:32:24 +00:00
anchor = models.SlugField(help_text="The HTML anchor to use for this info item.")
2019-06-16 12:32:24 +00:00
body = models.TextField(help_text="Body of this info item. Markdown is supported.")
weight = models.PositiveIntegerField(
2019-06-16 12:32:24 +00:00
help_text="Determines sorting/ordering. Heavier items sink to the bottom. Items with the same weight are ordered alphabetically. Defaults to 100.",
2018-03-04 14:56:11 +00:00
default=100,
)
@property
def camp(self):
return self.category.camp
2019-06-16 12:32:24 +00:00
camp_filter = "category__team__camp"
def clean(self):
2019-06-16 12:32:24 +00:00
if (
hasattr(self, "category")
and InfoCategory.objects.filter(
team__camp=self.category.team.camp, anchor=self.anchor
).exists()
):
# this anchor is already in use on a category, so it cannot be used here (they must be unique on the entire page)
2019-06-16 12:32:24 +00:00
raise ValidationError(
{"anchor": "Anchor is already in use on an info category for this camp"}
)
def __str__(self):
2019-06-16 12:32:24 +00:00
return "%s (%s)" % (self.headline, self.category)