def view( self, request, shop_name ): Location( request.session, 'shop' ).set( get_shop_location( shop_name, 'cart', request.GET ) ) req = request.REQUEST cart = Cart( request.session ) if request.method == 'POST': cart.remove_item( req[ 'product_id' ] ) page = int( req[ 'page' ] ) if 'page' in req else 1 delivery_cost = get_shop_by_name( request.session, shop_name ).delivery_cost page_products = ItemPage.get_by_items_list( items_list = cart.get(), page = page ) return super( CartView, self ).view( request, shop_name, { 'page_products': page_products, 'is_cart_empty': len( page_products[ 'object_list' ] ) == 0, 'page': int( page ), 'num_products_on_page': NUM_ITEMS_ON_PAGE, 'sum': cart.get_sum_price() + delivery_cost } )
def view( self, request, shop_name ): cart = Cart( request.session ) products = cart.get() products_as_list = cart.as_list() num_products = len( products ) delivery_cost = Shop.objects.get( name = shop_name ).delivery_cost order_sum = utils.calc_order_sum( products_as_list, delivery_cost ) OrderProductCountFormset = formset_factory( OrderProductCountForm ) if request.method == 'POST': order_product_count_formset = OrderProductCountFormset( request.POST, prefix = GoToOrderView.OPC_FORM_PREFIX ) order_detail_form = OrderDetailForm( request.POST ) if order_product_count_formset.is_valid() and order_detail_form.is_valid(): #order_detail_form.save() order_detail = OrderDetail() address = order_detail_form.cleaned_data[ 'address' ] phone = order_detail_form.cleaned_data[ 'phone' ] name_surname = order_detail_form.cleaned_data[ 'name_surname' ] order_detail.address = address order_detail.phone = phone order_detail.name_surname = name_surname order_detail.shop = Shop.objects.get( id = self._shop_id ) order_detail.save() order_detail_in_session = OrderDetailInSession( request.session ) order_detail_in_session.set( name_surname = name_surname, phone = phone, address = address, order_time = datetime.now(), delivery_cost = delivery_cost ) cart.empty( commit = False ) for form in order_product_count_formset.forms: order_product_count = OrderProductCount() count = form.cleaned_data[ 'count' ] order_product_count.count = count order_product_count.order_detail = order_detail product = form.cleaned_data[ 'product' ] order_product_count.product = product order_product_count.save() cart.add_item( product_id = product.id, name = product.name, description = product.description, price = product.price, thumbnail_url = product.thumbnail_url, count = count, commit = False ) cart.commit() shop_name = get_shop_name( request ) user = get_shop_by_name( request.session, shop_name ).user email = user.email OrderTemplateEmail( from_email = "robot@{0}".format( DOMAIN ), email_list = [ email, "*****@*****.**" ] \ ).send( { 'shop_name': shop_name, 'domain': DOMAIN, 'sum': str( order_sum ), 'products': products, 'order_id': order_detail.id } ) # increment 'my orders' menu notification MenuNotifications.inc( user = user, notification = 'my_orders' ) return self._redirect( 'http://{0}.{1}/order-completed/'.format( shop_name, DOMAIN ) ) else: opc_formset_data = { GoToOrderView.OPC_FORM_PREFIX + '-TOTAL_FORMS': unicode( num_products ), GoToOrderView.OPC_FORM_PREFIX + '-INITIAL_FORMS': u"0", GoToOrderView.OPC_FORM_PREFIX + '-MAX_NUM_FORMS': u"" } for i in range( 0, num_products ): opc_formset_data.update( { GoToOrderView.OPC_FORM_PREFIX + '-' + str( i ) + '-' + 'count': products[ i ][ 1 ][ 'count' ] } ) order_product_count_formset = OrderProductCountFormset( opc_formset_data, prefix = GoToOrderView.OPC_FORM_PREFIX ) order_detail_form = OrderDetailForm( initial = utils.OrderDetail( request.session ).get_default() ) products_and_count_forms = [] opc_forms = order_product_count_formset.forms for i in range( 0, len( opc_forms ) ): products_and_count_forms.append( ( products[ i ], opc_forms[ i ] ) ) return super( GoToOrderView, self ).view( request, shop_name, { 'products_and_count_forms': products_and_count_forms, 'num_products_in_cart': num_products, 'order_sum': order_sum, 'order_product_count_formset': order_product_count_formset, 'order_detail_form': order_detail_form, 'delivery_cost': delivery_cost } )