Skip to content

Commit

Permalink
add error handling for non-registrars
Browse files Browse the repository at this point in the history
  • Loading branch information
Tubo committed Dec 26, 2023
1 parent 326d3e2 commit 28d47c1
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions radscheduler/core/views/extra_duties.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
from datetime import date, timedelta

from django.contrib import messages
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import login_required
from django.db.models import OuterRef, Q, Subquery
from django.shortcuts import render
from django.shortcuts import redirect, render

from radscheduler.core.forms import ShiftInterestForm
from radscheduler.core.models import Shift, ShiftInterest, Status
from radscheduler.roster import ShiftType, StatusType, canterbury_holidays


@login_required
def page(request):
subquery = ShiftInterest.objects.filter(registrar=request.user.registrar, shift=OuterRef("pk"))
extra_shifts = (
Shift.objects.filter(extra_duty=True, date__gte=date.today() - timedelta(days=30))
.annotate(
interest_id=Subquery(subquery.values("pk")),
comment=Subquery(subquery.values("comment")),
if hasattr(request.user, "registrar"):
subquery = ShiftInterest.objects.filter(registrar=request.user.registrar, shift=OuterRef("pk"))
extra_shifts = (
Shift.objects.filter(extra_duty=True, date__gte=date.today() - timedelta(days=30))
.annotate(
interest_id=Subquery(subquery.values("pk")),
comment=Subquery(subquery.values("comment")),
)
.order_by("-date")
.select_related("registrar__user")
.prefetch_related("interests")
)
.order_by("-date")
.select_related("registrar__user")
.prefetch_related("interests")
)

return render(
request,
"extra_duties/page.html",
{"extra_shifts": extra_shifts, "holidays": canterbury_holidays},
)
return render(
request,
"extra_duties/page.html",
{"extra_shifts": extra_shifts, "holidays": canterbury_holidays},
)
else:
messages.error(request, "You are not a registrar.")
return redirect("home")


@login_required
def interests(request):
if request.method == "POST":
shift_id = request.POST.get("shift_id")
Expand All @@ -40,6 +49,7 @@ def interests(request):
)


@login_required
def interest(request, interest_id):
interest = ShiftInterest.objects.get(id=interest_id)
shift = interest.shift
Expand All @@ -61,6 +71,7 @@ def interest(request, interest_id):
return render(request, "extra_duties/row.html", {"shift": shift, "holidays": canterbury_holidays})


@staff_member_required
def edit_page(request):
extra_shifts = (
Shift.objects.filter(extra_duty=True, registrar=None).order_by("date").select_related("registrar__user")
Expand Down

0 comments on commit 28d47c1

Please sign in to comment.