black formatting
This commit is contained in:
parent
d041914c0f
commit
ff481a41c1
|
@ -4,16 +4,21 @@ from .models import Room, Shift, Helper, ShiftRegistration, Message, LoginToken
|
||||||
|
|
||||||
admin.site.register(Room)
|
admin.site.register(Room)
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Shift)
|
@admin.register(Shift)
|
||||||
class ShiftAdmin(admin.ModelAdmin):
|
class ShiftAdmin(admin.ModelAdmin):
|
||||||
list_display = ('room_name', 'start_at', 'free_slots')
|
list_display = ("room_name", "start_at", "free_slots")
|
||||||
|
|
||||||
def room_name(self, object):
|
def room_name(self, object):
|
||||||
return object.room.name
|
return object.room.name
|
||||||
#def helpers(self, object):
|
|
||||||
|
# def helpers(self, object):
|
||||||
# object.helpers.count()
|
# object.helpers.count()
|
||||||
def free_slots(self, object):
|
def free_slots(self, object):
|
||||||
return object.room.required_helpers - object.shiftregistration_set.count()
|
return object.room.required_helpers - object.shiftregistration_set.count()
|
||||||
#admin.site.register(Shift)
|
|
||||||
|
|
||||||
|
# admin.site.register(Shift)
|
||||||
|
|
||||||
|
|
||||||
admin.site.register(Helper)
|
admin.site.register(Helper)
|
||||||
|
|
|
@ -2,5 +2,5 @@ from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
class AppConfig(AppConfig):
|
class AppConfig(AppConfig):
|
||||||
default_auto_field = 'django.db.models.BigAutoField'
|
default_auto_field = "django.db.models.BigAutoField"
|
||||||
name = 'shiftregister.app'
|
name = "shiftregister.app"
|
||||||
|
|
|
@ -1,11 +1,16 @@
|
||||||
from django import forms
|
from django import forms
|
||||||
from .models import Helper
|
from .models import Helper
|
||||||
|
|
||||||
|
|
||||||
class RegisterForm(forms.Form):
|
class RegisterForm(forms.Form):
|
||||||
name = forms.CharField(max_length=Helper.name.field.max_length, label="Name")
|
name = forms.CharField(max_length=Helper.name.field.max_length, label="Name")
|
||||||
# actually verify phone number, lol
|
# actually verify phone number, lol
|
||||||
phone = forms.CharField(max_length=Helper.phone.field.max_length, label='Handynummer für Benachrichtigungen')
|
phone = forms.CharField(
|
||||||
okf = forms.BooleanField(label='Ich möchte SMS erhalten')
|
max_length=Helper.phone.field.max_length,
|
||||||
|
label="Handynummer für Benachrichtigungen",
|
||||||
|
)
|
||||||
|
okf = forms.BooleanField(label="Ich möchte SMS erhalten")
|
||||||
|
|
||||||
|
|
||||||
# placeholder form for simple submit button use cases so we get csrf protection
|
# placeholder form for simple submit button use cases so we get csrf protection
|
||||||
class EmptyForm(forms.Form):
|
class EmptyForm(forms.Form):
|
||||||
|
|
|
@ -9,58 +9,121 @@ class Migration(migrations.Migration):
|
||||||
|
|
||||||
initial = True
|
initial = True
|
||||||
|
|
||||||
dependencies = [
|
dependencies = []
|
||||||
]
|
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='Helper',
|
name="Helper",
|
||||||
fields=[
|
fields=[
|
||||||
('phone', models.CharField(max_length=200, primary_key=True, serialize=False)),
|
(
|
||||||
('name', models.CharField(max_length=200)),
|
"phone",
|
||||||
('number_validated', models.BooleanField(default=False)),
|
models.CharField(max_length=200, primary_key=True, serialize=False),
|
||||||
|
),
|
||||||
|
("name", models.CharField(max_length=200)),
|
||||||
|
("number_validated", models.BooleanField(default=False)),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='Room',
|
name="Room",
|
||||||
fields=[
|
fields=[
|
||||||
('name', models.CharField(max_length=200, primary_key=True, serialize=False)),
|
(
|
||||||
('required_helpers', models.IntegerField()),
|
"name",
|
||||||
|
models.CharField(max_length=200, primary_key=True, serialize=False),
|
||||||
|
),
|
||||||
|
("required_helpers", models.IntegerField()),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='Shift',
|
name="Shift",
|
||||||
fields=[
|
fields=[
|
||||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
(
|
||||||
('start_at', models.DateTimeField()),
|
"id",
|
||||||
('duration', models.DurationField()),
|
models.BigAutoField(
|
||||||
('room', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, to='app.room')),
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("start_at", models.DateTimeField()),
|
||||||
|
("duration", models.DurationField()),
|
||||||
|
(
|
||||||
|
"room",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.RESTRICT, to="app.room"
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='Message',
|
name="Message",
|
||||||
fields=[
|
fields=[
|
||||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
(
|
||||||
('text', models.CharField(max_length=160)),
|
"id",
|
||||||
('to', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.helper')),
|
models.BigAutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("text", models.CharField(max_length=160)),
|
||||||
|
(
|
||||||
|
"to",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE, to="app.helper"
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='LoginToken',
|
name="LoginToken",
|
||||||
fields=[
|
fields=[
|
||||||
('id', models.CharField(default=shiftregister.app.models.gen_token, editable=False, max_length=20, primary_key=True, serialize=False)),
|
(
|
||||||
('helper', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.helper')),
|
"id",
|
||||||
|
models.CharField(
|
||||||
|
default=shiftregister.app.models.gen_token,
|
||||||
|
editable=False,
|
||||||
|
max_length=20,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"helper",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE, to="app.helper"
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
migrations.CreateModel(
|
migrations.CreateModel(
|
||||||
name='ShiftRegistration',
|
name="ShiftRegistration",
|
||||||
fields=[
|
fields=[
|
||||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
(
|
||||||
('helper', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.helper')),
|
"id",
|
||||||
('shift', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, to='app.shift')),
|
models.BigAutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"helper",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.CASCADE, to="app.helper"
|
||||||
|
),
|
||||||
|
),
|
||||||
|
(
|
||||||
|
"shift",
|
||||||
|
models.ForeignKey(
|
||||||
|
on_delete=django.db.models.deletion.RESTRICT, to="app.shift"
|
||||||
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
options={
|
options={
|
||||||
'unique_together': {('shift', 'helper')},
|
"unique_together": {("shift", "helper")},
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
|
@ -4,12 +4,15 @@ from django.shortcuts import reverse
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
|
||||||
class Room(models.Model):
|
class Room(models.Model):
|
||||||
name = models.CharField(max_length=200, primary_key=True)
|
name = models.CharField(max_length=200, primary_key=True)
|
||||||
required_helpers = models.IntegerField()
|
required_helpers = models.IntegerField()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
class Shift(models.Model):
|
class Shift(models.Model):
|
||||||
room = models.ForeignKey(Room, on_delete=models.RESTRICT)
|
room = models.ForeignKey(Room, on_delete=models.RESTRICT)
|
||||||
start_at = models.DateTimeField()
|
start_at = models.DateTimeField()
|
||||||
|
@ -18,14 +21,17 @@ class Shift(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.room.name}: {self.start_at}"
|
return f"{self.room.name}: {self.start_at}"
|
||||||
|
|
||||||
|
|
||||||
class Helper(models.Model):
|
class Helper(models.Model):
|
||||||
# todo: add propper phone number validation, maybe even country code?
|
# todo: add propper phone number validation, maybe even country code?
|
||||||
phone = models.CharField(max_length=200, primary_key=True)
|
phone = models.CharField(max_length=200, primary_key=True)
|
||||||
name = models.CharField(max_length=200)
|
name = models.CharField(max_length=200)
|
||||||
# change this to a generic state variable to allow for number blocking/account deactivation?
|
# change this to a generic state variable to allow for number blocking/account deactivation?
|
||||||
number_validated = models.BooleanField(default=False)
|
number_validated = models.BooleanField(default=False)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def send_confirmation(self):
|
def send_confirmation(self):
|
||||||
(token, created) = LoginToken.objects.get_or_create(helper=self)
|
(token, created) = LoginToken.objects.get_or_create(helper=self)
|
||||||
text = f"Hallo {self.name}, hier ist dein Registrierungslink zum Kontaktfestival http://short.url{token.get_absolute_url()}"
|
text = f"Hallo {self.name}, hier ist dein Registrierungslink zum Kontaktfestival http://short.url{token.get_absolute_url()}"
|
||||||
|
@ -33,27 +39,37 @@ class Helper(models.Model):
|
||||||
msg.save()
|
msg.save()
|
||||||
return token
|
return token
|
||||||
|
|
||||||
|
|
||||||
class ShiftRegistration(models.Model):
|
class ShiftRegistration(models.Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
unique_together = (('shift', 'helper'),)
|
unique_together = (("shift", "helper"),)
|
||||||
|
|
||||||
# use restrict for now as Model.delete is not called
|
# use restrict for now as Model.delete is not called
|
||||||
shift = models.ForeignKey(Shift, on_delete=models.RESTRICT)
|
shift = models.ForeignKey(Shift, on_delete=models.RESTRICT)
|
||||||
helper = models.ForeignKey(Helper, on_delete=models.CASCADE)
|
helper = models.ForeignKey(Helper, on_delete=models.CASCADE)
|
||||||
|
|
||||||
def can_cancel(self):
|
def can_cancel(self):
|
||||||
return self.shift.start_at>(timezone.now()+timedelta(hours=4))
|
return self.shift.start_at > (timezone.now() + timedelta(hours=4))
|
||||||
|
|
||||||
|
|
||||||
class Message(models.Model):
|
class Message(models.Model):
|
||||||
# remove limit and send long messages in multiple messages?
|
# remove limit and send long messages in multiple messages?
|
||||||
text = models.CharField(max_length=160)
|
text = models.CharField(max_length=160)
|
||||||
to = models.ForeignKey(Helper, on_delete=models.CASCADE)
|
to = models.ForeignKey(Helper, on_delete=models.CASCADE)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.to.name}: {self.text}"
|
return f"{self.to.name}: {self.text}"
|
||||||
|
|
||||||
|
|
||||||
def gen_token():
|
def gen_token():
|
||||||
return secrets.token_urlsafe(20)
|
return secrets.token_urlsafe(20)
|
||||||
|
|
||||||
|
|
||||||
class LoginToken(models.Model):
|
class LoginToken(models.Model):
|
||||||
id = models.CharField(max_length=20, primary_key=True, default=gen_token, editable=False)
|
id = models.CharField(
|
||||||
|
max_length=20, primary_key=True, default=gen_token, editable=False
|
||||||
|
)
|
||||||
helper = models.ForeignKey(Helper, on_delete=models.CASCADE)
|
helper = models.ForeignKey(Helper, on_delete=models.CASCADE)
|
||||||
|
|
||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('token_login', kwargs={'token':self.id})
|
return reverse("token_login", kwargs={"token": self.id})
|
||||||
|
|
|
@ -3,10 +3,10 @@ from django.urls import path
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.index, name='index'),
|
path("", views.index, name="index"),
|
||||||
path('login/<slug:token>', views.login, name='token_login'),
|
path("login/<slug:token>", views.login, name="token_login"),
|
||||||
path('logout', views.logout, name='token_logout'),
|
path("logout", views.logout, name="token_logout"),
|
||||||
path('register', views.register, name='register'),
|
path("register", views.register, name="register"),
|
||||||
path('shift/<int:shiftid>', views.shift, name='shift'),
|
path("shift/<int:shiftid>", views.shift, name="shift"),
|
||||||
path('shift/<int:shiftid>/cancel', views.cancel, name='cancel'),
|
path("shift/<int:shiftid>/cancel", views.cancel, name="cancel"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -5,105 +5,139 @@ from .forms import RegisterForm, EmptyForm
|
||||||
from django.db.models.fields import DateTimeField
|
from django.db.models.fields import DateTimeField
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
if request.session.get('last_seen_shift'):
|
if request.session.get("last_seen_shift"):
|
||||||
del request.session['last_seen_shift']
|
del request.session["last_seen_shift"]
|
||||||
# dont show shifts starting in <60 minutes?
|
# dont show shifts starting in <60 minutes?
|
||||||
# currently only sorts by date
|
# currently only sorts by date
|
||||||
context = {}
|
context = {}
|
||||||
helper = None
|
helper = None
|
||||||
if request.session.get('token'):
|
if request.session.get("token"):
|
||||||
helper = LoginToken.objects.get(pk=request.session['token']).helper
|
helper = LoginToken.objects.get(pk=request.session["token"]).helper
|
||||||
context['helper'] = helper
|
context["helper"] = helper
|
||||||
context['my_shifts'] = helper.shiftregistration_set.filter(shift__start_at__gt=timezone.now()).order_by('shift__start_at')
|
context["my_shifts"] = helper.shiftregistration_set.filter(
|
||||||
context['current_shift'] = next(iter(helper.shiftregistration_set.annotate(shift_end=ExpressionWrapper(F('shift__start_at')+F('shift__duration'), output_field=DateTimeField())).filter(shift__start_at__lte=timezone.now(), shift_end__gte=timezone.now()).order_by('shift__start_at')[:1]), None)
|
shift__start_at__gt=timezone.now()
|
||||||
free_shifts = Shift.objects.annotate(reg_count=Count('shiftregistration')).filter(start_at__gt=timezone.now(),
|
).order_by("shift__start_at")
|
||||||
room__required_helpers__gt=F('reg_count')).order_by('start_at')
|
context["current_shift"] = next(
|
||||||
|
iter(
|
||||||
|
helper.shiftregistration_set.annotate(
|
||||||
|
shift_end=ExpressionWrapper(
|
||||||
|
F("shift__start_at") + F("shift__duration"),
|
||||||
|
output_field=DateTimeField(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.filter(
|
||||||
|
shift__start_at__lte=timezone.now(), shift_end__gte=timezone.now()
|
||||||
|
)
|
||||||
|
.order_by("shift__start_at")[:1]
|
||||||
|
),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
free_shifts = (
|
||||||
|
Shift.objects.annotate(reg_count=Count("shiftregistration"))
|
||||||
|
.filter(start_at__gt=timezone.now(), room__required_helpers__gt=F("reg_count"))
|
||||||
|
.order_by("start_at")
|
||||||
|
)
|
||||||
if helper:
|
if helper:
|
||||||
free_shifts = Shift.objects.annotate(reg_count=Count('shiftregistration')).filter(start_at__gt=timezone.now(),
|
free_shifts = (
|
||||||
room__required_helpers__gt=F('reg_count')).filter( ~Q(shiftregistration__helper=helper)).order_by('start_at')
|
Shift.objects.annotate(reg_count=Count("shiftregistration"))
|
||||||
context['free_shifts']= free_shifts
|
.filter(
|
||||||
return render(request, 'shiftlist.html', context)
|
start_at__gt=timezone.now(), room__required_helpers__gt=F("reg_count")
|
||||||
|
)
|
||||||
|
.filter(~Q(shiftregistration__helper=helper))
|
||||||
|
.order_by("start_at")
|
||||||
|
)
|
||||||
|
context["free_shifts"] = free_shifts
|
||||||
|
return render(request, "shiftlist.html", context)
|
||||||
|
|
||||||
|
|
||||||
def login(request, token):
|
def login(request, token):
|
||||||
tk = get_object_or_404(LoginToken,pk=token)
|
tk = get_object_or_404(LoginToken, pk=token)
|
||||||
if not tk.helper.number_validated:
|
if not tk.helper.number_validated:
|
||||||
tk.helper.number_validated=True
|
tk.helper.number_validated = True
|
||||||
tk.helper.save()
|
tk.helper.save()
|
||||||
request.session['token'] = token
|
request.session["token"] = token
|
||||||
# if the user was viewing a single shift before registering, they probably want to register for that
|
# if the user was viewing a single shift before registering, they probably want to register for that
|
||||||
# shift so we redirect them there.
|
# shift so we redirect them there.
|
||||||
if request.session.get('last_seen_shift'):
|
if request.session.get("last_seen_shift"):
|
||||||
return redirect('shift', shiftid=request.session['last_seen_shift'])
|
return redirect("shift", shiftid=request.session["last_seen_shift"])
|
||||||
return redirect('index')
|
return redirect("index")
|
||||||
|
|
||||||
|
|
||||||
def logout(request):
|
def logout(request):
|
||||||
if request.session.get('token'):
|
if request.session.get("token"):
|
||||||
del request.session['token']
|
del request.session["token"]
|
||||||
return redirect('index')
|
return redirect("index")
|
||||||
|
|
||||||
|
|
||||||
def register(request):
|
def register(request):
|
||||||
# handle number already being registered(resend message?)
|
# handle number already being registered(resend message?)
|
||||||
if request.session.get('token'):
|
if request.session.get("token"):
|
||||||
return redirect('index')
|
return redirect("index")
|
||||||
context = {}
|
context = {}
|
||||||
if request.method == 'POST':
|
if request.method == "POST":
|
||||||
form = RegisterForm(request.POST)
|
form = RegisterForm(request.POST)
|
||||||
if not form.is_valid():
|
if not form.is_valid():
|
||||||
context['form']=form
|
context["form"] = form
|
||||||
return render(request, 'register.html', context)
|
return render(request, "register.html", context)
|
||||||
helper = Helper(name = form.cleaned_data['name'], phone=form.cleaned_data['phone'])
|
helper = Helper(
|
||||||
|
name=form.cleaned_data["name"], phone=form.cleaned_data["phone"]
|
||||||
|
)
|
||||||
helper.save()
|
helper.save()
|
||||||
token = helper.send_confirmation()
|
token = helper.send_confirmation()
|
||||||
request.session['token'] = token.pk
|
request.session["token"] = token.pk
|
||||||
return render(request, 'wait_confirmation.html', {'helper':helper})
|
return render(request, "wait_confirmation.html", {"helper": helper})
|
||||||
context['form']=RegisterForm()
|
context["form"] = RegisterForm()
|
||||||
return render(request, 'register.html', context)
|
return render(request, "register.html", context)
|
||||||
|
|
||||||
|
|
||||||
def shift(request, shiftid):
|
def shift(request, shiftid):
|
||||||
shift = get_object_or_404(Shift, pk=shiftid)
|
shift = get_object_or_404(Shift, pk=shiftid)
|
||||||
helper = None
|
helper = None
|
||||||
context = { 'can_register':False,
|
context = {
|
||||||
'is_registered':False,
|
"can_register": False,
|
||||||
'can_cancel': False,
|
"is_registered": False,
|
||||||
'shift':shift,
|
"can_cancel": False,
|
||||||
'shift_form': EmptyForm,
|
"shift": shift,
|
||||||
}
|
"shift_form": EmptyForm,
|
||||||
|
}
|
||||||
# this currently ignores date/time
|
# this currently ignores date/time
|
||||||
request.session['last_seen_shift'] = shiftid
|
request.session["last_seen_shift"] = shiftid
|
||||||
if shift.room.required_helpers>shift.shiftregistration_set.count():
|
if shift.room.required_helpers > shift.shiftregistration_set.count():
|
||||||
context['can_register'] = True
|
context["can_register"] = True
|
||||||
if request.session.get('token'):
|
if request.session.get("token"):
|
||||||
helper = LoginToken.objects.get(pk=request.session['token']).helper
|
helper = LoginToken.objects.get(pk=request.session["token"]).helper
|
||||||
context['helper'] =helper
|
context["helper"] = helper
|
||||||
reg = ShiftRegistration.objects.filter(shift=shift, helper=helper)
|
reg = ShiftRegistration.objects.filter(shift=shift, helper=helper)
|
||||||
if reg:
|
if reg:
|
||||||
context['is_registered']=True
|
context["is_registered"] = True
|
||||||
context['can_register']=False
|
context["can_register"] = False
|
||||||
if reg[0].can_cancel():
|
if reg[0].can_cancel():
|
||||||
context['can_cancel'] = True
|
context["can_cancel"] = True
|
||||||
if request.method=='POST':
|
if request.method == "POST":
|
||||||
if EmptyForm(request.POST).is_valid():
|
if EmptyForm(request.POST).is_valid():
|
||||||
if not helper:
|
if not helper:
|
||||||
# todo: add flash message that signup is required?
|
# todo: add flash message that signup is required?
|
||||||
return redirect('register')
|
return redirect("register")
|
||||||
if context['can_register']:
|
if context["can_register"]:
|
||||||
s = ShiftRegistration(helper=helper, shift=shift)
|
s = ShiftRegistration(helper=helper, shift=shift)
|
||||||
s.save()
|
s.save()
|
||||||
# redirect so page can be reloaded without resending post data
|
# redirect so page can be reloaded without resending post data
|
||||||
return redirect('shift', shiftid=shift.pk)
|
return redirect("shift", shiftid=shift.pk)
|
||||||
return render(request, 'shift.html', context)
|
return render(request, "shift.html", context)
|
||||||
|
|
||||||
|
|
||||||
def cancel(request, shiftid):
|
def cancel(request, shiftid):
|
||||||
if request.method!='POST':
|
if request.method != "POST":
|
||||||
return redirect('shift', shiftid=shiftid)
|
return redirect("shift", shiftid=shiftid)
|
||||||
if not EmptyForm(request.POST).is_valid():
|
if not EmptyForm(request.POST).is_valid():
|
||||||
return redirect('shift', shiftid=shiftid)
|
return redirect("shift", shiftid=shiftid)
|
||||||
if not request.session.get('token'):
|
if not request.session.get("token"):
|
||||||
return redirect('shift', shiftid=shiftid)
|
return redirect("shift", shiftid=shiftid)
|
||||||
helper = LoginToken.objects.get(pk=request.session['token']).helper
|
helper = LoginToken.objects.get(pk=request.session["token"]).helper
|
||||||
shift = get_object_or_404(Shift, pk=shiftid)
|
shift = get_object_or_404(Shift, pk=shiftid)
|
||||||
reg = get_object_or_404(ShiftRegistration, helper=helper, shift=shift)
|
reg = get_object_or_404(ShiftRegistration, helper=helper, shift=shift)
|
||||||
if reg.can_cancel():
|
if reg.can_cancel():
|
||||||
reg.delete()
|
reg.delete()
|
||||||
return redirect('shift', shiftid=shiftid)
|
return redirect("shift", shiftid=shiftid)
|
||||||
|
|
|
@ -11,6 +11,6 @@ import os
|
||||||
|
|
||||||
from django.core.asgi import get_asgi_application
|
from django.core.asgi import get_asgi_application
|
||||||
|
|
||||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'shiftregister.settings')
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shiftregister.settings")
|
||||||
|
|
||||||
application = get_asgi_application()
|
application = get_asgi_application()
|
||||||
|
|
|
@ -21,69 +21,71 @@ BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
|
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
|
||||||
|
|
||||||
# SECURITY WARNING: keep the secret key used in production secret!
|
# SECURITY WARNING: keep the secret key used in production secret!
|
||||||
SECRET_KEY = getenv('SECRET_KEY', 'django-insecure-pdgzgd_!w&&cfqc%r&!v_^6pgf!sza=2wim67()!(kaf7_6-5)')
|
SECRET_KEY = getenv(
|
||||||
|
"SECRET_KEY", "django-insecure-pdgzgd_!w&&cfqc%r&!v_^6pgf!sza=2wim67()!(kaf7_6-5)"
|
||||||
|
)
|
||||||
|
|
||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = getenv('ENVIRONMENT', 'development') != 'production'
|
DEBUG = getenv("ENVIRONMENT", "development") != "production"
|
||||||
|
|
||||||
ALLOWED_HOSTS = list(filter(lambda s: s != '', getenv('ALLOWED_HOSTS', '').split(',')))
|
ALLOWED_HOSTS = list(filter(lambda s: s != "", getenv("ALLOWED_HOSTS", "").split(",")))
|
||||||
|
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
'shiftregister.app.apps.AppConfig',
|
"shiftregister.app.apps.AppConfig",
|
||||||
'shiftregister.team.apps.TeamConfig',
|
"shiftregister.team.apps.TeamConfig",
|
||||||
'django.contrib.admin',
|
"django.contrib.admin",
|
||||||
'django.contrib.auth',
|
"django.contrib.auth",
|
||||||
'django.contrib.contenttypes',
|
"django.contrib.contenttypes",
|
||||||
'django.contrib.sessions',
|
"django.contrib.sessions",
|
||||||
'django.contrib.messages',
|
"django.contrib.messages",
|
||||||
'django.contrib.staticfiles',
|
"django.contrib.staticfiles",
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
'django.middleware.security.SecurityMiddleware',
|
"django.middleware.security.SecurityMiddleware",
|
||||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||||
'django.middleware.common.CommonMiddleware',
|
"django.middleware.common.CommonMiddleware",
|
||||||
'django.middleware.csrf.CsrfViewMiddleware',
|
"django.middleware.csrf.CsrfViewMiddleware",
|
||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
"django.contrib.messages.middleware.MessageMiddleware",
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
]
|
]
|
||||||
|
|
||||||
ROOT_URLCONF = 'shiftregister.urls'
|
ROOT_URLCONF = "shiftregister.urls"
|
||||||
|
|
||||||
TEMPLATES = [
|
TEMPLATES = [
|
||||||
{
|
{
|
||||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||||||
'DIRS': [Path(BASE_DIR,Path("templates"))],
|
"DIRS": [Path(BASE_DIR, Path("templates"))],
|
||||||
'APP_DIRS': True,
|
"APP_DIRS": True,
|
||||||
'OPTIONS': {
|
"OPTIONS": {
|
||||||
'context_processors': [
|
"context_processors": [
|
||||||
'django.template.context_processors.debug',
|
"django.template.context_processors.debug",
|
||||||
'django.template.context_processors.request',
|
"django.template.context_processors.request",
|
||||||
'django.contrib.auth.context_processors.auth',
|
"django.contrib.auth.context_processors.auth",
|
||||||
'django.contrib.messages.context_processors.messages',
|
"django.contrib.messages.context_processors.messages",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
WSGI_APPLICATION = 'shiftregister.wsgi.application'
|
WSGI_APPLICATION = "shiftregister.wsgi.application"
|
||||||
|
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
|
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
|
||||||
|
|
||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
"default": {
|
||||||
'ENGINE': 'django.db.backends.'+getenv('DB_ENGINE', 'sqlite3'),
|
"ENGINE": "django.db.backends." + getenv("DB_ENGINE", "sqlite3"),
|
||||||
'NAME': getenv('DB_NAME', BASE_DIR / 'db.sqlite3'),
|
"NAME": getenv("DB_NAME", BASE_DIR / "db.sqlite3"),
|
||||||
'USER': getenv('DB_USER', ''),
|
"USER": getenv("DB_USER", ""),
|
||||||
'PASSWORD': getenv('DB_PASSWORD', ''),
|
"PASSWORD": getenv("DB_PASSWORD", ""),
|
||||||
'HOST': getenv('DB_HOST', ''),
|
"HOST": getenv("DB_HOST", ""),
|
||||||
'PORT': getenv('DB_PORT', ''),
|
"PORT": getenv("DB_PORT", ""),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,16 +95,16 @@ DATABASES = {
|
||||||
|
|
||||||
AUTH_PASSWORD_VALIDATORS = [
|
AUTH_PASSWORD_VALIDATORS = [
|
||||||
{
|
{
|
||||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -110,9 +112,9 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||||
# Internationalization
|
# Internationalization
|
||||||
# https://docs.djangoproject.com/en/4.0/topics/i18n/
|
# https://docs.djangoproject.com/en/4.0/topics/i18n/
|
||||||
|
|
||||||
LANGUAGE_CODE = 'de-de'
|
LANGUAGE_CODE = "de-de"
|
||||||
|
|
||||||
TIME_ZONE = 'Europe/Berlin'
|
TIME_ZONE = "Europe/Berlin"
|
||||||
|
|
||||||
USE_I18N = True
|
USE_I18N = True
|
||||||
|
|
||||||
|
@ -122,10 +124,10 @@ USE_TZ = True
|
||||||
# Static files (CSS, JavaScript, Images)
|
# Static files (CSS, JavaScript, Images)
|
||||||
# https://docs.djangoproject.com/en/4.0/howto/static-files/
|
# https://docs.djangoproject.com/en/4.0/howto/static-files/
|
||||||
|
|
||||||
STATIC_ROOT = '/opt/shiftregister/static'
|
STATIC_ROOT = "/opt/shiftregister/static"
|
||||||
STATIC_URL = 'static/'
|
STATIC_URL = "static/"
|
||||||
|
|
||||||
# Default primary key field type
|
# Default primary key field type
|
||||||
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
|
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
|
||||||
|
|
||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||||||
|
|
|
@ -2,5 +2,5 @@ from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
class TeamConfig(AppConfig):
|
class TeamConfig(AppConfig):
|
||||||
default_auto_field = 'django.db.models.BigAutoField'
|
default_auto_field = "django.db.models.BigAutoField"
|
||||||
name = 'shiftregister.team'
|
name = "shiftregister.team"
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from shiftregister.app.models import *
|
from shiftregister.app.models import *
|
||||||
|
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from . import views
|
from . import views
|
||||||
app_name = 'team'
|
|
||||||
|
app_name = "team"
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.shift_overview, name='index'),
|
path("", views.shift_overview, name="index"),
|
||||||
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('shift/<int:pk>', views.ShiftDetail.as_view(), name='shift'),
|
path("shift/<int:pk>", views.ShiftDetail.as_view(), name="shift"),
|
||||||
path('helper/<int:pk>', views.HelperDetail.as_view(), name='helper'),
|
path("helper/<int:pk>", views.HelperDetail.as_view(), name="helper"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -11,24 +11,47 @@ from django.contrib.auth.mixins import LoginRequiredMixin
|
||||||
def index(request):
|
def index(request):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def shift_overview(request):
|
def shift_overview(request):
|
||||||
context = {}
|
context = {}
|
||||||
context['running_shifts'] = (reg.shift for reg in ShiftRegistration.objects.annotate(shift_end=ExpressionWrapper(F('shift__start_at')+F('shift__duration'), output_field=DateTimeField())).filter(shift__start_at__lte=timezone.now(), shift_end__gte=timezone.now()).order_by('shift__start_at'))
|
context["running_shifts"] = (
|
||||||
|
reg.shift
|
||||||
|
for reg in ShiftRegistration.objects.annotate(
|
||||||
|
shift_end=ExpressionWrapper(
|
||||||
|
F("shift__start_at") + F("shift__duration"),
|
||||||
|
output_field=DateTimeField(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.filter(shift__start_at__lte=timezone.now(), shift_end__gte=timezone.now())
|
||||||
|
.order_by("shift__start_at")
|
||||||
|
)
|
||||||
|
|
||||||
# probably can do some distinct/group by stuff but not sure how tih django queries
|
# probably can do some distinct/group by stuff but not sure how tih django queries
|
||||||
context['next_shifts'] = (next(iter(Shift.objects.filter(room=room, start_at__gt=timezone.now()).order_by('start_at'))) for room in Room.objects.all())
|
context["next_shifts"] = (
|
||||||
|
next(
|
||||||
return render(request, 'shift_overview.html', context)
|
iter(
|
||||||
|
Shift.objects.filter(room=room, start_at__gt=timezone.now()).order_by(
|
||||||
|
"start_at"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
for room in Room.objects.all()
|
||||||
|
)
|
||||||
|
|
||||||
|
return render(request, "shift_overview.html", context)
|
||||||
|
|
||||||
|
|
||||||
class ShiftDetail(LoginRequiredMixin, DetailView):
|
class ShiftDetail(LoginRequiredMixin, DetailView):
|
||||||
template_name = "shift_detail.html"
|
template_name = "shift_detail.html"
|
||||||
model= Shift
|
model = Shift
|
||||||
|
|
||||||
|
|
||||||
class HelperDetail(LoginRequiredMixin, DetailView):
|
class HelperDetail(LoginRequiredMixin, DetailView):
|
||||||
template_name = "helper_detail.html"
|
template_name = "helper_detail.html"
|
||||||
model= Helper
|
model = Helper
|
||||||
|
|
||||||
|
|
||||||
class ShiftList(LoginRequiredMixin, ListView):
|
class ShiftList(LoginRequiredMixin, ListView):
|
||||||
template_name = "shift_list.html"
|
template_name = "shift_list.html"
|
||||||
model= Shift
|
model = Shift
|
||||||
|
|
|
@ -17,7 +17,7 @@ from django.contrib import admin
|
||||||
from django.urls import include, path
|
from django.urls import include, path
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', include('shiftregister.app.urls')),
|
path("", include("shiftregister.app.urls")),
|
||||||
path('team/', include('shiftregister.team.urls')),
|
path("team/", include("shiftregister.team.urls")),
|
||||||
path('admin/', admin.site.urls),
|
path("admin/", admin.site.urls),
|
||||||
]
|
]
|
||||||
|
|
|
@ -11,6 +11,6 @@ import os
|
||||||
|
|
||||||
from django.core.wsgi import get_wsgi_application
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'shiftregister.settings')
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shiftregister.settings")
|
||||||
|
|
||||||
application = get_wsgi_application()
|
application = get_wsgi_application()
|
||||||
|
|
Loading…
Reference in New Issue