def cart_total_count(self): # XXX: how to handle float? # XXX: count total items in cart or total unique items in cart? ret = Decimal('0') for uid, count, comment in extractitems(readcookie(self.request)): ret += count return ret
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 prepare(self): if not readcookie(self.request): raise Redirect(self.context.absolute_url()) checkout = self.form_context is CHECKOUT form_class = checkout and 'mode_edit' or 'mode_display' self.form = factory('#form', name='checkout', props={ 'action': self.form_action, 'class_add': form_class}) for fields_factory in self.provider_registry: fields_factory(self.context, self.request).extend(self.form) # checkout data input if checkout: self.form['checkout_back'] = factory('submit', props={ 'label': _('back', 'Back'), 'action': 'checkout_back', 'handler': None, 'next': self.checkout_back, 'skip': True}) self.form['next'] = factory('submit', props={ 'label': _('next', 'Next'), 'action': 'next', 'handler': None, 'next': self.checkout_summary}) # checkout confirmation else: self.form['confirm_back'] = factory('submit', props={ 'label': _('back', 'Back'), 'action': 'confirm_back', 'handler': None, 'next': self.confirm_back}) self.form['finish'] = factory('submit', props={ 'label': _('finish', 'Order now'), 'action': 'finish', 'handler': self.finish, 'next': self.checkout_done})
def available(self): available = self.stock.available shared_stock_data = ISharedStockData(self.context) related_uids = shared_stock_data.related_uids if available is not None: cart_items = extractitems(readcookie(self.request)) for item_uid in related_uids: for uid, count, comment in cart_items: if uid == item_uid: available -= float(count) return available
def prepare(self): if not readcookie(self.request): raise Redirect(self.context.absolute_url()) checkout = self.form_context is CHECKOUT form_class = checkout and "mode_edit" or "mode_display" self.form = factory("#form", name="checkout", props={"action": self.form_action, "class_add": form_class}) for fields_factory in self.provider_registry: fields_factory(self.context, self.request).extend(self.form) # checkout data input if checkout: self.form["checkout_back"] = factory( "submit", props={ "label": _("back", "Back"), "action": "checkout_back", "class_add": "standalone", "handler": None, "next": self.checkout_back, "skip": True, }, ) self.form["next"] = factory( "submit", props={ "label": _("next", "Next"), "action": "next", "class_add": "context", "handler": None, "next": self.checkout_summary, }, ) # checkout confirmation else: self.form["confirm_back"] = factory( "submit", props={ "label": _("back", "Back"), "action": "confirm_back", "handler": None, "next": self.confirm_back, }, ) self.form["finish"] = factory( "submit", props={ "class": "prevent_if_no_longer_available context", "label": _("finish", "Order now"), "action": "finish", "handler": self.finish, "next": self.checkout_done, }, )
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}
def items(self): return extractitems(readcookie(self.request))
def test_readcookie_has_cookie(self): from bda.plone.cart import readcookie self._set_cookie("uid-1:5,uid-2:100,uid-3:7") self.assertEquals(readcookie(self.request), "uid-1:5,uid-2:100,uid-3:7")
def test_readcookie_no_cookie(self): from bda.plone.cart import readcookie self.assertEquals(readcookie(self.request), "")
def checkout_url(self): cookie = readcookie(self.request) if not cookie: return return self.data_provider.checkout_url
def test_readcookie_has_cookie(self): from bda.plone.cart import readcookie self._set_cookie('uid-1:5,uid-2:100,uid-3:7') self.assertEquals( readcookie(self.request), 'uid-1:5,uid-2:100,uid-3:7')
def test_readcookie_no_cookie(self): from bda.plone.cart import readcookie self.assertEquals(readcookie(self.request), '')