Improved schedule models and behavior

This allows for full slot creation now.
This commit is contained in:
Brian Rosner 2012-08-29 23:58:03 -06:00
parent 143dbbce28
commit 77dc781e0d
2 changed files with 59 additions and 33 deletions

View file

@ -1,6 +1,7 @@
from django.db import models
from symposion.conference.models import Section
from symposion.schedule.utils import InlineSet
class Schedule(models.Model):
@ -10,50 +11,41 @@ class Schedule(models.Model):
class Day(models.Model):
schedule = models.ForeignKey(Schedule)
date = models.DateField()
class Meta:
unique_together = [("schedule", "date")]
class Track(models.Model):
class Room(models.Model):
schedule = models.ForeignKey(Schedule)
name = models.CharField(max_length=65)
room = models.CharField(max_length=100)
order = models.PositiveIntegerField()
class SlotKind(models.Model):
"""
A slot kind represents what kind a slot is. For example, a slot can be a
break, lunch, or X-minute talk.
"""
schedule = models.ForeignKey(Schedule)
label = models.CharField(max_length=50)
class Slot(models.Model):
day = models.ForeignKey(Day)
track_set = models.TextField(db_column="tracks")
room_set = models.TextField(db_column="rooms")
kind = models.ForeignKey(SlotKind)
start = models.TimeField()
end = models.TimeField()
@property
def tracks(self):
attr = "_tracks"
def rooms(self):
attr = "_rooms"
if not hasattr(self, attr):
slot = self
class TrackSet(object):
def __init__(self, data, delimiter):
self.data = set(data.split(delimiter))
def __iter__(self):
return Track.objects.filter(pk__in=self.data)
def add(self, track, commit=True):
"""
Add given track to the set, but check if it can exist
before adding it.
"""
self.data.add(track.pk)
self._update_model(commit=commit)
def remove(self, track, commit=True):
self.data.remove(track.pk)
self._update_model(commit=commit)
def _update_model(self, commit=True):
slot.track_set += self.delimiter.join(self.data)
if commit:
slot.save(force_update=True)
setattr(self, attr, TrackSet(self.track_set, delimiter=" "))
setattr(self, attr, InlineSet(obj=self, field="room_set", delimiter=" "))
return getattr(self, attr)

View file

@ -0,0 +1,34 @@
class InlineSet(object):
def __init__(self, obj, field, delimiter):
self.obj = obj
self.field = field
self.data = set([x for x in getattr(obj, field).split(delimiter) if x])
self.delimiter = delimiter
def __iter__(self):
return iter(self.queryset())
def __len__(self):
return self.queryset().count()
def queryset(self):
return self.obj.__class__._default_manager.filter(pk__in=self.data)
def add(self, obj, commit=True):
"""
Add given room to the set, but check if it can exist
before adding it.
"""
self.data.add(obj.pk)
self._update_model(commit=commit)
def remove(self, obj, commit=True):
self.data.remove(obj.pk)
self._update_model(commit=commit)
def _update_model(self, commit=True):
value = self.delimiter.join([str(x) for x in self.data])
setattr(self.obj, self.field, value)
if commit:
self.obj.save(force_update=True)