cmsmanage/rentals/views.py

47 lines
1.4 KiB
Python

from django.contrib.auth.decorators import login_required, permission_required
from django.contrib import messages
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from .models import LockerBank, LockerInfo
from .forms import LockerInfoForm
def lockerIndex(request):
locker_banks = {
bank: {
unit: [
LockerInfoForm(instance=locker, auto_id=locker.address + "_%s")
for locker in unit.lockers.all()
]
for unit in bank.units.all()
}
for bank in LockerBank.objects.all()
}
context = {
"locker_banks": locker_banks,
"form_media": list(list(locker_banks.values())[0].values())[0][0].media,
}
return render(request, "rentals/lockers.dj.html", context)
@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:
messages.add_message(request, messages.ERROR, str(form.errors))
return HttpResponseRedirect(reverse("rentals:index"))