# Path: symposion/schedule/forms.py
# class SlotEditForm(forms.Form):
#     
#     def __init__(self, *args, **kwargs):
#         self.slot = kwargs.pop("slot")
#         super(SlotEditForm, self).__init__(*args, **kwargs)
#         if self.slot.kind.label in ["talk", "tutorial", "sponsor-tutorial", "plenary"]:
#             self.fields["presentation"] = self.build_presentation_field()
#         else:
#             self.fields["content_override"] = self.build_content_override_field()
#     
#     def build_presentation_field(self):
#         kwargs = {}
#         queryset = Presentation.objects.all()
#         queryset = queryset.exclude(cancelled=True)
#         queryset = queryset.order_by("proposal_base__pk")
#         if self.slot.content:
#             queryset = queryset.filter(Q(slot=None) | Q(pk=self.slot.content.pk))
#             kwargs["required"] = False
#             kwargs["initial"] = self.slot.content
#         else:
#             queryset = queryset.filter(slot=None)
#             kwargs["required"] = True
#         kwargs["queryset"] = queryset
#         return forms.ModelChoiceField(**kwargs)
#     
#     def build_content_override_field(self):
#         kwargs = {
#             "label": "Content",
#             "widget": MarkItUpWidget(),
#             "required": False,
#             "initial": self.slot.content_override,
#         }
#         return forms.CharField(**kwargs)
# 
# Path: symposion/schedule/models.py
# class Schedule(models.Model):
#     
#     section = models.OneToOneField(Section)
#     published = models.BooleanField(default=True)
#     
#     def __unicode__(self):
#         return "%s Schedule" % self.section
# 
# class Day(models.Model):
#     
#     schedule = models.ForeignKey(Schedule)
#     date = models.DateField()
#     
#     def __unicode__(self):
#         return "%s" % self.date
#     
#     class Meta:
#         unique_together = [("schedule", "date")]
# 
# class Slot(models.Model):
#     
#     day = models.ForeignKey(Day)
#     kind = models.ForeignKey(SlotKind)
#     start = models.TimeField()
#     end = models.TimeField()
#     content_override = MarkupField(blank=True)
#     
#     def assign(self, content):
#         """
#         Assign the given content to this slot and if a previous slot content
#         was given we need to unlink it to avoid integrity errors.
#         """
#         self.unassign()
#         content.slot = self
#         content.save()
#     
#     def unassign(self):
#         """
#         Unassign the associated content with this slot.
#         """
#         if self.content and self.content.slot_id:
#             self.content.slot = None
#             self.content.save()
#     
#     @property
#     def duration(self):
#         start_dt = datetime.strptime(self.start.isoformat(), "%H:%M:%S")
#         end_dt = datetime.strptime(self.end.isoformat(), "%H:%M:%S")
#         delta = end_dt - start_dt
#         return delta.seconds // 60
#     
#     @property
#     def content(self):
#         """
#         Return the content this slot represents.
#         @@@ hard-coded for presentation for now
#         """
#         try:
#             return self.content_ptr
#         except ObjectDoesNotExist:
#             return None
#     
#     @property
#     def rooms(self):
#         return Room.objects.filter(pk__in=self.slotroom_set.values("room"))
#     
#     @property
#     def start_date(self):
#         return datetime.combine(self.day.date, self.start)
#     
#     @property
#     def end_date(self):
#         return datetime.combine(self.day.date, self.end)
#     
#     def __unicode__(self):
#         return "%s %s (%s - %s)" % (self.day, self.kind, self.start, self.end)
#     
#     class Meta:
#         ordering = ["day", "start", "end"]
# 
# class Presentation(models.Model):
#     
#     slot = models.OneToOneField(Slot, null=True, blank=True, related_name="content_ptr")
#     title = models.CharField(max_length=100)
#     description = MarkupField()
#     abstract = MarkupField()
#     speaker = models.ForeignKey("speakers.Speaker", related_name="presentations")
#     additional_speakers = models.ManyToManyField("speakers.Speaker", related_name="copresentations", blank=True)
#     cancelled = models.BooleanField(default=False)
#     proposal_base = models.OneToOneField(ProposalBase, related_name="presentation")
#     section = models.ForeignKey(Section, related_name="presentations")
#     
#     @property
#     def number(self):
#         return self.proposal.number
#     
#     @property
#     def proposal(self):
#         if self.proposal_base_id is None:
#             return None
#         return ProposalBase.objects.get_subclass(pk=self.proposal_base_id)
#     
#     def speakers(self):
#         yield self.speaker
#         for speaker in self.additional_speakers.all():
#             if speaker.user:
#                 yield speaker
#     
#     def __unicode__(self):
#         return "#%s %s (%s)" % (self.number, self.title, self.speaker)
#     
#     class Meta:
#         ordering = ["slot"]


import json
import datetime
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader, Context
from django.contrib.sites.models import Site
from django.contrib.auth.decorators import login_required
from symposion.schedule.forms import SlotEditForm
from symposion.schedule.models import Schedule, Day, Slot, Presentation
from symposion.schedule.timetable import TimeTable





def fetch_schedule(slug):
    qs = Schedule.objects.all()
    
    if slug is None:
        if qs.count() > 1:
            raise Http404()
        schedule = next(iter(qs), None)
        if schedule is None:
            raise Http404()
    else:
        schedule = get_object_or_404(qs, section__slug=slug)
    
    return schedule


def schedule_conference(request):
    

