Commits

Luke Plant committed 5ea9bd5

Cleaned up business logic API using explicit 'basket' and 'shelf'

Comments (0)

Files changed (2)

cciw/bookings/models.py

     def get_query_set(self):
         return super(BookingManager, self).get_query_set().select_related('camp', 'account')
 
-    def ready_to_book(self, year, shelved=False):
+    def basket(self, year):
+        return self._ready_to_book(year, False)
+
+    def shelf(self, year):
+        return self._ready_to_book(year, True)
+
+    def _ready_to_book(self, year, shelved):
         qs = self.get_query_set().filter(camp__year__exact=year, shelved=shelved)
         return qs.filter(state=BOOKING_INFO_COMPLETE) | qs.filter(state=BOOKING_APPROVED)
 
             # Complex - need to check the other places that are about to be booked.
             # (if there is one place left, and two campers for it, we can't say that
             # there are enough places)
-            same_camp_bookings = self.account.bookings.ready_to_book(self.camp.year).filter(camp=self.camp)
+            same_camp_bookings = self.account.bookings.basket(self.camp.year).filter(camp=self.camp)
             places_to_be_booked = same_camp_bookings.count()
             places_to_be_booked_male = same_camp_bookings.filter(sex=SEX_MALE).count()
             places_to_be_booked_female = same_camp_bookings.filter(sex=SEX_FEMALE).count()
         ordering = ['-created']
 
 
-def book_basket_now(account, year):
+def book_basket_now(bookings):
     try:
         lock = Lock(os.path.join(os.environ['HOME'], '.cciw_booking_lock'))
         lock.acquire()
-        bookings = list(account.bookings.ready_to_book(year))
+        bookings = list(bookings)
         now = datetime.now()
         for b in bookings:
             if len(b.get_booking_problems()) > 0:

cciw/bookings/views.py

         c = super(BookingListBookings, self).get_context_data(**kwargs)
         year = get_thisyear()
         bookings = self.request.booking_account.bookings
-        basket_bookings = list(bookings.ready_to_book(year))
-        shelf_bookings = list(bookings.ready_to_book(year, shelved=True))
+        basket_bookings = list(bookings.basket(year))
+        shelf_bookings = list(bookings.shelf(year))
         # Now apply business rules and other custom processing
         total = Decimal('0.00')
         all_bookable = True
 
         year = get_thisyear()
         bookings = request.booking_account.bookings
-        places = (bookings.ready_to_book(year, shelved=True) |
-                  bookings.ready_to_book(year, shelved=False))
+        # NB - use lists here, not querysets, so that both state_token and book_now
+        # functionality apply against same set of bookings.
+        basket_bookings = list(bookings.basket(year))
+        shelf_bookings = list(bookings.shelf(year))
+        places = basket_bookings + shelf_bookings
 
         def shelve(place):
             place.shelved = True
                 if m is not None:
                     try:
                         b_id = int(m.groups()[0])
-                        place = places.get(id=b_id)
+                        place = [p for p in places if p.id == b_id][0]
                         retval = action(place)
                         if retval is not None:
                             return retval
-                    except (ValueError, Booking.DoesNotExist):
+                    except (ValueError, # converting to string
+                            IndexError, # not in list
+                            ):
                         pass
 
         if 'book_now' in request.POST:
             state_token = request.POST.get('state_token', '')
-            if make_state_token(list(bookings.ready_to_book(year))) != state_token:
+            if make_state_token(basket_bookings) != state_token:
                 messages.error(request, "Places were not booked due to modifications made "
                                "to the details. Please check the details and try again.")
             else:
-                if book_basket_now(request.booking_account, year):
+                if book_basket_now(basket_bookings):
                     return HttpResponseRedirect(reverse('cciw.bookings.views.pay'))
                 else:
                     messages.error(request, "These places cannot be booked for the reasons "