Exemplo n.º 1
0
	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 )
Exemplo n.º 2
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 } )