Suppression des photos d'une table lumineuse par ajax.
[Portfolio.git] / skins / lightboxcontext.py
1 ##parameters=
2 from Products.CMFCore.utils import getToolByName
3 from Products.Portfolio.utils import translate
4 def _(message) : return translate(message, context).encode('utf-8')
5 options = {}
6
7 uidh = getToolByName(context, 'portal_uidhandler')
8 utool = getToolByName(context, 'portal_url')
9 atool = getToolByName(context, 'portal_actions')
10 pptool = getToolByName(context, 'portal_photo_print', None)
11 portal_url = utool()
12 lightboxUrl = context.absolute_url()
13 portal = utool.getPortalObject()
14 portalDepth = len(portal.getPhysicalPath())
15 req = context.REQUEST
16 toUrl = req.physicalPathToURL
17 resp = req.RESPONSE
18 selDict = req.SESSION.get('objects_selection_dict', {})
19 bsize = portal.getProperty('default_batch_size', 20)
20
21 if traverse_subpath[-1] == 'photo_view_ajax' :
22 photoSubPath = traverse_subpath[:-1]
23 ajax = True
24 else :
25 photoSubPath = traverse_subpath
26 ajax = False
27
28 photo = portal.restrictedTraverse(photoSubPath)
29 photouid = uidh.register(photo)
30
31 lightboxUids = context.getUidList()
32
33 if pptool :
34 buyable = bool(pptool.getPrintingOptionsFor(photo))
35 sd = context.session_data_manager.getSessionData(create=1)
36 cart = sd.get('cart', None)
37 if cart and cart.locked :
38 buyable = False
39 else :
40 buyable = False
41 options['buyable'] = buyable
42
43 infos = []
44 posOfPhoto = 0
45
46 if ajax == True :
47 try : posOfPhoto = lightboxUids.index(photouid)
48 except ValueError : pass
49 options['backToContextUrl'] = '%s?b_start:int=%s' % (lightboxUrl, posOfPhoto/bsize*bsize)
50 relPhotoPath = '/'.join(photo.getPhysicalPath()[portalDepth:])
51 lastBcUrl = '%s/lightboxcontext/%s' % (lightboxUrl, relPhotoPath)
52 options['lastBcUrl'] = lastBcUrl
53 app = context.restrictedTraverse('/')
54 meth = app.restrictedTraverse(photo.getPhysicalPath() + ('photo_view_ajax_template',))
55 return meth(**options)
56
57
58 for i, uid in enumerate(lightboxUids) :
59 b = uidh.getBrain(uid)
60 size = b.getThumbnailSize
61 size = {'width':int(size['width']/2.0), 'height':int(size['height']/2.0)}
62
63 className = selDict.has_key(uid) and 'selected' or ''
64 if uid == photouid :
65 className = ('%s displayed' % className).lstrip()
66 posOfPhoto = i
67
68 relPhotoPath = '/'.join(b.getPath().split('/')[portalDepth:])
69 href = '%s/lightboxcontext/%s' % (lightboxUrl, relPhotoPath)
70
71 d = {'src': '%s/getThumbnail' % b.getURL()
72 ,'href': href
73 ,'thumbSize': size
74 ,'title' : b.Description
75 ,'className': className
76 ,'displayed' : uid == photouid
77 , 'index': i
78 }
79 infos.append(d)
80
81 if posOfPhoto > 0 :
82 previous = infos[posOfPhoto - 1]['href']
83 else :
84 previous = infos[0]['href']
85
86 if posOfPhoto < len(infos) -1 :
87 next = infos[posOfPhoto + 1]['href']
88 else :
89 next = infos[-1]['href']
90
91 contextInfos = {'infos':infos,
92 'isSelected': selDict.has_key(photouid),
93 'backUrl' : '%s?b_start:int=%s' % (lightboxUrl, posOfPhoto/bsize*bsize),
94 'index' : posOfPhoto,
95 'previous' : previous,
96 'next' : next,
97 'reBaseCtxUrl':'/^%s/' % ('%s/lightboxcontext/' % lightboxUrl).replace('/', '\/'),
98 'canonicalUrl': "'%s/'" % portal_url}
99
100 options['contextInfos'] = contextInfos
101
102 # breadcrumbs customization
103 breadcrumbs = context.breadcrumbs()
104 breadcrumbs.append(
105 {'id' : photo.getId()
106 ,'title' : photo.title_or_id()
107 , 'url' : req.ACTUAL_URL}
108 )
109
110 options['breadcrumbs'] = breadcrumbs
111
112
113 ti = photo.getTypeInfo()
114 method_id = ti.queryMethodID('view', context=photo)
115 app = context.restrictedTraverse('/')
116 meth = app.restrictedTraverse(photo.getPhysicalPath() + (method_id,))
117 return meth(req, resp, **options)