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, reverse from .models import LockerBank, LockerInfo from .forms import LockerInfoForm def lockerIndex(request): locker_banks = { bank: { unit: [LockerInfoForm(instance=locker) for locker in unit.lockers.all()] for unit in bank.units.all() } for bank in LockerBank.objects.all() } context = { "locker_banks": locker_banks, } 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, form.errors) return HttpResponseRedirect(reverse("rentals:index"))