2016-12-25 14:52:55 +00:00
from django . db import models
2018-03-04 13:35:27 +00:00
from utils . models import CampRelatedModel
2016-12-25 14:52:55 +00:00
from django . core . exceptions import ValidationError
2018-05-04 21:40:43 +00:00
import reversion
2016-12-25 14:52:55 +00:00
2017-03-07 23:24:14 +00:00
class InfoCategory ( CampRelatedModel ) :
2016-12-25 14:52:55 +00:00
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 "
2016-12-25 14:52:55 +00:00
headline = models . CharField (
2019-06-16 12:32:24 +00:00
max_length = 100 , help_text = " The headline of this info category "
2016-12-25 14:52:55 +00:00
)
anchor = models . SlugField (
2018-03-04 14:56:11 +00:00
help_text = " The HTML anchor to use for this info category. "
2016-12-25 14:52:55 +00:00
)
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 ,
2016-12-25 14:52:55 +00:00
)
2018-05-04 21:40:43 +00:00
team = models . ForeignKey (
2019-06-16 12:32:24 +00:00
" teams.Team " ,
help_text = " The team responsible for this info category. " ,
2018-05-04 21:40:43 +00:00
on_delete = models . PROTECT ,
2019-06-16 12:32:24 +00:00
related_name = " info_categories " ,
2018-05-04 21:40:43 +00:00
)
2016-12-25 14:52:55 +00:00
def clean ( self ) :
2019-06-16 12:32:24 +00:00
if InfoItem . objects . filter (
category__team__camp = self . camp , anchor = self . anchor
) . exists ( ) :
2016-12-25 14:52:55 +00:00
# this anchor is already in use on an item, so it cannot be used (must be unique on the page)
2018-05-04 21:40:43 +00:00
raise ValidationError (
2019-06-16 12:32:24 +00:00
{ " anchor " : " Anchor is already in use on an info item for this camp " }
2018-05-04 21:40:43 +00:00
)
2018-05-20 20:10:38 +00:00
@property
def camp ( self ) :
return self . team . camp
2019-06-16 12:32:24 +00:00
camp_filter = " team__camp "
2018-06-21 07:19:09 +00:00
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
2016-12-25 14:52:55 +00:00
2018-05-04 21:40:43 +00:00
# We want to have info items under version control
@reversion.register ( )
2017-03-07 23:24:14 +00:00
class InfoItem ( CampRelatedModel ) :
2016-12-25 14:52:55 +00:00
class Meta :
2019-06-16 12:32:24 +00:00
ordering = [ " weight " , " headline " ]
unique_together = ( ( " anchor " , " category " ) , ( " headline " , " category " ) )
2016-12-25 14:52:55 +00:00
category = models . ForeignKey (
2019-06-16 12:32:24 +00:00
" info.InfoCategory " , related_name = " infoitems " , on_delete = models . PROTECT
2016-12-25 14:52:55 +00:00
)
2019-06-16 12:32:24 +00:00
headline = models . CharField ( max_length = 100 , help_text = " Headline of this info item. " )
2016-12-25 14:52:55 +00:00
2019-06-16 12:32:24 +00:00
anchor = models . SlugField ( help_text = " The HTML anchor to use for this info item. " )
2016-12-25 14:52:55 +00:00
2019-06-16 12:32:24 +00:00
body = models . TextField ( help_text = " Body of this info item. Markdown is supported. " )
2016-12-25 14:52:55 +00:00
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 ,
2016-12-25 14:52:55 +00:00
)
2017-03-07 20:44:30 +00:00
@property
def camp ( self ) :
return self . category . camp
2019-06-16 12:32:24 +00:00
camp_filter = " category__team__camp "
2018-06-20 20:03:29 +00:00
2016-12-25 14:52:55 +00:00
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 ( )
) :
2016-12-25 14:52:55 +00:00
# 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 " }
)
2016-12-25 14:52:55 +00:00
2017-01-20 15:18:10 +00:00
def __str__ ( self ) :
2019-06-16 12:32:24 +00:00
return " %s ( %s ) " % ( self . headline , self . category )