# Path: symposion/schedule/forms.py
# class SlotEditForm(forms.Form):
#     
#     def __init__(self, *args, **kwargs):
#         self.slot = kwargs.pop("slot")
#         super(SlotEditForm, self).__init__(*args, **kwargs)
#         if self.slot.kind.label in ["talk", "tutorial", "sponsor-tutorial", "plenary"]:
#             self.fields["presentation"] = self.build_presentation_field()
#         else:
#             self.fields["content_override"] = self.build_content_override_field()
#     
#     def build_presentation_field(self):
#         kwargs = {}
#         queryset = Presentation.objects.all()
#         queryset = queryset.exclude(cancelled=True)
#         queryset = queryset.order_by("proposal_base__pk")
#         if self.slot.content:
#             queryset = queryset.filter(Q(slot=None) | Q(pk=self.slot.content.pk))
#             kwargs["required"] = False
#             kwargs["initial"] = self.slot.content
#         else:
#             queryset = queryset.filter(slot=None)
#             kwargs["required"] = True
#         kwargs["queryset"] = queryset
#         return forms.ModelChoiceField(**kwargs)
#     
#     def build_content_override_field(self):
#         kwargs = {
#             "label": "Content",
#             "widget": MarkItUpWidget(),
#             "required": False,
#             "initial": self.slot.content_override,
#         }
#         return forms.CharField(**kwargs)
# 
# Path: symposion/schedule/models.py
# class Schedule(models.Model):
#     
#     section = models.OneToOneField(Section)
#     published = models.BooleanField(default=True)
#     
#     def __unicode__(self):
#         return "%s Schedule" % self.section
# 
# class Day(models.Model):
#     
#     schedule = models.ForeignKey(Schedule)
#     date = models.DateField()
#     
#     def __unicode__(self):
#         return "%s" % self.date
#     
#     class Meta:
#         unique_together = [("schedule", "date")]
# 
# class Slot(models.Model):
#     
#     day = models.ForeignKey(Day)
#     kind = models.ForeignKey(SlotKind)
#     start = models.TimeField()
#     end = models.TimeField()
#     content_override = MarkupField(blank=True)
#     
#     def assign(self, content):
#         """
#         Assign the given content to this slot and if a previous slot content
#         was given we need to unlink it to avoid integrity errors.
#         """
#         self.unassign()
#         content.slot = self
#         content.save()
#     
#     def unassign(self):
#         """
#         Unassign the associated content with this slot.
#         """
#         if self.content and self.content.slot_id:
#             self.content.slot = None
#             self.content.save()
#     
#     @property
#     def duration(self):
#         start_dt = datetime.strptime(self.start.isoformat(), "%H:%M:%S")
#         end_dt = datetime.strptime(self.end.isoformat(), "%H:%M:%S")
#         delta = end_dt - start_dt
#         return delta.seconds // 60
#     
#     @property
#     def content(self):
#         """
#         Return the content this slot represents.
#         @@@ hard-coded for presentation for now
#         """
#         try:
#             return self.content_ptr
#         except ObjectDoesNotExist:
#             return None
#     
#     @property
#     def rooms(self):
#         return Room.objects.filter(pk__in=self.slotroom_set.values("room"))
#     
#     @property
#     def start_date(self):
#         return datetime.combine(self.day.date, self.start)
#     
#     @property
#     def end_date(self):
#         return datetime.combine(self.day.date, self.end)
#     
#     def __unicode__(self):
#         return "%s %s (%s - %s)" % (self.day, self.kind, self.start, self.end)
#     
#     class Meta:
#         ordering = ["day", "start", "end"]
# 
# class Presentation(models.Model):
#     
#     slot = models.OneToOneField(Slot, null=True, blank=True, related_name="content_ptr")
#     title = models.CharField(max_length=100)
#     description = MarkupField()
#     abstract = MarkupField()
#     speaker = models.ForeignKey("speakers.Speaker", related_name="presentations")
#     additional_speakers = models.ManyToManyField("speakers.Speaker", related_name="copresentations", blank=True)
#     cancelled = models.BooleanField(default=False)
#     proposal_base = models.OneToOneField(ProposalBase, related_name="presentation")
#     section = models.ForeignKey(Section, related_name="presentations")
#     
#     @property
#     def number(self):
#         return self.proposal.number
#     
#     @property
#     def proposal(self):
#         if self.proposal_base_id is None:
#             return None
#         return ProposalBase.objects.get_subclass(pk=self.proposal_base_id)
#     
#     def speakers(self):
#         yield self.speaker
#         for speaker in self.additional_speakers.all():
#             if speaker.user:
#                 yield speaker
#     
#     def __unicode__(self):
#         return "#%s %s (%s)" % (self.number, self.title, self.speaker)
#     
#     class Meta:
#         ordering = ["slot"]


# Path: symposion/schedule/views.py
import json
import datetime
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader, Context
from django.contrib.sites.models import Site
from django.contrib.auth.decorators import login_required
from symposion.schedule.forms import SlotEditForm
from symposion.schedule.models import Schedule, Day, Slot, Presentation
from symposion.schedule.timetable import TimeTable






def fetch_schedule(slug):
    qs = Schedule.objects.all()
    
    if slug is None:
        if qs.count() > 1:
            raise Http404()
        schedule = next(iter(qs), None)
        if schedule is None:
            raise Http404()
    else:
        schedule = get_object_or_404(qs, section__slug=slug)
    
    return schedule


def schedule_conference(request):
    

    schedules = Schedule.objects.filter(published=True)