def reserve(request): form = DeviceSelectForm(request.POST or None) if form.is_valid(): if json.loads(form.data["next-available-spot"]): device = Device.objects.get(name=form.data["device"]) # first check whether a gpu is already available on that given machine for gpu in device.gpus.all(): if gpu.reservations.count() is 0: reservation = Reservation(gpu=gpu, user=request.user) reservation.save() send_gpu_done_mail(request, gpu, reservation) return HttpResponseRedirect(reverse("index")) # if there is no gpu available right now reserve all on this device and mark them as special reservation for gpu in device.gpus.all(): reservation = Reservation( gpu=gpu, user=request.user, user_reserved_next_available_spot=True) reservation.save() send_reservation_mail_for(request, gpu) else: gpu = GPU.objects.get(uuid=form.data["gpu"]) reservation = Reservation(gpu=gpu, user=request.user) reservation.save() send_reservation_mail_for(request, gpu) return HttpResponseRedirect(reverse("index")) return {"form": form}
def gpu_done(request, gpu_id): gpu = get_object_or_404(GPU, pk=gpu_id) current_reservation = gpu.current_reservation() if current_reservation is None: raise Http404 if current_reservation.user != request.user: return HttpResponseForbidden() current_reservation.delete() # get the user of the reservation that is now current and send him an email current_reservation = gpu.current_reservation() if current_reservation is not None: # clear all reservations made for this user if he only reserved the next available spot on this device if current_reservation.user_reserved_next_available_spot: device = current_reservation.gpu.device reservations_to_delete = [] for gpu in device.gpus.all(): for reservation in gpu.reservations.all(): if reservation == current_reservation: continue elif reservation.user == current_reservation.user and reservation.user_reserved_next_available_spot: reservations_to_delete.append(reservation) for reservation in reservations_to_delete: reservation.delete() send_gpu_done_mail(request, gpu, current_reservation) return HttpResponseRedirect(reverse("index"))
def reserve(request): form = DeviceSelectForm(request.POST or None) if form.is_valid(): if json.loads(form.data["next-available-spot"]): device = Device.objects.get(name=form.data["device"]) # first check whether a gpu is already available on that given machine for gpu in device.gpus.all(): if gpu.reservations.count() is 0: reservation = Reservation(gpu=gpu, user=request.user) reservation.save() send_gpu_done_mail(request, gpu, reservation) return HttpResponseRedirect(reverse("index")) # if there is no gpu available right now reserve all on this device and mark them as special reservation for gpu in device.gpus.all(): reservation = Reservation(gpu=gpu, user=request.user, user_reserved_next_available_spot=True) reservation.save() send_reservation_mail_for(request, gpu) else: gpu = GPU.objects.get(uuid=form.data["gpu"]) reservation = Reservation(gpu=gpu, user=request.user) reservation.save() send_reservation_mail_for(request, gpu) return HttpResponseRedirect(reverse("index")) return {"form": form}