Merge "Limit total number of active bookings per user"
[laas.git] / src / booking / quick_deployer.py
index f8dc9ff..640ded9 100644 (file)
@@ -92,6 +92,10 @@ class NoRemainingPublicNetwork(Exception):
     pass
 
 
+class BookingPermissionException(Exception):
+    pass
+
+
 def parse_host_field(host_field_contents):
     host_json = json.loads(host_field_contents)
     lab_dict = host_json['labs'][0]
@@ -119,7 +123,7 @@ def check_available_matching_host(lab, hostprofile):
     available_host_types = ResourceManager.getInstance().getAvailableHostTypes(lab)
     if hostprofile not in available_host_types:
         # TODO: handle deleting generic resource in this instance along with grb
-        raise HostNotAvailable("Could not book selected host due to changed availability. Try again later")
+        raise HostNotAvailable('Requested host type is not available. Please try again later. Host availability can be viewed in the "Hosts" tab to the left.')
 
     hostset = Host.objects.filter(lab=lab, profile=hostprofile).filter(booked=False).filter(working=True)
     if not hostset.exists():
@@ -262,6 +266,10 @@ def create_from_form(form, request):
     data['host_profile'] = host_profile
     check_invariants(request, **data)
 
+    # check booking privileges
+    if Booking.objects.filter(owner=request.user, end__gt=timezone.now()).count() >= 3 and not request.user.userprofile.booking_privledge:
+        raise BookingPermissionException("You do not have permission to have more than 3 bookings at a time.")
+
     check_available_matching_host(lab, host_profile)  # requires cleanup if failure after this point
 
     grbundle = generate_grb(request.user, lab, quick_booking_id)