-
Notifications
You must be signed in to change notification settings - Fork 2
/
views.py
118 lines (104 loc) · 3.65 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import logging
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from store.models import FandomHierarchy, Image, Media, Pattern, Item, ImageItem
from store.itemfilters import ApplyPredicates
from django.conf import settings
import copy
from django.db import connection
from store.search import doSearch
from decorators import render_to
from forms import SearchForm, ItemForm
from cart import Cart
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
@render_to('index.html')
def frontpage(request):
# add_related_count(FandomHierarchy.objects.all(), Image, 'foo', 'sub_images', cumulative=True)
return {'breadcrumbs': [[{'url': '/', 'text': 'Home'}]]}
def payment(request, result):
if result == "success":
return render_to_response('thanks.html', {'breadcrumbs': [[{'url': '/', 'text': 'Home'}, {'url': '/thanks', 'text': 'Thank you for your payment!'}]]})
return frontpage(request)
@render_to('by_category.html')
def filtered(request, predicate):
patterns, path = ApplyPredicates(predicate, Pattern.objects.all().select_related('image__name'))
bc = [[{'url': '/', 'text': 'Home'}]]
for crumb in path:
bc[0].append({'url': '/filter/fandom=%d-%d' % (crumb.lft, crumb.rght), 'text': crumb.name})
children = path.reverse()[0].get_children()
for crumb in children:
bc.append([{'url': '/filter/fandom=%d-%d' % (crumb.lft, crumb.rght), 'text': crumb.name}])
page = request.GET.get('page')
p = Paginator(patterns, 40)
try:
patterns = p.page(page)
except PageNotAnInteger:
patterns = p.page(1)
except EmptyPage:
patterns = p.page(p.num_pages)
return {'patterns': patterns, 'breadcrumbs': bc}
def findImageScaling(w, h):
goalWidth, goalHeight = 400, 400
imageScale = max(min(int(goalWidth / w), int(goalHeight / h)), 1)
return { "width": w * imageScale, "height": h * imageScale }
@render_to('pattern.html')
def pattern_page(request, pattern_id):
pattern = get_object_or_404(Pattern, pk=pattern_id)
return {
'pattern': pattern,
'scale': findImageScaling(pattern.pixel_width, pattern.pixel_height),
'media': Media.objects.all(),
'breadcrumbs': [[{'url': '/', 'text': 'Home'}]],
'carthelper': ItemForm(pattern_id=pattern_id),
}
@render_to('search_results.html')
def search_results(request):
if request.method == 'GET':
form = SearchForm(request.GET)
if form.is_valid():
results = doSearch(form.cleaned_data['query'])
return {
'query': form.cleaned_data['query'],
'results': results,
}
return {}
def add_to_cart(request):
form = ItemForm(request.POST or None)
if form.is_valid():
cart = Cart(request)
row = ImageItem(
pattern=form.cleaned_data['pattern_id'],
media=form.cleaned_data['media_option'],
media_orientation=form.cleaned_data['media_orientation'],
extra_text='',
special_instructions=''
)
row.save()
cart.add(row, form.cleaned_data['media_option'].price_cents / 100, 1)
else:
logging.debug("Invalid form!")
logging.debug(request)
logging.debug(dir(request))
# fixme: redirect to wherever we came from?
return HttpResponseRedirect(reverse('show_cart'))
def remove_from_cart(request, item_id):
cart = Cart(request)
item = Item.objects.get(pk=item_id, cart=cart.cart)
if item: item.delete()
return HttpResponseRedirect(reverse('show_cart'))
@render_to('cart.html')
def show_cart(request):
return { 'cart': Cart(request) }
@render_to('checkout_success.html')
def checkout_success(request):
#FIXME
return {}
@render_to('checkout_canceled.html')
def checkout_canceled(request):
#FIXME
return {}
@render_to('order_status.html')
def order_status(request):
#FIXME
return {}