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 :
2017-02-16 21:05:10 +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 (
2018-03-04 14:56:11 +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 (
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 ,
2016-12-25 14:52:55 +00:00
)
2018-05-04 21:40:43 +00:00
team = models . ForeignKey (
' teams.Team ' ,
help_text = ' The team responsible for this info category. ' ,
on_delete = models . PROTECT ,
2018-05-04 22:19: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 ) :
2018-06-21 07:19:09 +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 (
{ ' anchor ' : ' Anchor is already in use on an info item for this camp ' }
)
2018-05-20 20:10:38 +00:00
@property
def camp ( self ) :
return self . team . camp
2018-06-21 07:19:09 +00:00
camp_filter = ' team__camp '
2016-12-28 23:15:13 +00:00
def __str__ ( self ) :
return ' %s ( %s ) ' % ( self . headline , self . camp )
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 :
2017-02-16 21:05:10 +00:00
ordering = [ ' weight ' , ' headline ' ]
2016-12-25 14:52:55 +00:00
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
2016-12-25 14:52:55 +00:00
)
headline = models . CharField (
2018-03-04 14:56:11 +00:00
max_length = 100 ,
help_text = " Headline of this info item. "
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 item. "
2016-12-25 14:52:55 +00:00
)
body = models . TextField (
2018-03-04 14:56:11 +00:00
help_text = ' Body of this info item. Markdown is supported. '
2016-12-25 14:52:55 +00:00
)
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 ,
2016-12-25 14:52:55 +00:00
)
2017-03-07 20:44:30 +00:00
@property
def camp ( self ) :
return self . category . camp
2018-06-21 07:19:09 +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 ) :
2018-06-21 07:19:09 +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)
raise ValidationError ( { ' anchor ' : ' Anchor is already in use on an info category for this camp ' } )
2017-01-20 15:18:10 +00:00
def __str__ ( self ) :
return ' %s ( %s ) ' % ( self . headline , self . category )