Example #1
0
def updateProfile(request):
    currentUser=User.objects.get(pk=request.user.id)
    c=Customer.objects.get(user=currentUser.id)
    customerForm=CustomerForm(request.POST, instance = c)
    
    userForm=UserForm2(request.POST, instance = currentUser)
    
    if customerForm.is_valid() and userForm.is_valid():
        customerForm.save()
        userForm.save()
        
    request.user = User.objects.get(pk=request.user.id)
    #customer=Customer.objects.get(user=request.user.id)
    #customerForm=CustomerForm(instance = customer)
    return render_to_response('customers/edit_profile.html',
                               {'user': request.user ,
                                'userForm':userForm , 
                                'customerForm':customerForm },
                               context_instance=RequestContext(request))
Example #2
0
def subscribe(request):
    if request.method == 'POST':
        form = CustomerForm(request.POST)
        if form.is_valid():
            customer = form.save(commit=False)
            customer.user = request.user
            customer.save()
            
            # Send an email to teh admin letting them know that a person registered
            subject = render_to_string('subscribe/email_subject.txt',)
            message = render_to_string('subscribe/email.txt', {
                    'user': request.user,
                })
            recipients = ['*****@*****.**']
            # recipients = ['*****@*****.**']
            send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, recipients)
            
            return HttpResponseRedirect('/thanks/')
    else:
        form = CustomerForm()
        
    return render_to_response('subscribe/customer.html', {'form':form}, context_instance=RequestContext(request))
