def _pre_view( self_obj, request, shop_name, *args, **kwargs ): self_obj._shop = get_shop_by_name( request.session, shop_name ) if self_obj._shop.is_removed: return page403Or404( request ) self_obj._shop_face = ShopFace.objects.get( shop = self_obj._shop ) self_obj._shop_id = get_shop_id_by_name( request.session, shop_name ) return super( cls, self_obj ).pre_view( request, shop_name, *args, **kwargs )
def view( self, request, shop_name, dictionary = {} ): cart = Cart( request.session ) req = request.REQUEST Location( request.session, 'shop' ).set( get_shop_location( shop_name, 'search', request.GET ) ) search = Search( request.session ) search_is_set = search.is_set() d = { 'categories': get_sorted_by_name( get_field_list_from_meta( 'product_category', ShopProductCategory, 'shop', self._shop_id ) ), 'producers': get_sorted_by_name( get_field_list_from_meta( 'producer', ShopProducer, 'shop', self._shop_id ) ), 'countries': get_sorted_by_name( get_field_list_from_meta( 'country', ShopCountry, 'shop', self._shop_id ) ), 'search_is_set': search_is_set } q_dict = { 'shop': self._shop_id } if request.method == 'POST': form = SearchProductForm( request.POST ) fields = dict( [ ( key, request.POST[ key ] ) for key in form.fields.iterkeys() ] ) if form.is_valid(): d.update( is_valid = True ) q_dict.update( form.cleaned_data ) search.set( **q_dict ) shop = get_shop_by_name( request.session, shop_name ) page_products = ItemPage( Product ).get( q_dict = SearchView._removed_empty_items( form, q_dict ), page = 1 ) d.update( page = 1 ) else: d.update( is_valid = False ) page_products = None else: form = SearchProductForm() is_page = 'page' in req page = int( req[ 'page' ] ) if is_page else 1 d.update( page = page ) if search.is_set(): fields = search.get() if is_page: fields.update( shop = self._shop_id ) page_products = ItemPage( Product ).get( q_dict = SearchView._removed_empty_items( form, search.get() ), page = page ) else: page_products = None else: fields = dict( [ ( key, form.fields[ key ].initial ) for key in form.fields.iterkeys() ] ) page_products = None if page_products: cart.add_in_cart_attr( page_products[ 'object_list' ] ) d.update( page_products = page_products ) d.update( form = form ) d.update( fields = fields ) d.update( dictionary ) return super( SearchView, self ).view( request, shop_name, d )
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_obj, request, shop_name, dictionary = {} ): user = get_shop_by_name( request.session, shop_name ).user d = { 'background': self_obj._shop_face.get_background(), 'product_bk_color': self_obj._shop_face.product_bk_color, 'product_text_color': self_obj._shop_face.product_text_color, 'menu_bk_color': self_obj._shop_face.menu_bk_color, 'menu_text_color': self_obj._shop_face.menu_text_color, 'user_text_color': self_obj._shop_face.user_text_color, 'domain_shop_name': get_shop_name( request ), 'user_username': user.username, 'user_first_name': user.first_name, 'user_last_name': user.last_name } try: owner_profile = UserProfile.objects.get( user = user ) except ObjectDoesNotExist: d.update( has_user_profile = False ) else: user_image = owner_profile.image.url d.update( user_profile = owner_profile ) d.update( has_user_profile = True ) d.update( dictionary ) return super( cls, self_obj ).view( request, d )
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 } )