From f98a3f2f9a461901f556f5cd0f946c278cc5f8ae Mon Sep 17 00:00:00 2001 From: Brian Rosner Date: Wed, 19 Sep 2012 19:15:32 -0600 Subject: [PATCH] added ability to remove content from slots --- symposion/schedule/forms.py | 15 ++++++++------- symposion/schedule/models.py | 10 ++++++++-- symposion/schedule/views.py | 20 ++++++++++++-------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/symposion/schedule/forms.py b/symposion/schedule/forms.py index 6b4bf100..c94da716 100644 --- a/symposion/schedule/forms.py +++ b/symposion/schedule/forms.py @@ -6,17 +6,18 @@ from symposion.schedule.models import Presentation class SlotEditForm(forms.Form): - presentation = forms.ModelChoiceField( - queryset=Presentation.objects.all(), - required=True, - ) + presentation = forms.ModelChoiceField(queryset=Presentation.objects.all()) def __init__(self, *args, **kwargs): - presentation = kwargs.get("initial", {}).get("presentation") + content = kwargs.pop("content", None) + if content: + kwargs.setdefault("initial", {})["presentation"] = content super(SlotEditForm, self).__init__(*args, **kwargs) queryset = self.fields["presentation"].queryset - if presentation: - queryset = queryset.filter(Q(slot=None) | Q(pk=presentation.pk)) + if content: + queryset = queryset.filter(Q(slot=None) | Q(pk=content.pk)) + self.fields["presentation"].required = False else: queryset = queryset.filter(slot=None) + self.fields["presentation"].required = True self.fields["presentation"].queryset = queryset diff --git a/symposion/schedule/models.py b/symposion/schedule/models.py index 9e9e9159..9aac2b30 100644 --- a/symposion/schedule/models.py +++ b/symposion/schedule/models.py @@ -52,11 +52,17 @@ class Slot(models.Model): 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() - content.slot = self - content.save() @property def content(self): diff --git a/symposion/schedule/views.py b/symposion/schedule/views.py index 3c4e0b42..b2e429dc 100644 --- a/symposion/schedule/views.py +++ b/symposion/schedule/views.py @@ -67,19 +67,23 @@ def schedule_slot_edit(request, slot_pk): slot = get_object_or_404(Slot, pk=slot_pk) + # slot content + try: + content = slot.content + except ObjectDoesNotExist: + content = None + if request.method == "POST": - form = SlotEditForm(request.POST) + form = SlotEditForm(request.POST, content=content) if form.is_valid(): presentation = form.cleaned_data["presentation"] - slot.assign(presentation) + if presentation is None: + slot.unassign() + else: + slot.assign(presentation) return redirect("schedule_edit_singleton") else: - initial = {} - try: - initial["presentation"] = slot.content - except ObjectDoesNotExist: - pass - form = SlotEditForm(initial=initial) + form = SlotEditForm(content=content) ctx = { "form": form, "slot": slot,