From 735f17b1f1dfb1ecb1d994d405c95448717c9fd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Fri, 4 May 2018 23:40:43 +0200 Subject: [PATCH] Add an FK to teams.Team on info.InfoCategory, version control it using django-reversion. --- src/bornhack/settings.py | 1 + src/info/migrations/0004_infocategory_team.py | 20 ++++++++++++++++++ src/info/models.py | 21 ++++++++++++++++++- src/requirements/production.txt | 1 + 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/info/migrations/0004_infocategory_team.py diff --git a/src/bornhack/settings.py b/src/bornhack/settings.py index 9283d8fe..3dfe0e57 100644 --- a/src/bornhack/settings.py +++ b/src/bornhack/settings.py @@ -51,6 +51,7 @@ INSTALLED_APPS = [ 'allauth.account', 'bootstrap3', 'django_extensions', + 'reversion', ] #MEDIA_URL = '/media/' diff --git a/src/info/migrations/0004_infocategory_team.py b/src/info/migrations/0004_infocategory_team.py new file mode 100644 index 00000000..d7d2acd2 --- /dev/null +++ b/src/info/migrations/0004_infocategory_team.py @@ -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'), + ), + ] diff --git a/src/info/models.py b/src/info/models.py index f507c683..f86c60b1 100644 --- a/src/info/models.py +++ b/src/info/models.py @@ -2,6 +2,8 @@ from django.db import models from utils.models import CampRelatedModel from django.core.exceptions import ValidationError +import reversion + class InfoCategory(CampRelatedModel): class Meta: @@ -29,15 +31,32 @@ class InfoCategory(CampRelatedModel): 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): 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'}) + 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): return '%s (%s)' % (self.headline, self.camp) +# We want to have info items under version control +@reversion.register() class InfoItem(CampRelatedModel): class Meta: ordering = ['weight', 'headline'] diff --git a/src/requirements/production.txt b/src/requirements/production.txt index eacca40c..390ce93a 100644 --- a/src/requirements/production.txt +++ b/src/requirements/production.txt @@ -15,6 +15,7 @@ django-bleach==0.3.0 django-bootstrap3==8.2.2 django-extensions==1.7.7 django-wkhtmltopdf==3.1.0 +django-reversion==2.0.13 docopt==0.6.2 future==0.16.0 html5lib==0.9999999