root/trunk/djedna/store/views.py

Revision 409, 6.4 kB (checked in by thomas, 11 months ago)

First check in of initial store functionality

Line 
1 # (c) Copyright 2008 Thomas Bohmbach, Jr.
2 #
3 # This file is part of DJ Edna.
4 #
5 # DJ Edna is free software: you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
9 #
10 # DJ Edna is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # DJ Edna.  If not, see <http://www.gnu.org/licenses/>.
17
18 import decimal
19 import logging as log
20
21 from django.conf import settings
22 from django.contrib.auth.decorators import login_required
23 from django.contrib.auth.models import User
24 from django.core.urlresolvers import reverse
25 from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound, HttpResponseForbidden, Http404, HttpResponseServerError
26 from django.template import loader, Context, RequestContext
27 from django.shortcuts import render_to_response, get_object_or_404
28 from django.views.generic.list_detail import object_list, object_detail
29
30 from forms import create_price_form, create_price_forms
31 from models import UserAccessPermitProduct
32
33
34 def product_list(request, content_object=None):
35     cart = None
36     if request.user and request.user.is_authenticated():
37         cart = request.user.get_profile().cart
38     if content_object:
39         products = UserAccessPermitProduct.objects.get_products(user, content_object)
40     else:
41         products = UserAccessPermitProduct.objects.all()
42     query_set = UserAccessPermitProduct.objects.filter(id__in=[product.id for product in products if product.is_valid()])
43     return object_list(request,
44                        query_set,
45                        template_name='store/product_list.html',
46                        template_object_name='product',
47                        extra_context={'cart' : cart})
48
49 def product_detail(request, product_id):
50     cart = None
51     if request.user and request.user.is_authenticated():
52         cart = request.user.get_profile().cart
53     query_set = UserAccessPermitProduct.objects.all()
54     return object_detail(request,
55                          query_set,
56                          object_id=product_id,
57                          template_name='store/product_detail.html',
58                          template_object_name='product',
59                          extra_context={'cart' : cart})
60
61 #TODO: Get the cart from a utility method that checks the session if the user isn't logged in
62 #TODO: When a user registers, their session cart should be assigned to that user
63 #TODO: When a user logs in, if they have a session cart it should replace (merge with) their user cart
64 @login_required
65 def buy_product_form(request, product_id, next_page=None):
66     product = get_object_or_404(UserAccessPermitProduct, pk=product_id)
67     cart = request.user.get_profile().cart
68     next_page = next_page or request.REQUEST.get('next_page', '') or product.content_object.get_absolute_url()
69     if request.method == 'POST':
70         form = create_price_form(product, data=request.POST)
71         if form.is_valid():
72             price = form.final_price
73             cart.add(product, price)
74             return HttpResponseRedirect(next_page)
75     else:
76         form = create_price_form(product)
77     return render_to_response('store/buy_product_form.html',
78                               {'next_page' : next_page, 'form' : form, 'product' : product, 'cart' : cart},
79                               context_instance=RequestContext(request))
80
81 @login_required
82 def buy_products_form(request, products=[], next_page=None):
83     cart = request.user.get_profile().cart
84     if not products:
85         all_products = UserAccessPermitProduct.objects.all()
86         products = UserAccessPermitProduct.objects.filter(
87             id__in=[product.id for product in all_products if (product.is_valid() and not cart.contains_product(product))]
88         )
89     next_page = next_page or request.REQUEST.get('next_page', request.get_full_path())
90     product_forms = []
91     if request.method == 'POST':
92         #We don't want to validate all the forms yet, so just create blank ones
93         product_forms, selected_form = create_price_forms(products)
94         for product, form in product_forms:
95             #Did the user hit the AddToCart button for this form and is the form valid?
96             if request.POST.get('%s-add_to_cart' % form.prefix, None):
97                 #Now that we know which product was selected, create the forms, only validating that one
98                 product_forms, selected_form = create_price_forms(products, data=request.POST, selected=product)
99                 if selected_form.is_valid():
100                     cart.add(product, selected_form.final_price)
101                     return HttpResponseRedirect(next_page)
102     else:
103         product_forms, selected_form = create_price_forms(products)
104     return render_to_response('store/buy_products_form.html',
105                               {'next_page' : next_page, 'product_forms' : product_forms, 'cart' : cart},
106                               context_instance=RequestContext(request))
107    
108
109 @login_required
110 def buy_user_access_permit(request, product_id, price):
111     product = get_object_or_404(UserAccessPermitProduct, pk=product_id)
112     price = decimal.Decimal(price)
113     cart = request.user.get_profile().cart
114     cart.add(product, price)
115     return render_to_response('store/cart_add.html',
116                               {'cart' : cart,
117                                'product' : product},
118                               context_instance=RequestContext(request))
119
120 @login_required
121 def remove_from_cart(request, next_page=None):
122     log.info("next_page=%s" % next_page)
123     next_page = next_page or request.REQUEST.get('next_page', request.get_full_path())
124     cart = request.user.get_profile().cart
125     for cart_item in cart.items.all():
126         if request.POST.get('%s-remove_from_cart' % cart_item.id, None):
127             cart.remove(cart_item.product)
128     return HttpResponseRedirect(next_page)
129
130 @login_required
131 def cart_clear(request):
132     cart = request.user.get_profile().cart
133     cart.clear()
134
135 @login_required
136 def checkout(request):
137     pass
138
139 @login_required
140 def review(request):
141     pass
142
143 @login_required
144 def purchase(request):
145     cart = request.user.get_profile().cart
146     cart.purchase()
147
Note: See TracBrowser for help on using the browser.