2022-02-16 16:06:51 -05:00
|
|
|
from django.contrib.auth.decorators import login_required, permission_required
|
|
|
|
from django.contrib import messages
|
|
|
|
from django.http import HttpResponseRedirect
|
2022-12-13 13:30:37 -05:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.urls import reverse
|
2022-01-24 22:03:10 -05:00
|
|
|
|
2022-02-16 16:06:51 -05:00
|
|
|
from .models import LockerBank, LockerInfo
|
|
|
|
|
|
|
|
from .forms import LockerInfoForm
|
2022-01-24 22:03:10 -05:00
|
|
|
|
|
|
|
|
|
|
|
def lockerIndex(request):
|
2022-02-16 16:06:51 -05:00
|
|
|
locker_banks = {
|
|
|
|
bank: {
|
2022-02-17 00:06:59 -05:00
|
|
|
unit: [
|
|
|
|
LockerInfoForm(instance=locker, auto_id=locker.address + "_%s")
|
|
|
|
for locker in unit.lockers.all()
|
|
|
|
]
|
2022-02-16 16:06:51 -05:00
|
|
|
for unit in bank.units.all()
|
|
|
|
}
|
|
|
|
for bank in LockerBank.objects.all()
|
|
|
|
}
|
|
|
|
|
2022-01-24 22:03:10 -05:00
|
|
|
context = {
|
2022-02-16 16:06:51 -05:00
|
|
|
"locker_banks": locker_banks,
|
2022-05-05 17:21:48 -04:00
|
|
|
"form_media": list(list(locker_banks.values())[0].values())[0][0].media,
|
2022-01-24 22:03:10 -05:00
|
|
|
}
|
|
|
|
return render(request, "rentals/lockers.dj.html", context)
|
2022-02-16 16:06:51 -05:00
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
@permission_required("rentals.change_lockerinfo", raise_exception=True)
|
|
|
|
def lockerUpdate(request, locker_id: int):
|
|
|
|
if request.method == "POST":
|
|
|
|
try:
|
|
|
|
instance = LockerInfo.objects.get(pk=locker_id)
|
|
|
|
except LockerInfo.DoesNotExist:
|
|
|
|
pass # TODO
|
|
|
|
|
|
|
|
form = LockerInfoForm(request.POST, instance=instance)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
else:
|
2022-12-13 13:30:37 -05:00
|
|
|
messages.add_message(request, messages.ERROR, str(form.errors))
|
2022-02-16 16:06:51 -05:00
|
|
|
|
|
|
|
return HttpResponseRedirect(reverse("rentals:index"))
|