added ability to remove content from slots

This commit is contained in:
Brian Rosner 2012-09-19 19:15:32 -06:00
parent 7e81853772
commit f98a3f2f9a
3 changed files with 28 additions and 17 deletions

View file

@ -6,17 +6,18 @@ from symposion.schedule.models import Presentation
class SlotEditForm(forms.Form): class SlotEditForm(forms.Form):
presentation = forms.ModelChoiceField( presentation = forms.ModelChoiceField(queryset=Presentation.objects.all())
queryset=Presentation.objects.all(),
required=True,
)
def __init__(self, *args, **kwargs): 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) super(SlotEditForm, self).__init__(*args, **kwargs)
queryset = self.fields["presentation"].queryset queryset = self.fields["presentation"].queryset
if presentation: if content:
queryset = queryset.filter(Q(slot=None) | Q(pk=presentation.pk)) queryset = queryset.filter(Q(slot=None) | Q(pk=content.pk))
self.fields["presentation"].required = False
else: else:
queryset = queryset.filter(slot=None) queryset = queryset.filter(slot=None)
self.fields["presentation"].required = True
self.fields["presentation"].queryset = queryset self.fields["presentation"].queryset = queryset

View file

@ -52,11 +52,17 @@ class Slot(models.Model):
Assign the given content to this slot and if a previous slot 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. 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: if self.content and self.content.slot_id:
self.content.slot = None self.content.slot = None
self.content.save() self.content.save()
content.slot = self
content.save()
@property @property
def content(self): def content(self):

View file

@ -67,19 +67,23 @@ def schedule_slot_edit(request, slot_pk):
slot = get_object_or_404(Slot, pk=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": if request.method == "POST":
form = SlotEditForm(request.POST) form = SlotEditForm(request.POST, content=content)
if form.is_valid(): if form.is_valid():
presentation = form.cleaned_data["presentation"] presentation = form.cleaned_data["presentation"]
slot.assign(presentation) if presentation is None:
slot.unassign()
else:
slot.assign(presentation)
return redirect("schedule_edit_singleton") return redirect("schedule_edit_singleton")
else: else:
initial = {} form = SlotEditForm(content=content)
try:
initial["presentation"] = slot.content
except ObjectDoesNotExist:
pass
form = SlotEditForm(initial=initial)
ctx = { ctx = {
"form": form, "form": form,
"slot": slot, "slot": slot,