added ability to remove content from slots
This commit is contained in:
parent
7e81853772
commit
f98a3f2f9a
3 changed files with 28 additions and 17 deletions
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue