2
0
Fork 0

add "add helper" functionality to team view

This commit is contained in:
Andreas (@xAndy) Zimmermann 2022-04-21 02:44:15 +02:00
parent 4048c7d9ac
commit 171cf5bf1f
4 changed files with 67 additions and 5 deletions

View File

@ -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"))

View File

@ -9,4 +9,25 @@
<li><a href="{% url 'team:helper' reg.helper.pk %}">{{ reg.helper.name }}</a> <a href="tel:{{ reg.helper.phone }}">📞</a></li> <li><a href="{% url 'team:helper' reg.helper.pk %}">{{ reg.helper.name }}</a> <a href="tel:{{ reg.helper.phone }}">📞</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
<form action="" method="post">
{% csrf_token %}
{% for field in add_helper_form %}
<div class="field">
{% if field.widget_type == 'checkbox' %}
<div class="control">
<label class="checkbox" for="{{ field.id_for_label }}">{{ field }} {{ field.label }}</label>
</div>
{% else %}
<label class="label" for="{{ field.id_for_label }}">{{ field.label }}</label>
<div class="control">
{{ field }}
</div>
{% endif %}
{% if field.help_text %}
<p class="is-size-7 mt-1">{{ field.help_text }}</p>
{% endif %}
</div>
{% endfor %}
<button class="button is-link" type="submit">Hinzufuegen</button>
</form>
{% endblock %} {% endblock %}

View File

@ -8,6 +8,6 @@ urlpatterns = [
path("overview/", views.shift_overview, name="shift_overview"), path("overview/", views.shift_overview, name="shift_overview"),
path("shifts/", views.ShiftList.as_view(), name="shift_all"), path("shifts/", views.ShiftList.as_view(), name="shift_all"),
path("free_shifts/", views.FreeShiftList.as_view(), name="shift_free"), path("free_shifts/", views.FreeShiftList.as_view(), name="shift_free"),
path("shift/<int:pk>", views.ShiftDetail.as_view(), name="shift"), path("shift/<int:pk>", views.shift_detail, name="shift"),
path("helper/<int:pk>", views.HelperDetail.as_view(), name="helper"), path("helper/<int:pk>", views.HelperDetail.as_view(), name="helper"),
] ]

View File

@ -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.contrib.auth.decorators import login_required
from django.utils import timezone from django.utils import timezone
from django.db.models.fields import DateTimeField 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 .models import ShiftRegistration, Room, Shift, Helper
from django.views.generic import DetailView, ListView from django.views.generic import DetailView, ListView
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib import messages
from .forms import HelperShift
# Create your views here. # Create your views here.
def index(request): def index(request):
@ -38,9 +40,38 @@ def shift_overview(request):
return render(request, "shift_overview.html", context) return render(request, "shift_overview.html", context)
class ShiftDetail(LoginRequiredMixin, DetailView): def add_helper_shift(self):
template_name = "shift_detail.html" pass
model = Shift
@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): class HelperDetail(LoginRequiredMixin, DetailView):