diff --git a/shiftregister/team/forms.py b/shiftregister/team/forms.py new file mode 100644 index 0000000..93b5ad6 --- /dev/null +++ b/shiftregister/team/forms.py @@ -0,0 +1,10 @@ +from django import forms +from .models import ShiftRegistration, Helper + +# placeholder form for simple submit button use cases so we get csrf protection +class EmptyForm(forms.Form): + pass + + +class HelperShift(forms.Form): + helper = forms.ModelChoiceField(queryset=Helper.objects.order_by("name")) diff --git a/shiftregister/team/templates/shift_detail.html b/shiftregister/team/templates/shift_detail.html index 9b52721..6a7b061 100644 --- a/shiftregister/team/templates/shift_detail.html +++ b/shiftregister/team/templates/shift_detail.html @@ -9,4 +9,25 @@
  • {{ reg.helper.name }} 📞
  • {% endfor %} +
    + {% csrf_token %} +{% for field in add_helper_form %} +
    +{% if field.widget_type == 'checkbox' %} +
    + +
    +{% else %} + +
    + {{ field }} +
    +{% endif %} +{% if field.help_text %} +

    {{ field.help_text }}

    +{% endif %} +
    +{% endfor %} + +
    {% endblock %} diff --git a/shiftregister/team/urls.py b/shiftregister/team/urls.py index dc1e76a..6c9df3d 100644 --- a/shiftregister/team/urls.py +++ b/shiftregister/team/urls.py @@ -8,6 +8,6 @@ urlpatterns = [ path("overview/", views.shift_overview, name="shift_overview"), path("shifts/", views.ShiftList.as_view(), name="shift_all"), path("free_shifts/", views.FreeShiftList.as_view(), name="shift_free"), - path("shift/", views.ShiftDetail.as_view(), name="shift"), + path("shift/", views.shift_detail, name="shift"), path("helper/", views.HelperDetail.as_view(), name="helper"), ] diff --git a/shiftregister/team/views.py b/shiftregister/team/views.py index b7976a1..be71835 100644 --- a/shiftregister/team/views.py +++ b/shiftregister/team/views.py @@ -1,4 +1,4 @@ -from django.shortcuts import render, get_object_or_404 +from django.shortcuts import render, get_object_or_404, redirect from django.contrib.auth.decorators import login_required from django.utils import timezone from django.db.models.fields import DateTimeField @@ -6,6 +6,8 @@ from django.db.models import F, Count, Q, ExpressionWrapper from .models import ShiftRegistration, Room, Shift, Helper from django.views.generic import DetailView, ListView from django.contrib.auth.mixins import LoginRequiredMixin +from django.contrib import messages +from .forms import HelperShift # Create your views here. def index(request): @@ -38,9 +40,38 @@ def shift_overview(request): return render(request, "shift_overview.html", context) -class ShiftDetail(LoginRequiredMixin, DetailView): - template_name = "shift_detail.html" - model = Shift +def add_helper_shift(self): + pass + + +@login_required() +def shift_detail(request, pk): + shift = get_object_or_404(Shift, pk=pk) + form = HelperShift() + if request.method == "POST": + form = HelperShift(request.POST) + if form.is_valid(): + (reg, created) = ShiftRegistration.objects.get_or_create( + helper=form.cleaned_data["helper"], shift=shift + ) + if created: + messages.add_message( + request, + messages.SUCCESS, + "Helfer erfolgreich zur Schicht hinzugefügt", + ) + else: + messages.add_message( + request, + messages.WARNING, + "Helfer ist bereits für diese Schicht angemeldet", + ) + return redirect("team:shift", pk=shift.pk) + context = { + "shift": shift, + "add_helper_form": form, + } + return render(request, "shift_detail.html", context) class HelperDetail(LoginRequiredMixin, DetailView):