Esempio n. 1
0
def edit(request):
    profile_instance = Profile.objects.get_or_create(user=request.user.id)[0]
    address_object_list = request.user.profile.addresses.all()
    i = 0
    addresses = [[0 for x in range(2)]
                 for y in range(len(address_object_list))]
    for address in address_object_list:
        addresses[i] = {
            'object': address,
            'form': AddressForm(request.POST or None, instance=address)
        }
        i += 1
    context = {
        'user_form':
        UserForm(request.POST or None,
                 request.FILES or None,
                 instance=request.user),
        'profile_form':
        ProfileForm(request.POST or None,
                    request.FILES or None,
                    instance=profile_instance),
        'addresses':
        addresses,
    }
    if request.method == 'POST':
        if context['user_form'].is_valid(
        ) and context['profile_form'].is_valid():
            context['user_form'].save()
            context['profile_form'].save()
    return TemplateResponse(request, 'account/edit.html', context)
Esempio n. 2
0
def address_edit(request, type, id=None):

    # NOT USING FOR NOW
    # TODO need to discuss how this form should looks like
    if id:
        address = get_object_or_404(Address, pk=id)
    else:
        address = Address(type=type, user=request.user)

    if request.method == 'POST':
        form = AddressForm(request.POST, instance=address)
        if form.is_valid():
            form.save()
            messages.add_message(request, messages.SUCCESS,
                                 'Address was successfully saved')
            return HttpResponse(json.dumps({'result': 'success', 'next': '/'}))
        else:
            response = {}
            for k in form.errors:
                response[k] = form.errors[k][0]

            return HttpResponse(
                json.dumps({
                    'response': response,
                    'result': 'error'
                }))
    else:
        form = AddressForm(instance=address)

    return render(request, 'account/edit_address.html', {
        'form': form,
        'type': type
    })
Esempio n. 3
0
def edit_address(request, addr_type):
    try:
        profile_obj = request.user.get_profile()
    except ObjectDoesNotExist:
        return HttpResponseRedirect(reverse('profiles_create_profile'))

    if addr_type == "1":
        address = profile_obj.billing_address
    elif addr_type == "2":
        address = profile_obj.shipping_address
    else:
        raise Http404

    if not address:
        address = Address()

    if request.method == 'POST':
        form = AddressForm(data=request.POST, files=request.FILES, instance=address)

        if form.is_valid():
            obj = form.save()
            if addr_type == "1":
                profile_obj.billing_address = obj
            elif addr_type == "2":
                profile_obj.shipping_address = obj
            else:
                raise Http404
            profile_obj.save()
            return HttpResponseRedirect(reverse('profiles_profile_detail'))
    else:
        form = AddressForm(instance=address)

    return render(
        request,
        'profiles/address.html',
        {'form': form})
Esempio n. 4
0
def order_create_view(request, option_id):
	option = get_object_or_404(Option, pk=option_id)

	if request.POST:
		name_consumer = request.POST.getlist('name_consumer[]')
		option_id = request.POST.getlist('option_id[]')
		quantity = request.POST.getlist('quantity[]')
		order_total = 0
		offers_shipping = ""

		order = Order.objects.create(user=request.user, status=2, total=0)
		#SUBTOTAIS
		# import pdb;pdb.set_trace()
		if len(name_consumer) == len(option_id) and len(option_id) == len(quantity):
			for i in range(len(name_consumer)):
				opt = get_object_or_404(Option, pk=option_id[i])
				item_total = int(quantity[i]) * option.new_price
				order_item = OrderItem.objects.create(order=order, name_consumer=name_consumer[i], option=opt, quantity=quantity[i], total=item_total)
				order_total += item_total
				offers_shipping += ",%s:%s" % (opt.id, quantity[i])

		order_total = float(order_total)

		#FRETE
		if option.offer.delivery and option.offer.shipping and request.POST.get('cep'):
			result = calculate_shipping(request.POST.get('cep'), offers_shipping[1:])
			if result['error'] != 0:
				return HttpResponse('erro frete: %s' % result['data'])
			else:
				order_total += float(result['data'])
				order.shipping = float(result['data'])

		# import pdb;pdb.set_trace()
		#CUPOM DE DESCONTO
		if request.POST.get('code_discount'):
			result = PromotionCode.objects.filter(code=request.POST.get('code_discount'), start_time__lte=datetime.today(), end_time__gte=datetime.today())
			if result:
				order_total -= float(result[0].discount)
				order.discount = float(result[0].discount)

		# SALDO DE COMPRA
		if request.POST.get('use_credit', False):
			credit = float(request.user.credit)
			if credit <= order_total:
				order_total -= credit
				Operation.objects.create(user=request.user, type_operation=DEBIT, description='compra: %s' % order.id, value=credit)
				
			else:
				credit = order_total
				Operation.objects.create(user=request.user, type_operation=DEBIT, description='compra: %s' % order.id, value=order_total)
				order_total = 0
				# order.status = ORDER_AUTHORIZED
			order.balance = credit

		# import pdb;pdb.set_trace()
		# ENDEREÇO
		if option.offer.delivery and request.POST.get('address'):
			if request.POST.get('address') == 'new':
				form = AddressForm(request.POST)
				if form.is_valid():
					address = form.save()
			else:
				address = get_object_or_404(Address, id=request.POST.get('address'))
			
			order.address = address

		order.total = order_total
		order.save()

		# messages.add_message(request, messages.INFO, 'Hello world.')		

		if order.total == 0:
			order.status = ORDER_AUTHORIZED
			self.object.save()
		elif order.total > 0:
			return redirect(order.pay_pagseguro())
		return HttpResponseRedirect(reverse_lazy('offer:user:my_orders', kwargs={}))


	context = {}
	context['option'] = option
	context['other_options'] = option.offer.options.all()
	max_by_user = option.offer.max_by_user+1 if option.offer.max_by_user else 11
	context['range_quantity'] = range(1, max_by_user)
	return render(request, 'checkout/order_create.html', context)