cmsmanage/rentals/views.py

43 lines
1.3 KiB
Python
Raw Permalink Normal View History

from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from .forms import LockerInfoForm
from .models import LockerBank, LockerInfo
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":
instance = get_object_or_404(LockerInfo, pk=locker_id)
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"))