Esempio n. 1
0
	def view( self, request, shop_name ):
		Location( request.session, 'shop' ).set( get_shop_location( shop_name, '', request.GET ) )
		req = request.REQUEST
		form_request = None
		page = int( req[ 'page' ] ) if 'page' in req else 1	

		q_dict = {}
		q_dict.update( shop = self._shop_id )
		
		if 'category' in req:
			category = int( req[ 'category' ] )
			q_dict.update( category = category )
		else:
			category = None
		
		page_products = ItemPage( Product ).get( q_dict = q_dict, page = page )
		
		cart = Cart( request.session )
		cart.add_in_cart_attr( page_products[ 'object_list' ] )

		return super( ShopView, self ).view( request, shop_name,
												{	'page_products': page_products,
													'page': int( page ),
													'num_products_on_page': NUM_ITEMS_ON_PAGE,
													'categories': get_sorted_by_name( get_field_list_from_meta( 'product_category', ShopProductCategory, 'shop', self._shop_id  ) ),
													'category': category
												} )
Esempio n. 2
0
	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 )
Esempio n. 3
0
	def view( self, request, shop_name ):
		cart = Cart( request.session )
		product_id = request.REQUEST[ 'product_id' ]
		product = Product.objects.get( id = product_id )
		cart.add_item(		name = product.name, 
							description = product.description,
							price = product.price, 
							count = 1, 
							product_id = product_id, 
							thumbnail_url = product.thumbnail_url )
		return self._cart_result_ok( request, product_id )
Esempio n. 4
0
	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
												} )
Esempio n. 5
0
	def view( self, request, shop_name ):

		cart = Cart( request.session )

		order_detail_in_session = utils.OrderDetail( request.session )
		order_detail = order_detail_in_session.get()

		delivery_cost = order_detail[ 'delivery_cost' ]

		products = copy( cart.as_list() )
		cart.empty()

		order_sum = utils.calc_order_sum( products, delivery_cost )
		
		return super( OrderCompletedView, self ).view(	request, shop_name,
											{	'name_surname': order_detail[ 'name_surname' ],
												'phone': order_detail[ 'phone' ],
												'address': order_detail[ 'address' ],
												'delivery_cost': delivery_cost,
												'order_time': order_detail[ 'order_time' ],
												'products': products,
												'order_sum': order_sum
											} )
Esempio n. 6
0
	def view( self, request, shop_name, product_id ):
		req_get = request.GET		
		product = Product.objects.get( id = int( product_id ) )
		shop_id = get_shop_id_by_name( request.session, shop_name )

		if product.shop.id != shop_id:
			raise ProductNotInThisShopError( "Product with id {0} not found in shop {1}".format( product_id, self._shop_name ) )		
		cart = Cart( request.session)
		in_cart = str( product.id ) in cart.as_dict()

		location = Location( request.session, 'shop' )
		if location.is_set():
			back = location.get()
		else:
			back = None

		return super( ProductView, self ).view(	request, 
												shop_name, 
												{ 
													'back': back, 
													'product': product, 
													'in_cart': in_cart, 
													'opt': req_get[ 'opt' ] if 'opt' in req_get else None 
												} )
Esempio n. 7
0
	def view( self, request, shop_name ):
		cart = Cart( request.session )
		product_id = request.REQUEST[ 'product_id' ]
		cart.remove_item( product_id )
		return self._cart_result_ok( request, product_id )
Esempio n. 8
0
	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 } )