Merge branch 'dd_ordering' into zope-2.13
[Portfolio.git] / skins / add_to_cart.py
1 ##parameters=
2 from Products.CMFCore.utils import getToolByName
3 from Products.Portfolio.utils import translate
4 from Products.photoprint.exceptions import SoldOutError, CartLockedError
5 def _(message) : return translate(message, context).encode('utf-8')
6
7 form = context.REQUEST.form
8 ajax = form.get('ajax')
9 if ajax :
10 context.REQUEST.RESPONSE.setHeader('Content-Type', 'text/xml;;charset=utf-8')
11
12 atool = getToolByName(context, 'portal_actions')
13
14 quantity = form.get('quantity')
15
16 msg = ''
17 try :
18 quantity = int(quantity)
19 except ValueError :
20 msg = _('You must enter an integer for quantity (found: %s)') % quantity
21
22 if quantity <= 0 :
23 msg = _('You must enter a positive value for quantity (found: %s)') % quantity
24
25 if msg :
26 if ajax :
27 return "<error>%s</error>" % msg
28 else :
29 context.setStatus(False, msg)
30 return context.setRedirect(atool, 'user/panier')
31
32 sd = context.session_data_manager.getSessionData(create = 1)
33
34 from Products.CMFCore.utils import getToolByName
35 uidh = getToolByName(context, 'portal_uidhandler')
36 item = {'cmf_uid':form['cmf_uid']
37 ,'printing_template':form['printing_template']
38 ,'quantity':quantity
39 }
40 photo = uidh.getBrain(item['cmf_uid'])
41
42 from Products.photoprint.cart import PrintCart
43 cart = sd.get('cart', PrintCart())
44 try :
45 cart.append(context, item)
46 except SoldOutError, e :
47 n = e.n
48 if n > 1 :
49 msg = _("Only %d available copies of this photo in this size.") % n
50 elif n == 1 :
51 msg = _("Only one last available copy of this photo in this size.")
52 else :
53 msg = _("No more available copy of this photo in this size.")
54
55 if ajax :
56 return "<error>%s</error>" % msg
57 else :
58 context.setStatus(False, msg)
59 return context.setRedirect(atool, 'user/panier')
60
61 except CartLockedError :
62 msg = _("Your cart is locked:\nplease complete your current order first.")
63 if ajax :
64 return "<error>%s</error>" % msg
65 else :
66 context.setStatus(False, msg)
67 return context.setRedirect(atool, 'user/panier')
68
69 sd['cart'] = cart
70
71 if ajax :
72 return '<confirm duration="2">%s</confirm>' % _('Added to cart.')
73 else :
74 context.setStatus(False, _('Added to cart.'))
75 return context.setRedirect(atool, 'user/panier')