def aggregated_count(self): items = extractitems(readcookie(self.request)) stock_data = ISharedStockData(self.context) aggregated_count = 0 for uid in stock_data.related_uids: aggregated_count += aggregate_cart_item_count(uid, items) return aggregated_count
def test_aggregate_cart_item_count_matching_uid(self): from bda.plone.cart import aggregate_cart_item_count items = [ ("uid-1", 5, "no comment"), ("uid-2", 100, "no comment"), ("uid-1", 7, "no comment"), ] self.assertEquals(aggregate_cart_item_count("uid-1", items), 12)
def test_aggregate_cart_item_count_matching_uid(self): from bda.plone.cart import aggregate_cart_item_count items = [ ('uid-1', 5, 'no comment', ), ('uid-2', 100, 'no comment', ), ('uid-1', 7, 'no comment', ) ] self.assertEquals(aggregate_cart_item_count('uid-1', items), 12)
def validate_count(self, uid, count): """Validate setting cart item count for uid. uid - Is the cart item UID. count - If count is 0, it means that a cart item is going to be deleted, which is always allowed. If count is > 0, it's the aggregated item count in cart. """ count = float(count) # count is 0, return if not count: return {'success': True, 'error': ''} cart_item = get_object_by_uid(self.context, uid) item_data = get_item_data_provider(cart_item) buyable_event = self.acquire_event(cart_item) buyable_event_data = IBuyableEventData(buyable_event) # cart count limit is set for all event tickets if buyable_event_data.cart_count_limit: related_uids = IEventTickets(cart_item).related_uids aggregated_count = count items = extractitems(readcookie(self.request)) for ticket_uid in related_uids: # we already have count for item to validate if uid == ticket_uid: continue aggregated_count += float( aggregate_cart_item_count(ticket_uid, items)) if aggregated_count > buyable_event_data.cart_count_limit: message = translate( _('event_tickets_limit_reached', default="Limit of tickets for this event reached"), context=self.request) return {'success': False, 'error': message} # cart count limit is set for ticket elif item_data.cart_count_limit: if count > item_data.cart_count_limit: message = translate( _('ticket_limit_reached', default="Limit for this ticket reached"), context=self.request) return {'success': False, 'error': message} # stock check item_state = get_item_state(cart_item, self.request) if item_state.validate_count(count): return {'success': True, 'error': ''} # out of stock message = translate(_('trying_to_add_more_tickets_than_available', default="Not enough tickets available, abort."), context=self.request) return {'success': False, 'error': message}
def validate_count(self, uid, count): """Validate setting cart item count for uid. uid - Is the cart item UID. count - If count is 0, it means that a cart item is going to be deleted, which is always allowed. If count is > 0, it's the aggregated item count in cart. """ count = float(count) # count is 0, return if not count: return {'success': True, 'error': ''} cart_item = get_object_by_uid(self.context, uid) item_data = get_item_data_provider(cart_item) buyable_event = self.acquire_event(cart_item) buyable_event_data = IBuyableEventData(buyable_event) # cart count limit is set for all event tickets if buyable_event_data.cart_count_limit: related_uids = IEventTickets(cart_item).related_uids aggregated_count = count items = extractitems(readcookie(self.request)) for ticket_uid in related_uids: # we already have count for item to validate if uid == ticket_uid: continue aggregated_count += float( aggregate_cart_item_count(ticket_uid, items)) if aggregated_count > buyable_event_data.cart_count_limit: message = translate(_( 'event_tickets_limit_reached', default="Limit of tickets for this event reached"), context=self.request) return {'success': False, 'error': message} # cart count limit is set for ticket elif item_data.cart_count_limit: if count > item_data.cart_count_limit: message = translate(_('ticket_limit_reached', default="Limit for this ticket reached"), context=self.request) return {'success': False, 'error': message} # stock check item_state = get_item_state(cart_item, self.request) if item_state.validate_count(count): return {'success': True, 'error': ''} # out of stock message = translate(_('trying_to_add_more_tickets_than_available', default="Not enough tickets available, abort."), context=self.request) return {'success': False, 'error': message}