Example #3
0
def checkout(request, confirm=False):
    ses = request.session
    seshelp = SessionHelper(ses)
    totqty, grandtot = seshelp.cart_totals(
    )  # TODO: convert these legacy references to use the dict retruned from cart_summary
    post = request.POST
    get = request.GET
    user = request.user
    cart = ses.get('cart')

    if not cart:  # likely landed here from somewhere else, redirect to "Cart is empty" message
        return HttpResponseRedirect('/cart/')

    if post:
        customer = ses.get('customer')
        billing_address = ses.get('billing_address')
        shipping_address = ses.get('shipping_address')

        if trace_order:
            print 'POST', customer, billing_address, shipping_address

        user_form = UserForm(post, instance=user)
        customer_form = CustomerForm(post, instance=customer)
        order_form = OrderForm(post)
        shipping_address_form = AddressForm(post,
                                            prefix='shipping',
                                            instance=shipping_address)
        billing_address_form = BillingAddressForm(post,
                                                  prefix='billing',
                                                  instance=billing_address)
        payment_form = PaymentForm(post)

        def cross_form_fdedits():  # must be called after forms are checked
            if shipping_address_form['state'].value(
            ) == 'CA' and not order_form['california_tax'].value():
                # this fails on Dj 1.7.x
                #order_form._update_errors ({ 'california_tax': ['CA shipments must choose county'] })
                # nope, this isn't trapped
                #raise ValidationError ('CA shipments must choose county',
                #    code='CA_County_required')
                #params={ 'california_tax': 'county required' })
                order_form.add_error('california_tax',
                                     'Shipments to CA must choose county')
                return False

            return True

        if (customer_form.is_valid() and order_form.is_valid()
                and shipping_address_form.is_valid()
                and (billing_address_form.is_valid()
                     or billing_address_form.is_empty()
                     )  # has_changed nfg for choice fields like state
                and payment_form.is_valid() and cross_form_fdedits()):
            # Process the forms taking care of proper creation order for FKs
            customer = customer_form.save(commit=False)
            customer.user = user
            ses['customer'] = customer
            #customer.save()
            if trace:
                print 'POST CUSTOMER', customer.id, user.id, customer.user_id

            order = order_form.save(commit=False)

            # TODO: compute shipping based on weight, boxes, etc!
            multiplier = 1.0  # TODO: get this from table, above
            #multiplier = totqty  # no - (totqty is the # of line items)

            if order.shipping_payment == "included":
                # Note to Mani & developers: What we should do here is:
                # - ensure the totweight is calculated correctly, including the quantity of each line item
                # - (check also how quotes work too, and ensure the cart is being filled correctly)
                # - in the future, also includ # of packages in the calcualtion, as shipping of each box has a separate cost
                # We could also look into a shipping calcualtion add-on for Django
                shipping = seshelp.cart_summary()['totweight'] * multiplier
            else:
                shipping = 0.0

            if order.california_tax:
                tax = grandtot * float(order.california_tax.tax) / 100
            else:
                tax = 0.0

            order.tax = tax
            order.shipping = str(shipping)
            order.customer_id = customer.id
            order.status = 'new'
            order.cart = cart

            shipping_address = shipping_address_form.save(commit=False)
            shipping_address.customer_id = customer.id

            if billing_address_form.is_empty():  # not has_changed():
                shipping_address.type = 'both'
                ses['shipping_address'] = shipping_address
                #shipping_address.save()
                #print shipping_address.id
                order.bill_to_address_id = shipping_address.id
                billing_address = shipping_address
                billing_name = customer.name()
            else:
                shipping_address.type = 'shipping'
                ses['shipping_address'] = shipping_address
                #shipping_address.save()
                billing_address = billing_address_form.save(commit=False)
                billing_address.customer_id = customer.id
                billing_address.type = 'billing'
                ses['billing_address'] = billing_address
                #billing_address.save()
                #print billing_address.id
                order.bill_to_address_id = billing_address.id
                if not billing_address.name:
                    billing_address.name = customer.name()
                billing_name = billing_address.name

            order.ship_to_address_id = shipping_address.id
            ses['billing_name'] = billing_name
            ses['order'] = order
            #order.save()

            payment = payment_form.save(commit=False)
            payment.user = user
            #print order.id, order.customer_id
            payment.order_id = order.id
            payment.last_4 = payment_form.cleaned_data['card_number'][-4:]
            payment.payment_terms = payment.payment_method[2:]
            ses['payment'] = payment
            ses['payment_data'] = payment_form.cleaned_data
            #payment.save()

            gtts = grandtot + tax + shipping
            ses['gtts'] = gtts

            if confirm:  # display conf form
                return render_to_response(
                    'confirm.html',
                    dict(
                        breadcrumbs=(
                            Breadcrumb('Checkout', 'Checkout - Enter info',
                                       '/checkout/'),
                            Breadcrumb('Confirm', 'Confirm your order',
                                       '/checkout/confirm/'),
                        ),
                        ses=ses,
                        seshelp=seshelp,
                        summary=seshelp.cart_summary_table(
                        ),  # JJW 8/7/15 for consistency in new theme templates
                        grandtot=grandtot,
                        totqty=totqty,
                        tax=tax,
                        shipping=shipping,
                        gtts=gtts,
                        order=order,
                        payment=payment),
                    context_instance=RequestContext(request))

    else:  # it's a GET, fill in the inital form
        #customer = user.customer_set.all() [:1] or []
        customer = Customer.objects.filter(user=user)

        if customer:
            address_obj = Address.objects.filter(customer=customer)
            user_form = UserForm(instance=user)
            customer = customer[0]

            if trace: print 'CUSTOMER', customer.id, user.id, customer.user_id
            customer_form = CustomerForm(instance=customer)
            ses['customer'] = customer
            order_form = OrderForm()
            if address_obj:
                shipping_address_form = AddressForm(
                    prefix='shipping', instance=customer.default_shipping())
            else:
                shipping_address_form = AddressForm(
                    prefix='shipping',
                    instance=customer.default_shipping(),
                    initial={
                        'name':
                        customer.user.first_name + ' ' +
                        customer.user.last_name
                    })
            #shipping_address_form    = AddressForm (prefix='shipping', instance=customer.default_shipping(),initial={'name': customer.user.first_name})
            billing_address_form = BillingAddressForm(
                prefix='billing', instance=customer.default_billing())
            ses['shipping_address'] = customer.default_shipping()
            ses['billing_address'] = customer.default_billing()
            payment_form = PaymentForm()
        else:
            if trace: print 'USER', user, user.id
            user_form = UserForm()
            customer_form = CustomerForm(initial=dict(email=user.email))
            order_form = OrderForm()
            shipping_address_form = AddressForm(prefix='shipping')
            billing_address_form = BillingAddressForm(prefix='billing')
            payment_form = PaymentForm()

        user_form.header += ' (User: '******')'

    return render_to_response(
        'checkout.html',
        dict(
            breadcrumbs=(Breadcrumb('Checkout', 'Checkout', '/checkout/'), ),
            formlist=(user_form, customer_form, order_form,
                      shipping_address_form, billing_address_form,
                      payment_form),
            totqty=totqty,
            grandtot=grandtot,
            summary=seshelp.cart_summary_table(
            ),  # JJW 8/7/15 for consistency in new theme templates
        ),
        context_instance=RequestContext(request))
