update camp with read only field, fix #96

This commit is contained in:
Stephan Telling 2017-03-07 21:44:30 +01:00
parent 1c1bc2188f
commit d7435ffa96
6 changed files with 65 additions and 17 deletions

View file

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-03-07 20:04
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('camps', '0019_auto_20170131_1849'),
]
operations = [
migrations.AddField(
model_name='camp',
name='read_only',
field=models.BooleanField(default=False, help_text='Whether the camp is read only (i.e. in the past)'),
),
]

View file

@ -1,4 +1,3 @@
import datetime
from django.db import models from django.db import models
from utils.models import UUIDModel, CreatedUpdatedModel from utils.models import UUIDModel, CreatedUpdatedModel
from program.models import EventType, EventLocation from program.models import EventType, EventLocation
@ -47,6 +46,11 @@ class Camp(CreatedUpdatedModel, UUIDModel):
help_text='The camp teardown period.', help_text='The camp teardown period.',
) )
read_only = models.BooleanField(
help_text='Whether the camp is read only (i.e. in the past)',
default=False
)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('camp_detail', kwargs={'camp_slug': self.slug}) return reverse('camp_detail', kwargs={'camp_slug': self.slug})

View file

@ -1,11 +1,9 @@
from django.contrib import messages
from django.db import models from django.db import models
from utils.models import CreatedUpdatedModel from utils.models import CreatedUpdatedModel, CampRelatedModel
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
class InfoCategory(CreatedUpdatedModel): class InfoCategory(CreatedUpdatedModel, CampRelatedModel):
class Meta: class Meta:
ordering = ['weight', 'headline'] ordering = ['weight', 'headline']
unique_together = (('anchor', 'camp'), ('headline', 'camp')) unique_together = (('anchor', 'camp'), ('headline', 'camp'))
@ -40,7 +38,7 @@ class InfoCategory(CreatedUpdatedModel):
return '%s (%s)' % (self.headline, self.camp) return '%s (%s)' % (self.headline, self.camp)
class InfoItem(CreatedUpdatedModel): class InfoItem(CreatedUpdatedModel, CampRelatedModel):
class Meta: class Meta:
ordering = ['weight', 'headline'] ordering = ['weight', 'headline']
unique_together = (('anchor', 'category'), ('headline', 'category')) unique_together = (('anchor', 'category'), ('headline', 'category'))
@ -69,6 +67,10 @@ class InfoItem(CreatedUpdatedModel):
default = 100, default = 100,
) )
@property
def camp(self):
return self.category.camp
def clean(self): def clean(self):
if InfoCategory.objects.filter(camp=self.category.camp, anchor=self.anchor).exists(): 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) # this anchor is already in use on a category, so it cannot be used here (they must be unique on the entire page)

View file

@ -3,12 +3,12 @@ from django.db import models
from django.utils.text import slugify from django.utils.text import slugify
from django.conf import settings from django.conf import settings
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from utils.models import CreatedUpdatedModel from utils.models import CreatedUpdatedModel, CampRelatedModel
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from datetime import timedelta from datetime import timedelta
class EventLocation(CreatedUpdatedModel): class EventLocation(CreatedUpdatedModel, CampRelatedModel):
""" The places where stuff happens """ """ The places where stuff happens """
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
slug = models.SlugField() slug = models.SlugField()
@ -34,7 +34,7 @@ class EventType(CreatedUpdatedModel):
return self.name return self.name
class Event(CreatedUpdatedModel): class Event(CreatedUpdatedModel, CampRelatedModel):
""" Something that is on the program one or more times. """ """ Something that is on the program one or more times. """
title = models.CharField(max_length=255) title = models.CharField(max_length=255)
slug = models.SlugField(blank=True, max_length=255) slug = models.SlugField(blank=True, max_length=255)
@ -72,7 +72,7 @@ class Event(CreatedUpdatedModel):
return False return False
class EventInstance(CreatedUpdatedModel): class EventInstance(CreatedUpdatedModel, CampRelatedModel):
""" An instance of an event """ """ An instance of an event """
event = models.ForeignKey('program.event', related_name='instances') event = models.ForeignKey('program.event', related_name='instances')
when = DateTimeRangeField() when = DateTimeRangeField()
@ -93,6 +93,10 @@ class EventInstance(CreatedUpdatedModel):
if errors: if errors:
raise ValidationError(errors) raise ValidationError(errors)
@property
def camp(self):
return self.event.camp
@property @property
def schedule_date(self): def schedule_date(self):
""" """
@ -123,7 +127,7 @@ def get_speaker_picture_upload_path(instance, filename):
} }
class Speaker(CreatedUpdatedModel): class Speaker(CreatedUpdatedModel, CampRelatedModel):
""" A Person anchoring an event. """ """ A Person anchoring an event. """
name = models.CharField(max_length=150) name = models.CharField(max_length=150)
biography = models.TextField() biography = models.TextField()

View file

@ -1,5 +1,6 @@
import uuid import uuid
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.contrib import messages
from django.db import models from django.db import models
@ -46,3 +47,22 @@ class CreatedUpdatedModel(CleanedModel):
updated = models.DateTimeField(auto_now=True) updated = models.DateTimeField(auto_now=True)
class CampRelatedModel(CleanedModel):
class Meta:
abstract = True
def save(self, **kwargs):
if self.camp.read_only:
if hasattr(self, 'request'):
messages.error(self.request, 'Camp is in read only mode.')
raise ValidationError('This camp is in read only mode.')
super(CampRelatedModel, self).save(**kwargs)
def delete(self, **kwargs):
if self.camp.read_only:
if hasattr(self, 'request'):
messages.error(self.request, 'Camp is in read only mode.')
raise ValidationError('This camp is in read only mode.')
super(CampRelatedModel, self).save(**kwargs)

View file

@ -1,15 +1,13 @@
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from django.db import models from django.db import models
from django.utils.text import slugify from django.utils.text import slugify
from utils.models import CreatedUpdatedModel, UUIDModel from utils.models import CreatedUpdatedModel, UUIDModel, CampRelatedModel
from .managers import VillageQuerySet from .managers import VillageQuerySet
class Village(CreatedUpdatedModel, UUIDModel): class Village(CreatedUpdatedModel, UUIDModel, CampRelatedModel):
class Meta: class Meta:
ordering = ['name'] ordering = ['name']