jslint
[Portfolio.git] / skins / my_cart.py
1 ##parameters=order='', shipping='', set_shipping=''
2 from Products.CMFCore.utils import getToolByName
3 from Products.Portfolio.utils import translate
4 from Products.photoprint.exceptions import SoldOutError
5 from Products.photoprint.cart import PrintCart
6 from Products.photoprint.price import Price
7
8 def _(message) : return translate(message, context).encode('utf-8')
9
10 options = {}
11 session = context.REQUEST.SESSION
12 sg = session.get
13 cart = sg('cart', PrintCart())
14 utool = getToolByName(context, 'portal_url')
15 portal_url = utool()
16 portal = utool.getPortalObject()
17 uidh = portal.portal_uidhandler
18 pptool = portal.portal_photo_print
19 mtool = portal.portal_membership
20 isAnon = mtool.isAnonymousUser()
21 form = context.REQUEST.form
22 nextStep = None
23
24 if cart.locked :
25 pendingOrder = context.restrictedTraverse(cart.pendingOrderPath)
26 return context.setRedirect(pendingOrder, 'object/view', ajax=form.get('ajax'))
27
28 if order :
29 if isAnon :
30 atool = portal.portal_actions
31 came_from = '%s/my_cart' % portal_url
32 return context.setRedirect(atool, 'customer/login', came_from=came_from, ajax=form.get('ajax'))
33
34 if shipping :
35 member = mtool.getAuthenticatedMember()
36 options['shipping_fullname'] = member.getMemberFullName(nameBefore=0)
37 options['shipping_address'] = member.getProperty('billing_address')
38 options['shipping_city'] = member.getProperty('billing_city')
39 options['shipping_zipcode'] = member.getProperty('billing_zipcode')
40 options['shipping_country'] = member.getProperty('country')
41 return context.shipping_template(**options)
42
43 if set_shipping :
44 fg = lambda name : form.get(name,'').strip()
45 if context.shipping_set_control(**form) :
46 order = pptool.addPrintOrder(cart)
47 return context.setRedirect(order, 'object/view', ajax=fg('ajax'))
48 else :
49 for name in [ 'shipping_fullname'
50 , 'shipping_address'
51 , 'shipping_city'
52 , 'shipping_zipcode'
53 , 'shipping_country'] :
54 options[name] = fg(name)
55 return context.shipping_template(**options)
56
57 if isAnon :
58 nextStep = {'name':'order', 'value' : _('Order >>')}
59 else :
60 nextStep = {'name':'shipping', 'value' : _('Shipping >>')}
61
62
63 msg = ''
64
65 for k, v in form.items() :
66 if hasattr(v, 'items') :
67 uid, templateId = k.split('_',1)
68 if v.has_key('refresh') :
69 quantity = v['quantity']
70 try :
71 quantity = int(quantity)
72 except ValueError:
73 msg = _('You must enter an integer for quantity (found: %s)') % quantity
74 if quantity <= 0 :
75 msg = _('You must enter a positive value for quantity (found: %s)') % quantity
76 if not msg :
77 item = {'cmf_uid' : uid
78 ,'printing_template' : templateId
79 ,'quantity' : quantity}
80 try :
81 cart.update(context, item)
82 except SoldOutError, e :
83 n = e.n
84 if n > 1 :
85 msg = _("Only %d available copies of this photo in this size.") % n
86 elif n == 1 :
87 msg = _("Only one last available copy of this photo in this size.")
88 else :
89 msg = _("No more available copy of this photo and in this size.")
90
91
92 if v.has_key('delete'):
93 cart.remove(context, uid, templateId)
94
95 options['empty'] = not cart
96 infos = []
97 prices = []
98 quantityTotal = 0
99 for item in cart :
100 b = uidh.getBrain(item['cmf_uid'])
101 poptions = pptool.getPrintingOptionsContainerFor(b.getObject())
102 pt = getattr(poptions, item['printing_template'])
103 size = b.getThumbnailSize
104 d = {'thumbUrl' : '%s/getThumbnail' % b.getURL()
105 ,'thumbHeight' : size['height'] / 2
106 ,'thumbWidth' : size['width'] / 2
107 ,'alt' : ('%s - %s' % (b.Title, b.Description)).strip(' -')
108 ,'cmf_uid':b.cmf_uid
109 ,'title': pt.title
110 ,'description': pt.description
111 ,'quantity':item['quantity']
112 ,'amount': '%s €' % (pt.price * item['quantity']).taxed
113 ,'templateId': pt.getId()
114 }
115 quantityTotal += item['quantity']
116 prices.append(pt.price * item['quantity'])
117 infos.append(d)
118
119 options['infos'] = infos
120 if len(prices) == 1:
121 pricesTotal = prices[0]
122 elif len(prices) > 1 :
123 pricesTotal = reduce(lambda a, b : a + b, prices)
124
125 if prices :
126 options['pricesTotal'] = pricesTotal
127 options['quantityTotal'] = quantityTotal
128 shippingFees = pptool.getShippingFeesFor(price=pricesTotal)
129 options['shippingFees'] = shippingFees
130 options['totalAmount'] = pricesTotal + shippingFees
131
132 breadcrumbs = [
133 { 'id' : 'root'
134 , 'title' : portal.title
135 , 'url' : portal_url},
136
137 {'id' : 'my_cart'
138 ,'title' : _('My cart')
139 , 'url' : '%s/my_cart' % portal_url}
140 ]
141
142 options['breadcrumbs'] = breadcrumbs
143
144 if msg :
145 context.REQUEST.other['portal_status_message'] = msg
146 options['cartIsOrder'] = False
147 options['nextStep'] = nextStep
148 return context.my_cart_template(**options)