def add_ticket(request, quantity=1, redirect_to='satchmo_cart'): formdata = request.POST.copy() details = [] form1 = SelectEventDateForm(request.POST) form1.fields['datetime'].queryset = EventDate.objects.all() if form1.is_valid(): datetime = form1.cleaned_data['datetime'] form2 = SelectPlaceForm(request.POST) form2.fields['seat'].queryset = datetime.event.hallscheme.seats.all() if form2.is_valid(): seat = form2.cleaned_data['seat'] ticket = Ticket.objects.get(seat=seat, datetime=datetime) cart = Cart.objects.from_request(request, create=True) satchmo_cart_details_query.send( cart, product=ticket.product, quantity=quantity, details=details, request=request, form=formdata ) try: added_item = cart.add_item(ticket.product, number_added=1, details=details) added_item.quantity = 1 added_item.save() except CartAddProhibited, cap: return _product_error(request, ticket.product, cap.message) # got to here with no error, now send a signal so that listeners can also operate on this form. satchmo_cart_add_complete.send(cart, cart=cart, cartitem=added_item, product=ticket.product, request=request, form=formdata) satchmo_cart_changed.send(cart, cart=cart, request=request) if request.is_ajax(): data = { 'id': ticket.product.id, 'slug': seat.slug, 'name': ticket.product.translated_name(), 'item_id': added_item.id, 'item_qty': str(round_decimal(quantity, 2)), 'item_price': str(added_item.line_total) or "0.00", 'cart_count': str(round_decimal(cart.numItems, 2)), 'cart_total': str(round_decimal(cart.total, 2)), # Legacy result, for now 'results': _("Success"), } return _json_response(data) else: return redirect(redirect_to) else: return _json_response({'errors': form2.errors, 'results': _("Error")}, True)
def save(self, cart, request): log.debug('saving') self.full_clean() cartplaces = config_value('SHOP', 'CART_PRECISION') roundfactor = config_value('SHOP', 'CART_ROUNDING') site = Site.objects.get_current() for name, value in self.cleaned_data.items(): opt, key = name.split('__') log.debug('%s=%s', opt, key) quantity = 0 product = None if opt == 'qty': try: quantity = round_decimal(value, places=cartplaces, roundfactor=roundfactor) except RoundedDecimalError: quantity = 0 if not key in self.slugs: log.debug('caught attempt to add product not in the form: %s', key) else: try: product = Product.objects.get(slug=key, site=site) except Product.DoesNotExist: log.debug( 'caught attempt to add an non-existent product, ignoring: %s', key) if product and quantity > Decimal('0'): log.debug('Adding %s=%s to cart from MultipleProductForm', key, value) details = [] formdata = request.POST satchmo_cart_details_query.send(cart, product=product, quantity=quantity, details=details, request=request, form=formdata) added_item = cart.add_item(product, number_added=quantity, details=details) satchmo_cart_add_complete.send(cart, cart=cart, cartitem=added_item, product=product, request=request, form=formdata)
def save(self, cart, request): log.debug('saving'); self.full_clean() cartplaces = config_value('SHOP', 'CART_PRECISION') roundfactor = config_value('SHOP', 'CART_ROUNDING') for name, value in self.cleaned_data.items(): opt, key = name.split('__') log.debug('%s=%s', opt, key) items = {} quantity = 0 product = None if opt=='qty': try: quantity = round_decimal(value, places=cartplaces, roundfactor=roundfactor) except RoundedDecimalError, P: quantity = 0 if not key in self.slugs: log.debug('caught attempt to add product not in the form: %s', key) else: try: product = Product.objects.get(slug=key) except Product.DoesNotExist: log.debug('caught attempt to add an non-existent product, ignoring: %s', key) if product and quantity > Decimal('0'): log.debug('Adding %s=%s to cart from MultipleProductForm', key, value) details = {} formdata = request.POST satchmo_cart_details_query.send( cart, product=product, quantity=quantity, details=details, request=request, form=formdata ) added_item = cart.add_item(product, number_added=quantity, details=details) satchmo_cart_add_complete.send(cart, cart=cart, cartitem=added_item, product=product, request=request, form=formdata)
def add(request, id=0, redirect_to='satchmo_cart'): """Add an item to the cart.""" log.debug('FORM: %s', request.POST) formdata = request.POST.copy() productslug = None cartplaces = config_value('SHOP', 'CART_PRECISION') roundfactor = config_value('SHOP', 'CART_ROUNDING') if 'productname' in formdata: productslug = formdata['productname'] try: product, details = product_from_post(productslug, formdata) if not (product and product.active): log.debug("product %s is not active" % productslug) return bad_or_missing(request, _("That product is not available at the moment.")) else: log.debug("product %s is active" % productslug) except (Product.DoesNotExist, MultiValueDictKeyError): log.debug("Could not find product: %s", productslug) return bad_or_missing(request, _('The product you have requested does not exist.')) # First we validate that the number isn't too big. if decimal_too_big(formdata['quantity']): return _product_error(request, product, _("Please enter a smaller number.")) # Then we validate that we can round it appropriately. try: quantity = round_decimal(formdata['quantity'], places=cartplaces, roundfactor=roundfactor) except RoundedDecimalError: return _product_error(request, product, _("Invalid quantity.")) if quantity <= Decimal('0'): return _product_error(request, product, _("Please enter a positive number.")) cart = Cart.objects.from_request(request, create=True) # send a signal so that listeners can update product details before we add it to the cart. satchmo_cart_details_query.send( cart, product=product, quantity=quantity, details=details, request=request, form=formdata ) try: added_item = cart.add_item(product, number_added=quantity, details=details) except CartAddProhibited as cap: return _product_error(request, product, cap.message) # got to here with no error, now send a signal so that listeners can also operate on this form. satchmo_cart_add_complete.send(cart, cart=cart, cartitem=added_item, product=product, request=request, form=formdata) satchmo_cart_changed.send(cart, cart=cart, request=request) if request.is_ajax(): data = { 'id': product.id, 'name': product.translated_name(), 'item_id': added_item.id, 'item_qty': str(round_decimal(quantity, 2)), 'item_price': six.text_type(moneyfmt(added_item.line_total)) or "0.00", 'cart_count': str(round_decimal(cart.numItems, 2)), 'cart_total': six.text_type(moneyfmt(cart.total)), # Legacy result, for now 'results': _("Success"), } log.debug('CART AJAX: %s', data) return _json_response(data) else: url = reverse(redirect_to) return HttpResponseRedirect(url)
satchmo_cart_details_query.send( cart, product=product, quantity=quantity, details=details, request=request, form=formdata ) try: added_item = cart.add_item(product, number_added=quantity, details=details) except CartAddProhibited, cap: return _product_error(request, product, cap.message) # got to here with no error, now send a signal so that listeners can also operate on this form. satchmo_cart_add_complete.send(cart, cart=cart, cartitem=added_item, product=product, request=request, form=formdata) satchmo_cart_changed.send(cart, cart=cart, request=request) if request.is_ajax(): data = { 'id': product.id, 'name': product.translated_name(), 'item_id': added_item.id, 'item_qty': str(round_decimal(quantity, 2)), 'item_price': unicode(moneyfmt(added_item.line_total)) or "0.00", 'cart_count': str(round_decimal(cart.numItems, 2)), 'cart_total': unicode(moneyfmt(cart.total)), # Legacy result, for now 'results': _("Success"), } log.debug('CART AJAX: %s', data)
def add_ajax(request, id=0, template="shop/json.html"): cartplaces = config_value('SHOP', 'CART_PRECISION') roundfactor = config_value('SHOP', 'CART_ROUNDING') data = {'errors': []} product = None formdata = request.POST.copy() if not formdata.has_key('productname'): data['errors'].append(('product', _('No product requested'))) else: productslug = formdata['productname'] log.debug('CART_AJAX: slug=%s', productslug) try: product, details = product_from_post(productslug, formdata) except Product.DoesNotExist: log.warn("Could not find product: %s", productslug) product = None if not product: data['errors'].append(('product', _('The product you have requested does not exist.'))) else: if not product.active: data['errors'].append(('product', _('That product is not available at the moment.'))) else: data['id'] = product.id data['name'] = product.translated_name() if not formdata.has_key('quantity'): quantity = Decimal('-1') else: quantity = formdata['quantity'] try: quantity = round_decimal(quantity, places=cartplaces, roundfactor=roundfactor) if quantity < Decimal('0'): data['errors'].append(('quantity', _('Choose a quantity.'))) except RoundedDecimalError: data['errors'].append(('quantity', _('Invalid quantity.'))) tempCart = Cart.objects.from_request(request, create=True) if not data['errors']: # send a signal so that listeners can update product details before we add it to the cart. satchmo_cart_details_query.send( tempCart, product=product, quantity=quantity, details=details, request=request, form=formdata ) try: added_item = tempCart.add_item(product, number_added=quantity) request.session['cart'] = tempCart.id data['results'] = _('Success') if added_item: # send a signal so that listeners can also operate on this form and item. satchmo_cart_add_complete.send( tempCart, cartitem=added_item, product=product, request=request, form=formdata ) except CartAddProhibited, cap: data['results'] = _('Error') data['errors'].append(('product', cap.message))