protect schedule edit views

This commit is contained in:
James Tauber 2012-08-31 01:16:30 -04:00
parent 06ff53e73c
commit d6a59f2e4f

View file

@ -1,6 +1,8 @@
from django.http import Http404 from django.http import Http404
from django.shortcuts import render, get_object_or_404, redirect from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required
from symposion.schedule.forms import SlotEditForm from symposion.schedule.forms import SlotEditForm
from symposion.schedule.models import Schedule, Day, Slot from symposion.schedule.models import Schedule, Day, Slot
from symposion.schedule.timetable import TimeTable from symposion.schedule.timetable import TimeTable
@ -8,26 +10,35 @@ from symposion.schedule.timetable import TimeTable
def schedule_detail(request, slug=None): def schedule_detail(request, slug=None):
qs = Schedule.objects.all() qs = Schedule.objects.all()
if slug is None: if slug is None:
schedule = next(iter(qs), None) schedule = next(iter(qs), None)
if schedule is None: if schedule is None:
raise Http404() raise Http404()
else: else:
schedule = get_object_or_404(qs, slug=slug) schedule = get_object_or_404(qs, slug=slug)
ctx = { ctx = {
"schedule": schedule, "schedule": schedule,
} }
return render(request, "schedule/schedule_detail.html", ctx) return render(request, "schedule/schedule_detail.html", ctx)
@login_required
def schedule_edit(request, slug=None): def schedule_edit(request, slug=None):
if not request.user.is_staff:
raise Http404()
qs = Schedule.objects.all() qs = Schedule.objects.all()
if slug is None: if slug is None:
schedule = next(iter(qs), None) schedule = next(iter(qs), None)
if schedule is None: if schedule is None:
raise Http404() raise Http404()
else: else:
schedule = get_object_or_404(qs, slug=slug) schedule = get_object_or_404(qs, slug=slug)
days_qs = Day.objects.filter(schedule=schedule) days_qs = Day.objects.filter(schedule=schedule)
days = [TimeTable(day) for day in days_qs] days = [TimeTable(day) for day in days_qs]
form = SlotEditForm() form = SlotEditForm()
@ -39,7 +50,12 @@ def schedule_edit(request, slug=None):
return render(request, "schedule/schedule_edit.html", ctx) return render(request, "schedule/schedule_edit.html", ctx)
@login_required
def schedule_slot_edit(request, slot_pk): def schedule_slot_edit(request, slot_pk):
if not request.user.is_staff:
raise Http404()
slot = get_object_or_404(Slot, pk=slot_pk) slot = get_object_or_404(Slot, pk=slot_pk)
form = SlotEditForm(request.POST) form = SlotEditForm(request.POST)