def edit_reservation(request, reservation_id): page_title = 'Edit Reservation' reservation = get_object_or_404(Reservation, pk=reservation_id) form = ReservationForm(request.POST or None, instance=reservation, user=request.user) queryset = Equipment.objects.filter(id__in=get_allowed_equipment(request.user)) if request.POST and form.is_valid(): form.instance.user = request.user form.instance.is_approved = False form.instance.is_conflicting = False reservation = form.save(commit=False) reservation.save() # Deletes the previous selections from the database EquipmentReservation.objects.filter(reservation=reservation).delete() # Add the new equipment to the database selected_equipment = form.cleaned_data.get('equipment') for equipment in selected_equipment: quantity = request.POST['quantity_' + str(equipment.id)] equipment_reservation = EquipmentReservation(equipment=equipment, reservation=reservation, quantity=quantity) equipment_reservation.save() conflicts = detect_conflicts(reservation) if conflicts.__len__() > 0: reservation.is_conflicting = True reservation.save() return HttpResponseRedirect("/checkout/reservations/add/" + str(reservation.id) + "/conflicts/") # Check if there are kits and prompt for more options kits = [] for equipment in selected_equipment: if equipment.is_kit: kits.append(equipment) if kits.__len__() > 0: reservation.save() return HttpResponseRedirect("/checkout/reservations/add/" + str(reservation.id) + "/options/") return HttpResponseRedirect('/checkout/reservations') return render_to_response("checkout/reservation_edit.html", {'form': form, 'reservation_tab': True, 'queryset': queryset, 'page_title': page_title}, context_instance=RequestContext(request))
def new_reservation(request): page_title = 'New Reservation' queryset = Equipment.objects.filter(id__in=get_allowed_equipment(request.user)) if request.POST: form = ReservationForm(request.POST, user=request.user) if form.is_valid(): form.instance.user = request.user form.instance.is_approved = False form.instance.is_conflicting = False reservation = form.save(commit=False) reservation.save() selected_equipment = form.cleaned_data.get('equipment') for equipment in selected_equipment: quantity = request.POST['quantity_' + str(equipment.id)] equipment_reservation = EquipmentReservation(equipment=equipment, reservation=reservation, quantity=quantity) equipment_reservation.save() # Check for conflicts and redirect to the conflicts page conflicts = detect_conflicts(reservation) if conflicts.__len__() > 0: reservation.is_conflicting = True reservation.save() return HttpResponseRedirect("/checkout/reservations/add/" + str(reservation.id) + "/conflicts/") # Check if there are kits and prompt for more options kits = [] for equipment in selected_equipment: if equipment.is_kit: kits.append(equipment) if kits.__len__() > 0: reservation.save() return HttpResponseRedirect("/checkout/reservations/add/" + str(reservation.id) + "/options/") return HttpResponseRedirect('/checkout/reservations') else: form = ReservationForm(user=request.user) return render_to_response("checkout/reservation_edit.html", {'form': form, 'reservation_tab': True, 'queryset': queryset, 'page_title': page_title}, context_instance=RequestContext(request))