# Path: symposion/schedule/models.py
# 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"]


from django import forms
from django.db.models import Q
from markitup.widgets import MarkItUpWidget
from symposion.schedule.models import Presentation




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

# Path: symposion/schedule/models.py
# 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/forms.py
from django import forms
from django.db.models import Q
from markitup.widgets import MarkItUpWidget
from symposion.schedule.models import Presentation





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)