add cancel functionality to shift reservations
This commit is contained in:
parent
1e85c26f34
commit
1a3d7fe114
|
@ -1,6 +1,8 @@
|
|||
from django.db import models
|
||||
import secrets
|
||||
from django.shortcuts import reverse
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
class Room(models.Model):
|
||||
name = models.CharField(max_length=200, primary_key=True)
|
||||
|
@ -37,6 +39,8 @@ class ShiftRegistration(models.Model):
|
|||
# use restrict for now as Model.delete is not called
|
||||
shift = models.ForeignKey(Shift, on_delete=models.RESTRICT)
|
||||
helper = models.ForeignKey(Helper, on_delete=models.CASCADE)
|
||||
def can_cancel(self):
|
||||
return self.shift.start_at>(timezone.now()+timedelta(hours=4))
|
||||
|
||||
class Message(models.Model):
|
||||
# remove limit and send long messages in multiple messages?
|
||||
|
|
|
@ -15,4 +15,15 @@ Dauer: {{shift.duration}}<br>
|
|||
<input type="submit" value="Anmelden">
|
||||
</form>
|
||||
{%endif%}
|
||||
{% if is_registered%}
|
||||
{% if can_cancel %}
|
||||
<form action="{%url 'cancel' shift.pk%}" method="post">
|
||||
{% csrf_token %}
|
||||
{{shift_form.as_p}}
|
||||
<input type="submit" value="Abmelden">
|
||||
</form>
|
||||
{%else%}
|
||||
Bitte wende dich an den Infopoint falls du es nicht zu deiner Schicht schaffst.
|
||||
{%endif%}
|
||||
{% endif %}
|
||||
{% endblock %}
|
|
@ -8,4 +8,5 @@ urlpatterns = [
|
|||
path('logout', views.logout, name='token_logout'),
|
||||
path('register', views.register, name='register'),
|
||||
path('shift/<int:shiftid>', views.shift, name='shift'),
|
||||
path('shift/<int:shiftid>/cancel', views.cancel, name='cancel'),
|
||||
]
|
||||
|
|
|
@ -61,6 +61,7 @@ def shift(request, shiftid):
|
|||
helper = None
|
||||
context = { 'can_register':False,
|
||||
'is_registered':False,
|
||||
'can_cancel': False,
|
||||
'shift':shift,
|
||||
'shift_form': EmptyForm,
|
||||
}
|
||||
|
@ -71,9 +72,12 @@ def shift(request, shiftid):
|
|||
if request.session.get('token'):
|
||||
helper = LoginToken.objects.get(pk=request.session['token']).helper
|
||||
context['helper'] =helper
|
||||
if ShiftRegistration.objects.filter(shift=shift, helper=helper).count()!=0:
|
||||
reg = ShiftRegistration.objects.filter(shift=shift, helper=helper)
|
||||
if reg:
|
||||
context['is_registered']=True
|
||||
context['can_register']=False
|
||||
if reg[0].can_cancel():
|
||||
context['can_cancel'] = True
|
||||
if request.method=='POST':
|
||||
if EmptyForm(request.POST).is_valid():
|
||||
if not helper:
|
||||
|
@ -84,4 +88,18 @@ def shift(request, shiftid):
|
|||
s.save()
|
||||
# redirect so page can be reloaded without resending post data
|
||||
return redirect('shift', shiftid=shift.pk)
|
||||
return render(request, 'shift.html', context)
|
||||
return render(request, 'shift.html', context)
|
||||
|
||||
def cancel(request, shiftid):
|
||||
if request.method!='POST':
|
||||
return redirect('shift', shiftid=shiftid)
|
||||
if not EmptyForm(request.POST).is_valid():
|
||||
return redirect('shift', shiftid=shiftid)
|
||||
if not request.session.get('token'):
|
||||
return redirect('shift', shiftid=shiftid)
|
||||
helper = LoginToken.objects.get(pk=request.session['token']).helper
|
||||
shift = get_object_or_404(Shift, pk=shiftid)
|
||||
reg = get_object_or_404(ShiftRegistration, helper=helper, shift=shift)
|
||||
if reg.can_cancel():
|
||||
reg.delete()
|
||||
return redirect('shift', shiftid=shiftid)
|
|
@ -106,7 +106,7 @@ AUTH_PASSWORD_VALIDATORS = [
|
|||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
TIME_ZONE = 'Europe/Berlin'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
|
|
Loading…
Reference in New Issue