Example #4
0
def checkout (request, confirm=False):
    ses = request.session
    seshelp = SessionHelper (ses)
    totqty, grandtot = seshelp.cart_totals() # TODO: convert these legacy references to use the dict retruned from cart_summary
    post = request.POST
    get = request.GET
    user = request.user
    cart = ses.get ('cart')

    if not cart:  # likely landed here from somewhere else, redirect to "Cart is empty" message
        return HttpResponseRedirect ('/cart/')

    if post:
        customer                = ses.get ('customer')
        billing_address         = ses.get ('billing_address')
        shipping_address        = ses.get ('shipping_address')

        if trace_order: print 'POST', customer, billing_address, shipping_address

        user_form               = UserForm (post, instance=user)
        customer_form           = CustomerForm (post, instance=customer)
        order_form              = OrderForm (post)
        shipping_address_form   = AddressForm (post, prefix='shipping', instance=shipping_address)
        billing_address_form    = BillingAddressForm (post, prefix='billing', instance=billing_address)
        payment_form            = PaymentForm (post)

        def cross_form_fdedits():  # must be called after forms are checked
            if shipping_address_form['state'].value() == 'CA' and not order_form['california_tax'].value():
                # this fails on Dj 1.7.x
                #order_form._update_errors ({ 'california_tax': ['CA shipments must choose county'] })
                # nope, this isn't trapped
                #raise ValidationError ('CA shipments must choose county',
                #    code='CA_County_required')
                    #params={ 'california_tax': 'county required' })
                order_form.add_error ( 'california_tax', 'Shipments to CA must choose county')
                return False

            return True

        if (customer_form.is_valid()
                and order_form.is_valid()
                and shipping_address_form.is_valid()
                and (billing_address_form.is_valid() or billing_address_form.is_empty())  # has_changed nfg for choice fields like state
                and payment_form.is_valid()
                and cross_form_fdedits()
            ):
            # Process the forms taking care of proper creation order for FKs
            customer = customer_form.save (commit=False)
            customer.user = user
            ses ['customer'] = customer
            #customer.save()
            if trace: print 'POST CUSTOMER', customer.id, user.id, customer.user_id

            order = order_form.save (commit=False)


            # TODO: compute shipping based on weight, boxes, etc!
            multiplier = 1.0      # TODO: get this from table, above
            #multiplier = totqty  # no - (totqty is the # of line items)

            if order.shipping_payment == "included":
                # Note to Mani & developers: What we should do here is:
                # - ensure the totweight is calculated correctly, including the quantity of each line item
                # - (check also how quotes work too, and ensure the cart is being filled correctly)
                # - in the future, also includ # of packages in the calcualtion, as shipping of each box has a separate cost
                # We could also look into a shipping calcualtion add-on for Django
                shipping = seshelp.cart_summary() ['totweight'] * multiplier
            else:
                shipping = 0.0

            if order.california_tax:
                tax = grandtot * float (order.california_tax.tax)/100
            else:
                tax = 0.0

            order.tax = tax
            order.shipping = str(shipping)
            order.customer_id = customer.id
            order.status = 'new'
            order.cart = cart

            shipping_address = shipping_address_form.save (commit=False)
            shipping_address.customer_id = customer.id

            if billing_address_form.is_empty():  # not has_changed():
                shipping_address.type = 'both'
                ses ['shipping_address'] = shipping_address
                #shipping_address.save()
                #print shipping_address.id
                order.bill_to_address_id = shipping_address.id
                billing_address = shipping_address
                billing_name = customer.name()
            else:
                shipping_address.type = 'shipping'
                ses ['shipping_address'] = shipping_address
                #shipping_address.save()
                billing_address = billing_address_form.save (commit=False)
                billing_address.customer_id = customer.id
                billing_address.type = 'billing'
                ses ['billing_address'] = billing_address
                #billing_address.save()
                #print billing_address.id
                order.bill_to_address_id = billing_address.id
                if not billing_address.name:
                    billing_address.name = customer.name()
                billing_name = billing_address.name

            order.ship_to_address_id = shipping_address.id
            ses ['billing_name'] = billing_name
            ses ['order'] = order
            #order.save()

            payment = payment_form.save(commit=False)
            payment.user = user
            #print order.id, order.customer_id
            payment.order_id = order.id
            payment.last_4 = payment_form.cleaned_data ['card_number'] [-4:]
            payment.payment_terms = payment.payment_method [2:]
            ses ['payment'] = payment
            ses ['payment_data'] = payment_form.cleaned_data
            #payment.save()

            gtts = grandtot + tax + shipping
            ses ['gtts'] = gtts

            if confirm:  # display conf form
                return render_to_response ('confirm.html', dict (
                    breadcrumbs     = ( Breadcrumb ('Checkout', 'Checkout - Enter info', '/checkout/'),
                                        Breadcrumb ('Confirm', 'Confirm your order', '/checkout/confirm/'),
                                      ),
                    ses         = ses,
                    seshelp     = seshelp,
                    summary     = seshelp.cart_summary_table(), # JJW 8/7/15 for consistency in new theme templates
                    grandtot    = grandtot,
                    totqty      = totqty,
                    tax         = tax,
                    shipping    = shipping,
                    gtts        = gtts,
                    order       = order,
                    payment     = payment
                ), context_instance=RequestContext(request))

    else:  # it's a GET, fill in the inital form
        #customer = user.customer_set.all() [:1] or []
        customer = Customer.objects.filter(user=user)

        if customer:
            address_obj = Address.objects.filter(customer=customer)
            user_form                = UserForm (instance=user)
            customer                 = customer [0]

            if trace: print 'CUSTOMER', customer.id, user.id, customer.user_id
            customer_form            = CustomerForm (instance=customer)
            ses ['customer']         = customer
            order_form               = OrderForm()
            if address_obj:
                shipping_address_form    = AddressForm (prefix='shipping', instance=customer.default_shipping())
            else:
                shipping_address_form    = AddressForm (prefix='shipping', instance=customer.default_shipping(),initial={'name': customer.user.first_name + ' ' + customer.user.last_name})
            #shipping_address_form    = AddressForm (prefix='shipping', instance=customer.default_shipping(),initial={'name': customer.user.first_name})
            billing_address_form     = BillingAddressForm (prefix='billing', instance=customer.default_billing())
            ses ['shipping_address'] = customer.default_shipping()
            ses ['billing_address']  = customer.default_billing()
            payment_form             = PaymentForm ()
        else:
            if trace: print 'USER', user, user.id
            user_form               = UserForm()
            customer_form           = CustomerForm (initial = dict (email=user.email))
            order_form              = OrderForm()
            shipping_address_form   = AddressForm (prefix='shipping')
            billing_address_form    = BillingAddressForm (prefix='billing')
            payment_form            = PaymentForm()

        user_form.header += ' (User: '******')'

    return render_to_response ('checkout.html', dict (
            breadcrumbs     = (Breadcrumb ('Checkout', 'Checkout', '/checkout/'),),
            formlist        = (user_form, customer_form, order_form, shipping_address_form, billing_address_form, payment_form),
            totqty          = totqty,
            grandtot        = grandtot,
            summary         = seshelp.cart_summary_table(), # JJW 8/7/15 for consistency in new theme templates
        ), context_instance=RequestContext(request))