bornhack-website/program/models.py

111 lines
3.6 KiB
Python
Raw Normal View History

2016-07-13 17:13:47 +00:00
from __future__ import unicode_literals
from django.contrib.postgres.fields import DateTimeRangeField
2016-07-13 17:13:47 +00:00
from django.db import models
2016-08-07 13:49:30 +00:00
from django.utils.text import slugify
from django.conf import settings
2016-07-13 17:13:47 +00:00
from utils.models import CreatedUpdatedModel
from django.core.exceptions import ValidationError
from datetime import timedelta
2016-07-13 17:13:47 +00:00
class EventType(CreatedUpdatedModel):
2016-07-13 19:44:09 +00:00
""" Every event needs to have a type. """
2016-07-13 17:13:47 +00:00
name = models.CharField(max_length=100)
slug = models.SlugField()
color = models.CharField(max_length=50)
light_text = models.BooleanField(default=False)
2016-07-13 17:13:47 +00:00
def __unicode__(self):
2016-07-13 20:37:20 +00:00
return self.name
2016-07-13 17:13:47 +00:00
class Event(CreatedUpdatedModel):
""" Something that is on the program one or more times. """
2016-07-13 17:13:47 +00:00
title = models.CharField(max_length=255)
2016-08-07 13:49:30 +00:00
slug = models.SlugField(blank=True, max_length=255)
abstract = models.TextField()
2016-07-13 19:44:09 +00:00
event_type = models.ForeignKey(EventType)
camp = models.ForeignKey('camps.Camp', null=True, related_name="events")
2016-08-08 17:36:13 +00:00
class Meta:
ordering = ['title']
2016-07-13 19:44:09 +00:00
def __unicode__(self):
return '%s (%s)' % (self.title, self.camp.title)
2016-07-13 20:37:20 +00:00
2016-08-07 13:49:30 +00:00
def save(self, **kwargs):
if not self.slug:
self.slug = slugify(self.title)
2016-08-07 13:49:30 +00:00
super(Event, self).save(**kwargs)
2016-07-13 17:13:47 +00:00
class EventInstance(CreatedUpdatedModel):
""" An instance of an event """
event = models.ForeignKey('program.event', related_name='instances')
when = DateTimeRangeField()
class Meta:
ordering = ['when']
def __unicode__(self):
return '%s (%s)' % (self.event, self.when)
def __clean__(self):
errors = []
if self.when.lower > self.when.upper:
errors.append(ValidationError({'when', "Start should be earlier than finish"}))
if self.when.lower.time().minute != 0 and self.when.lower.time().minute != 30:
errors.append(ValidationError({'when', "Start time minute should be 0 or 30."}))
if self.when.upper.time().minute != 0 and self.when.upper.time().minute != 30:
errors.append(ValidationError({'when', "End time minute should be 0 or 30."}))
if errors:
raise ValidationError(errors)
@property
def schedule_date(self):
"""
Returns the schedule date of this eventinstance. Schedule date is determined by substracting
settings.SCHEDULE_MIDNIGHT_OFFSET_HOURS from the eventinstance start time. This means that if
an event is scheduled for 00:30 wednesday evening (technically thursday) then the date
after substracting 5 hours would be wednesdays date, not thursdays.
"""
return (self.when.lower-timedelta(hours=settings.SCHEDULE_MIDNIGHT_OFFSET_HOURS)).date()
@property
def timeslots(self):
"""
Find the number of timeslots this eventinstance takes up
"""
seconds = (self.when.upper-self.when.lower).seconds
minutes = seconds / 60
return minutes / settings.SCHEDULE_TIMESLOT_LENGTH_MINUTES
2016-07-13 17:13:47 +00:00
class Speaker(CreatedUpdatedModel):
""" A Person anchoring an event. """
2016-07-13 17:13:47 +00:00
name = models.CharField(max_length=150)
biography = models.TextField()
2016-07-13 19:44:09 +00:00
picture = models.ImageField(null=True, blank=True)
2016-08-08 17:36:13 +00:00
slug = models.SlugField(blank=True, max_length=255)
2016-07-13 17:13:47 +00:00
events = models.ManyToManyField(
Event,
related_name='speakers',
related_query_name='speaker',
blank=True,
2016-07-13 19:44:09 +00:00
)
2016-07-13 20:37:20 +00:00
2016-08-08 17:36:13 +00:00
class Meta:
ordering = ['name']
def __unicode__(self):
2016-07-13 20:37:20 +00:00
return self.name
2016-08-08 17:36:13 +00:00
def save(self, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
2016-08-08 18:15:04 +00:00
super(Speaker, self).save(**kwargs)
2016-08-08 17:36:13 +00:00