def get_namespace(self, resource, context): abspath = resource.get_abspath() cart = ProductCart(context) # Base namespace namespace = STLForm.get_namespace(self, resource, context) # Choose payments payments = resource.get_resource('payments') total_price = cart.get_total_price(resource) view = Payments_ChoosePayment(total_price=total_price) namespace['choose_payment'] = view.GET(payments, context) # Alert MSG namespace['alert_msg'] = MSG( u"""To continue, you have to validate the terms of sales!""") # Progress bar namespace['progress'] = Shop_Progress(index=5).GET(resource, context) # Get delivery and bill address namespace for key in ['delivery_address', 'bill_address']: id = cart.addresses[key] if id is not None: namespace[key] = resource.get_user_address_namespace(id) else: namespace[key] = None # Get products informations namespace['cart'] = Cart_View(see_actions=False).GET(resource, context) # Get user delivery country addresses = resource.get_resource('addresses').handler delivery_address = cart.addresses['delivery_address'] record = addresses.get_record(delivery_address) country = addresses.get_record_value(record, 'country') return namespace
def action_add(self, resource, context, form): cart = ProductCart(context) product_name = cart.get_product_name(form['id']) quantity_in_cart = cart.get_product_quantity_in_cart(product_name) product = resource.get_resource(product_name) if product.is_in_stock_or_ignore_stock(quantity_in_cart+1): cart.add_a_product(form['id'])
def action_pay(self, resource, context, form): from orders import Order cart = ProductCart(context) root = context.root # Check if cart is valid if not cart.is_valid(): return context.come_back(CART_ERROR, goto='/') # Calcul total price total_price_with_tax = decimal(0) total_price_without_tax = decimal(0) total_weight = decimal(0) for cart_elt in cart.products: product = context.root.get_resource(cart_elt['name']) quantity = cart_elt['quantity'] declination = cart_elt['declination'] unit_price_with_tax = product.get_price_with_tax(declination) unit_price_without_tax = product.get_price_without_tax(declination) total_price_with_tax += unit_price_with_tax * quantity total_price_without_tax += unit_price_without_tax * quantity total_weight += product.get_weight(declination) * quantity # Get Shipping price shipping_price = cart.get_shipping_ns(resource, context)['price'] total_price_with_tax += shipping_price total_price_without_tax += shipping_price # Arrondi total_price_with_tax = get_arrondi(total_price_with_tax) total_price_without_tax = get_arrondi(total_price_without_tax) # Guess ref number # We take last order name + 1 search = root.search(format='order') orders = search.get_documents(sort_by='creation_datetime', reverse=True) if orders: ref = str(int(orders[0].name) + 1) else: ref = '1' # We create a new order kw = {'user': context.user, 'payment_mode': form['payment'], 'shipping_price': shipping_price, 'total_price': total_price_with_tax, 'total_weight': total_weight, 'cart': cart, 'shop': resource, 'shop_uri': context.uri.resolve('/')} orders = resource.get_resource('orders') order = Order.make_resource(Order, orders, ref, title={'en': u'#%s' % ref}, **kw) # We clear the cart cart.clear() # We show the payment form kw = {'ref': ref, 'amount': total_price_with_tax, 'amount_without_tax': total_price_without_tax, 'resource_validator': str(order.get_abspath()), 'mode': form['payment']} payments = resource.get_resource('payments') return payments.show_payment_form(context, kw)
def action(self, resource, context, form): # We save option, if user choose it option = '' # We save shipping mode/option choosen by user cart = ProductCart(context) cart.set_shipping(form['shipping'], option) # Goto recapitulatif return context.uri.resolve(';show_recapitulatif')
def get_namespace(self, resource, context): cart = ProductCart(context) cart.clean() cart_is_empty = cart.products == [] if cart_is_empty: cart = None else: cart = Cart_View(see_actions=True).GET(resource, context) return {'cart': cart, 'cart_is_empty': cart_is_empty, 'progress': Shop_Progress(index=1).GET(resource, context)}
def action(self, resource, context, form): cart = ProductCart(context) # Set addresses cart._set_addresses(form['delivery_address'], form['bill_address']) # Set delivery zone addresses = resource.get_resource('addresses').handler delivery_address = addresses.get_record(int(form['delivery_address'])) country_id = addresses.get_record_value(delivery_address, 'country') countries = resource.get_resource('countries').handler country_record = countries.get_record(int(country_id)) cart.set_id_zone(countries.get_record_value(country_record, 'zone')) return context.come_back(MSG_CHANGES_SAVED, ';addresses')
def get_namespace(self, resource, context): cart = ProductCart(context) cart.clean() cart_is_empty = cart.products == [] if cart_is_empty: cart = None else: cart = Cart_View(see_actions=True).GET(resource, context) return { 'cart': cart, 'cart_is_empty': cart_is_empty, 'progress': Shop_Progress(index=1).GET(resource, context) }
def get_namespace(self, resource, context): namespace = {} cart = ProductCart(context) widget = SelectWidget('delivery_address', has_empty_option=False) namespace['delivery_address'] = widget.to_html( Addresses_Enumerate, cart.addresses['delivery_address']) widget = SelectWidget('bill_address', has_empty_option=False) namespace['bill_address'] = widget.to_html( Addresses_Enumerate, cart.addresses['bill_address']) return namespace
def GET(self, resource, context): # If user has no addresses, redirect to edit_address view cart = ProductCart(context) delivery_address = cart.addresses['delivery_address'] if delivery_address==None: delivery_address = resource.get_user_main_address(context.user.name) if not delivery_address: return context.uri.resolve(';add_address') else: # Set delivery address cart.set_delivery_address(delivery_address.id) # Set delivery zone addresses = resource.get_resource('addresses').handler country_id = addresses.get_record_value(delivery_address, 'country') countries = resource.get_resource('countries').handler country_record = countries.get_record(int(country_id)) cart.set_id_zone( countries.get_record_value(country_record, 'zone')) # Normal return STLView.GET(self, resource, context)
def action_add(self, resource, context, form): cart = ProductCart(context) product_name = cart.get_product_name(form['id']) quantity_in_cart = cart.get_product_quantity_in_cart(product_name) product = resource.get_resource(product_name) if product.is_in_stock_or_ignore_stock(quantity_in_cart + 1): cart.add_a_product(form['id'])
def get_namespace(self, resource, context): ns = {} # Progress bar ns['progress'] = Shop_Progress(index=3).GET(resource, context) # Get cart cart = ProductCart(context) # Delivery address delivery_address = cart.addresses['delivery_address'] ns['delivery_address'] = resource.get_user_address_namespace( delivery_address) # Bill ns['bill_address'] = None bill_address = cart.addresses['bill_address'] if bill_address and bill_address != delivery_address: ns['bill_address'] = resource.get_user_address_namespace( bill_address) return ns
def get_namespace(self, resource, context): ns = {} cart = ProductCart(context) # Progress ns['progress'] = Shop_Progress(index=4).GET(resource, context) # Get user delivery country addresses = resource.get_resource('addresses').handler delivery_address = cart.addresses['delivery_address'] record = addresses.get_record(delivery_address) country = addresses.get_record_value(record, 'country') # Guess shipping posibilities shippings_details = get_shippings_details(cart, context) shippings = resource.get_resource('shippings') ns['shipping'] = shippings.get_namespace_shipping_ways( context, country, shippings_details) # If no shipping ns['msg_if_no_shipping'] = shippings.get_property('msg_if_no_shipping') return ns
def GET(self, resource, context): # If user has no addresses, redirect to edit_address view cart = ProductCart(context) delivery_address = cart.addresses['delivery_address'] if delivery_address == None: delivery_address = resource.get_user_main_address( context.user.name) if not delivery_address: return context.uri.resolve(';add_address') else: # Set delivery address cart.set_delivery_address(delivery_address.id) # Set delivery zone addresses = resource.get_resource('addresses').handler country_id = addresses.get_record_value( delivery_address, 'country') countries = resource.get_resource('countries').handler country_record = countries.get_record(int(country_id)) cart.set_id_zone( countries.get_record_value(country_record, 'zone')) # Normal return STLView.GET(self, resource, context)
def get_namespace(self, resource, context): namespace = {'products': [], 'show_ht_price': resource.show_ht_price(context), 'see_actions': self.see_actions} abspath = resource.get_abspath() # Get cart cart = ProductCart(context) # Get products informations total_weight = decimal(0) total = {'with_tax': decimal(0), 'without_tax': decimal(0)} for product_cart in cart.products: # Get product product = context.root.get_resource(product_cart['name'], soft=True) # Check product is buyable if not product or not product.is_buyable(context): continue quantity = product_cart['quantity'] declination = product_cart['declination'] # Weight total_weight += product.get_weight(declination) * quantity # Prices unit_price_with_tax = product.get_price_with_tax(declination) unit_price_without_tax = product.get_price_without_tax(declination) total_price_with_tax = unit_price_with_tax * quantity total_price_without_tax = unit_price_without_tax * quantity price = { 'unit': {'with_tax': format_price(unit_price_with_tax), 'without_tax': format_price(unit_price_without_tax)}, 'total': {'with_tax': format_price(total_price_with_tax), 'without_tax': format_price(total_price_without_tax)}} total['without_tax'] += total_price_without_tax total['with_tax'] += total_price_with_tax # All declination_name = product_cart['declination'] if declination_name: declination_ns = product.get_declination_namespace(declination_name) else: declination_ns = None can_add_quantity = product.is_in_stock_or_ignore_stock(quantity+1, declination_name) namespace['products'].append( {'id': product_cart['id'], 'name': product.name, 'img': product.get_cover_namespace(context), 'title': product.get_title(), 'href': context.get_link(product), 'can_add_quantity': can_add_quantity, 'quantity': quantity, 'declination': declination_ns, 'price': price}) namespace['total'] = total # Get shippings namespace['ship'] = None if cart.shipping: namespace['ship'] = cart.get_shipping_ns(resource, context) namespace['total']['with_tax'] += namespace['ship']['price'] namespace['total']['without_tax'] += namespace['ship']['price'] # Format total prices for key in ['with_tax', 'without_tax']: namespace['total'][key] = format_price(namespace['total'][key]) return namespace
def action_remove(self, resource, context, form): cart = ProductCart(context) cart.remove_a_product(form['id'])
def action(self, resource, context, form): shop = get_shop(resource) root = context.root site_root = resource.get_site_root() # Check the new password matches password = form['password'].strip() if password != form['password_check']: context.message = ERROR(u"The two passwords are different.") return if shop.get_property('registration_need_email_validation') is False: msg = MSG(u'Your inscription has been validaded.') else: msg = MSG(u'Your inscription has been validaded, ' u'you will receive an email to confirm it.') # Do we already have a user with that email? email = form['email'].strip() user = root.get_user_from_login(email) if user is not None: context.message = ERROR(u'This email address is already used.') return # Add the user users = root.get_resource('users') user = users.set_user(email, password) # Set user group (do it befor save_form for dynanic schema) group = self.get_group(context) user.set_property('user_group', str(group.get_abspath())) # Save properties user.save_form(self.get_schema(resource, context), form) # Save address in addresses table if group.get_property('hide_address_on_registration') is False: kw = {'user': user.name} addresses = shop.get_resource('addresses') for key in ['gender', 'lastname', 'firstname', 'address_1', 'address_2', 'zipcode', 'town', 'country']: kw[key] = form[key] kw['title'] = MSG(u'Your address').gettext() addresses.handler.add_record(kw) # Clean cart, if another user already login before cart = ProductCart(context) cart.clean() # Set the role site_root.set_user_role(user.name, 'guests') # We log authentification shop = get_shop(resource) logs = shop.get_resource('customers/authentification_logs') logs.log_authentification(user.name) user.set_property('last_time', datetime.now()) # Send confirmation email need_email_validation = shop.get_property('registration_need_email_validation') user.send_register_confirmation(context, need_email_validation) # User is enabled ? user_is_enabled = group.get_property('user_is_enabled_when_register') user.set_property('is_enabled', user_is_enabled) # Create modules if needed search = context.root.search(is_shop_user_module=True) for brain in search.get_documents(): shop_user_module = root.get_resource(brain.abspath) shop_user_module.initialize(user) # If user not enabled, send mail to webmaster to validate user if user_is_enabled is False: subject = MSG(u'A customer must be validated in your shop').gettext() shop_backoffice_uri = shop.get_property('shop_backoffice_uri') body = registration_notification_body.gettext( name=user.name, email=email, shop_backoffice_uri=shop_backoffice_uri) for to_addr in shop.get_property('order_notification_mails'): root.send_email(to_addr, subject, text=body) # If need_email_validation or user not enable redirect on Welcome if need_email_validation is True or user_is_enabled is False: goto = '%s/welcome/' % context.get_link(group) return context.come_back(msg, goto=goto) ######################## # Do authentification ######################## # Set cookie user.set_auth_cookie(context, form['password']) # Set context context.user = user # Redirect shop = get_shop(resource) if form['goto']: goto = context.query['goto'] elif resource == shop: goto = './;addresses' elif resource.class_id == shop.product_class.class_id: goto = './' else: goto = '/users/%s' % user.name return context.come_back(msg, goto)
def action_clear(self, resource, context, form): cart = ProductCart(context) cart.clear()
def action(self, resource, context, form): shop = get_shop(resource) root = context.root site_root = resource.get_site_root() # Check the new password matches password = form['password'].strip() if password != form['password_check']: context.message = ERROR(u"The two passwords are different.") return if shop.get_property('registration_need_email_validation') is False: msg = MSG(u'Your inscription has been validaded.') else: msg = MSG(u'Your inscription has been validaded, ' u'you will receive an email to confirm it.') # Do we already have a user with that email? email = form['email'].strip() user = root.get_user_from_login(email) if user is not None: context.message = ERROR(u'This email address is already used.') return # Add the user users = root.get_resource('users') user = users.set_user(email, password) # Set user group (do it befor save_form for dynanic schema) group = self.get_group(context) user.set_property('user_group', str(group.get_abspath())) # Save properties user.save_form(self.get_schema(resource, context), form) # Save address in addresses table if group.get_property('hide_address_on_registration') is False: kw = {'user': user.name} addresses = shop.get_resource('addresses') for key in [ 'gender', 'lastname', 'firstname', 'address_1', 'address_2', 'zipcode', 'town', 'country' ]: kw[key] = form[key] kw['title'] = MSG(u'Your address').gettext() addresses.handler.add_record(kw) # Clean cart, if another user already login before cart = ProductCart(context) cart.clean() # Set the role site_root.set_user_role(user.name, 'guests') # We log authentification shop = get_shop(resource) logs = shop.get_resource('customers/authentification_logs') logs.log_authentification(user.name) user.set_property('last_time', datetime.now()) # Send confirmation email need_email_validation = shop.get_property( 'registration_need_email_validation') user.send_register_confirmation(context, need_email_validation) # User is enabled ? user_is_enabled = group.get_property('user_is_enabled_when_register') user.set_property('is_enabled', user_is_enabled) # Create modules if needed search = context.root.search(is_shop_user_module=True) for brain in search.get_documents(): shop_user_module = root.get_resource(brain.abspath) shop_user_module.initialize(user) # If user not enabled, send mail to webmaster to validate user if user_is_enabled is False: subject = MSG( u'A customer must be validated in your shop').gettext() shop_backoffice_uri = shop.get_property('shop_backoffice_uri') body = registration_notification_body.gettext( name=user.name, email=email, shop_backoffice_uri=shop_backoffice_uri) for to_addr in shop.get_property('order_notification_mails'): root.send_email(to_addr, subject, text=body) # If need_email_validation or user not enable redirect on Welcome if need_email_validation is True or user_is_enabled is False: goto = '%s/welcome/' % context.get_link(group) return context.come_back(msg, goto=goto) ######################## # Do authentification ######################## # Set cookie user.set_auth_cookie(context, form['password']) # Set context context.user = user # Redirect shop = get_shop(resource) if form['goto']: goto = context.query['goto'] elif resource == shop: goto = './;addresses' elif resource.class_id == shop.product_class.class_id: goto = './' else: goto = '/users/%s' % user.name return context.come_back(msg, goto)
def GET(self, resource, context): cart = ProductCart(context) # Check if cart is valid if not cart.is_valid(): return context.come_back(CART_ERROR, goto='/') return STLForm.GET(self, resource, context)
def action_delete(self, resource, context, form): cart = ProductCart(context) cart.delete_a_product(form['id'])
def GET(self, resource, context): cart = ProductCart(context) if cart.get_nb_products() <= 0: return return STLView.GET(self, resource, context)
def get_namespace(self, resource, context): namespace = { 'products': [], 'show_ht_price': resource.show_ht_price(context), 'see_actions': self.see_actions } abspath = resource.get_abspath() # Get cart cart = ProductCart(context) # Get products informations total_weight = decimal(0) total = {'with_tax': decimal(0), 'without_tax': decimal(0)} for product_cart in cart.products: # Get product product = context.root.get_resource(product_cart['name'], soft=True) # Check product is buyable if not product or not product.is_buyable(context): continue quantity = product_cart['quantity'] declination = product_cart['declination'] # Weight total_weight += product.get_weight(declination) * quantity # Prices unit_price_with_tax = product.get_price_with_tax(declination) unit_price_without_tax = product.get_price_without_tax(declination) total_price_with_tax = unit_price_with_tax * quantity total_price_without_tax = unit_price_without_tax * quantity price = { 'unit': { 'with_tax': format_price(unit_price_with_tax), 'without_tax': format_price(unit_price_without_tax) }, 'total': { 'with_tax': format_price(total_price_with_tax), 'without_tax': format_price(total_price_without_tax) } } total['without_tax'] += total_price_without_tax total['with_tax'] += total_price_with_tax # All declination_name = product_cart['declination'] if declination_name: declination_ns = product.get_declination_namespace( declination_name) else: declination_ns = None can_add_quantity = product.is_in_stock_or_ignore_stock( quantity + 1, declination_name) namespace['products'].append({ 'id': product_cart['id'], 'name': product.name, 'img': product.get_cover_namespace(context), 'title': product.get_title(), 'href': context.get_link(product), 'can_add_quantity': can_add_quantity, 'quantity': quantity, 'declination': declination_ns, 'price': price }) namespace['total'] = total # Get shippings namespace['ship'] = None if cart.shipping: namespace['ship'] = cart.get_shipping_ns(resource, context) namespace['total']['with_tax'] += namespace['ship']['price'] namespace['total']['without_tax'] += namespace['ship']['price'] # Format total prices for key in ['with_tax', 'without_tax']: namespace['total'][key] = format_price(namespace['total'][key]) return namespace
def get_namespace(self, resource, context): cart = ProductCart(context) return cart.get_namespace(resource)
def action_pay(self, resource, context, form): from orders import Order cart = ProductCart(context) root = context.root # Check if cart is valid if not cart.is_valid(): return context.come_back(CART_ERROR, goto='/') # Calcul total price total_price_with_tax = decimal(0) total_price_without_tax = decimal(0) total_weight = decimal(0) for cart_elt in cart.products: product = context.root.get_resource(cart_elt['name']) quantity = cart_elt['quantity'] declination = cart_elt['declination'] unit_price_with_tax = product.get_price_with_tax(declination) unit_price_without_tax = product.get_price_without_tax(declination) total_price_with_tax += unit_price_with_tax * quantity total_price_without_tax += unit_price_without_tax * quantity total_weight += product.get_weight(declination) * quantity # Get Shipping price shipping_price = cart.get_shipping_ns(resource, context)['price'] total_price_with_tax += shipping_price total_price_without_tax += shipping_price # Arrondi total_price_with_tax = get_arrondi(total_price_with_tax) total_price_without_tax = get_arrondi(total_price_without_tax) # Guess ref number # We take last order name + 1 search = root.search(format='order') orders = search.get_documents(sort_by='creation_datetime', reverse=True) if orders: ref = str(int(orders[0].name) + 1) else: ref = '1' # We create a new order kw = { 'user': context.user, 'payment_mode': form['payment'], 'shipping_price': shipping_price, 'total_price': total_price_with_tax, 'total_weight': total_weight, 'cart': cart, 'shop': resource, 'shop_uri': context.uri.resolve('/') } orders = resource.get_resource('orders') order = Order.make_resource(Order, orders, ref, title={'en': u'#%s' % ref}, **kw) # We clear the cart cart.clear() # We show the payment form kw = { 'ref': ref, 'amount': total_price_with_tax, 'amount_without_tax': total_price_without_tax, 'resource_validator': str(order.get_abspath()), 'mode': form['payment'] } payments = resource.get_resource('payments') return payments.show_payment_form(context, kw)