bornhack-website/src/info/models.py

81 lines
2.6 KiB
Python
Raw Normal View History

from django.db import models
2018-03-04 13:35:27 +00:00
from utils.models import CampRelatedModel
from django.core.exceptions import ValidationError
2017-03-07 23:24:14 +00:00
class InfoCategory(CampRelatedModel):
class Meta:
2017-02-16 21:05:10 +00:00
ordering = ['weight', 'headline']
unique_together = (('anchor', 'camp'), ('headline', 'camp'))
2016-12-28 23:15:13 +00:00
verbose_name_plural = "Info Categories"
camp = models.ForeignKey(
'camps.Camp',
2018-03-04 14:56:11 +00:00
related_name='infocategories',
on_delete=models.PROTECT
)
headline = models.CharField(
2018-03-04 14:56:11 +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(
2018-03-04 14:56:11 +00:00
help_text='Determines sorting/ordering. Heavier categories sink to the bottom. Categories with the same weight are ordered alphabetically. Defaults to 100.',
default=100,
)
def clean(self):
2016-12-28 23:15:13 +00:00
if InfoItem.objects.filter(category__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({'anchor': 'Anchor is already in use on an info item for this camp'})
2016-12-28 23:15:13 +00:00
def __str__(self):
return '%s (%s)' % (self.headline, self.camp)
2017-03-07 23:24:14 +00:00
class InfoItem(CampRelatedModel):
class Meta:
2017-02-16 21:05:10 +00:00
ordering = ['weight', 'headline']
unique_together = (('anchor', 'category'), ('headline', 'category'))
category = models.ForeignKey(
'info.InfoCategory',
2018-03-04 14:56:11 +00:00
related_name='infoitems',
on_delete=models.PROTECT
)
headline = models.CharField(
2018-03-04 14:56:11 +00:00
max_length=100,
help_text="Headline of this info item."
)
anchor = models.SlugField(
2018-03-04 14:56:11 +00:00
help_text="The HTML anchor to use for this info item."
)
body = models.TextField(
2018-03-04 14:56:11 +00:00
help_text='Body of this info item. Markdown is supported.'
)
weight = models.PositiveIntegerField(
2018-03-04 14:56:11 +00:00
help_text='Determines sorting/ordering. Heavier items sink to the bottom. Items with the same weight are ordered alphabetically. Defaults to 100.',
default=100,
)
@property
def camp(self):
return self.category.camp
def clean(self):
2016-12-28 23:15:13 +00:00
if InfoCategory.objects.filter(camp=self.category.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)
raise ValidationError({'anchor': 'Anchor is already in use on an info category for this camp'})
def __str__(self):
return '%s (%s)' % (self.headline, self.category)