Skip to content

Commit

Permalink
Add random registrar selection and save functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Tubo committed Dec 30, 2023
1 parent b5604d3 commit 2ae9d75
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
2 changes: 2 additions & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
path("interests/", extra_duties_views.interests, name="extra_interests"),
path("interest/<int:interest_id>/", extra_duties_views.interest, name="extra_interest"),
path("editor/", extra_duties_views.edit_page, name="extra_edit_page"),
path("editor/random/", extra_duties_views.interested_random_registrar, name="extra_random_registrar"),
path("editor/save/<int:shift_id>/", extra_duties_views.save_registrar, name="extra_save_registrar"),
]

ical_urls = [
Expand Down
40 changes: 35 additions & 5 deletions radscheduler/core/views/extra_duties.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import random
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.db.models import F, OuterRef, Q, Subquery
from django.http import HttpResponse, JsonResponse
from django.shortcuts import redirect, render

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


Expand Down Expand Up @@ -75,11 +78,38 @@ def interest(request, interest_id):
def edit_page(request):
extra_shifts = (
Shift.objects.filter(extra_duty=True, registrar=None)
.order_by("date")
.order_by("-date")
.select_related("registrar__user")
.prefetch_related("interests", "interests__registrar__user")
)
).all()
end = extra_shifts.first().date if extra_shifts else date.today()
start = extra_shifts.last().date if extra_shifts else date.today()
registrars = active_registrars(start, end)

return render(
request, "extra_duties/edit_page.html", {"extra_shifts": extra_shifts, "holidays": canterbury_holidays}
request,
"extra_duties/edit_page.html",
{"extra_shifts": extra_shifts, "registrars": registrars, "holidays": canterbury_holidays},
)


@staff_member_required
def interested_random_registrar(request):
shift_id = request.GET.get("id")
interests = Shift.objects.get(pk=shift_id).interests.values("registrar").all()
if len(interests) == 0:
return JsonResponse({}, safe=True)
else:
return JsonResponse(random.choice(list(interests)), safe=True)


@staff_member_required
def save_registrar(request, shift_id):
if request.method == "POST":
form = ShiftChangeForm(request.POST)
if form.is_valid():
registrar = form.cleaned_data["registrar"]
shift = Shift.objects.get(pk=shift_id)
shift.registrar = registrar
shift.save()
return HttpResponse("Saved", status=200)
31 changes: 27 additions & 4 deletions radscheduler/templates/extra_duties/edit_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,24 @@
{% endfor %}
</td>
<td>
<div class="input-group">
<input type="text" class="form-control" placeholder="No one selected" />
<button class="btn btn-primary" type="button">Roll</button>
<button class="btn btn-warning" type="button">Save</button>
<div x-data="{shift_id: {{ shift.id }} }" class="input-group">
<select class="form-select"
id="registrar_selected"
name="registrar"
x-ref="select_element">
<option value=""></option>
{% for registrar in registrars %}
<option value="{{ registrar.pk }}" class="registrar_option">{{ registrar.user.username }}</option>
{% endfor %}
</select>
<button class="btn btn-primary"
type="button"
@click="$store.select_random(shift_id, $refs.select_element)">Roll</button>
<button class="btn btn-warning"
type="button"
hx-post="{% url 'extra_save_registrar' shift.pk %}"
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-include="previous #registrar_selected">Save</button>
</div>
</td>
</tr>
Expand All @@ -46,7 +60,16 @@
{% block inline_javascript %}
<script>
window.addEventListener('DOMContentLoaded', () => {
Alpine.store("select_random", select_random_registrar)
Alpine.start();
});

function select_random_registrar(shift_id, select_element) {
fetch("{% url 'extra_random_registrar' %}" + "?id=" + shift_id)
.then(response => response.json())
.then(data => {
select_element.value = data.registrar
})
}
</script>
{% endblock inline_javascript %}

0 comments on commit 2ae9d75

Please sign in to comment.