Add an FK to teams.Team on info.InfoCategory, version control it using django-reversion.

This commit is contained in:
Víðir Valberg Guðmundsson 2018-05-04 23:40:43 +02:00
parent 12563c890d
commit 735f17b1f1
4 changed files with 42 additions and 1 deletions

View file

@ -51,6 +51,7 @@ INSTALLED_APPS = [
'allauth.account', 'allauth.account',
'bootstrap3', 'bootstrap3',
'django_extensions', 'django_extensions',
'reversion',
] ]
#MEDIA_URL = '/media/' #MEDIA_URL = '/media/'

View file

@ -0,0 +1,20 @@
# Generated by Django 2.0.4 on 2018-05-04 21:11
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('teams', '0042_auto_20180413_1933'),
('info', '0003_auto_20170218_1148'),
]
operations = [
migrations.AddField(
model_name='infocategory',
name='team',
field=models.ForeignKey(blank=True, help_text='The team responsible for this info category.', null=True, on_delete=django.db.models.deletion.PROTECT, to='teams.Team'),
),
]

View file

@ -2,6 +2,8 @@ from django.db import models
from utils.models import CampRelatedModel from utils.models import CampRelatedModel
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
import reversion
class InfoCategory(CampRelatedModel): class InfoCategory(CampRelatedModel):
class Meta: class Meta:
@ -29,15 +31,32 @@ class InfoCategory(CampRelatedModel):
default=100, default=100,
) )
team = models.ForeignKey(
'teams.Team',
null=True,
blank=True,
help_text='The team responsible for this info category.',
on_delete=models.PROTECT,
)
def clean(self): def clean(self):
if InfoItem.objects.filter(category__camp=self.camp, anchor=self.anchor).exists(): 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) # 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'}) raise ValidationError(
{'anchor': 'Anchor is already in use on an info item for this camp'}
)
if self.team.camp != self.camp:
raise ValidationError(
{'team': 'Team is a team from another camp.'}
)
def __str__(self): def __str__(self):
return '%s (%s)' % (self.headline, self.camp) return '%s (%s)' % (self.headline, self.camp)
# We want to have info items under version control
@reversion.register()
class InfoItem(CampRelatedModel): class InfoItem(CampRelatedModel):
class Meta: class Meta:
ordering = ['weight', 'headline'] ordering = ['weight', 'headline']

View file

@ -15,6 +15,7 @@ django-bleach==0.3.0
django-bootstrap3==8.2.2 django-bootstrap3==8.2.2
django-extensions==1.7.7 django-extensions==1.7.7
django-wkhtmltopdf==3.1.0 django-wkhtmltopdf==3.1.0
django-reversion==2.0.13
docopt==0.6.2 docopt==0.6.2
future==0.16.0 future==0.16.0
html5lib==0.9999999 html5lib==0.9999999