Ménage, lint.
[Portfolio.git] / Products / Portfolio / skins / selectioncontext.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 selectionUrl = atool.getActionInfo('user/selection')['url']
13 portal = utool.getPortalObject()
14 portalDepth = len(portal.getPhysicalPath())
15 req = context.REQUEST
16 toUrl = req.physicalPathToURL
17 resp = req.RESPONSE
18 bsize = portal.getProperty('default_batch_size', 20)
19
20 if traverse_subpath[-1] == 'photo_view_ajax' :
21 photoSubPath = traverse_subpath[:-1]
22 ajax = True
23 else :
24 photoSubPath = traverse_subpath
25 ajax = False
26
27 photo = portal.restrictedTraverse(photoSubPath)
28 photouid = uidh.register(photo)
29
30 lightboxpath = req.SESSION.get('lightboxpath', None)
31 selectionIsLightbox = False
32 if lightboxpath is None :
33 selection = req.SESSION.get('objects_selection', [])
34 else :
35 try :
36 lightbox = portal.restrictedTraverse(lightboxpath)
37 selection = lightbox.getUidList()
38 selectionIsLightbox = True
39 except:
40 req.SESSION.set('lightboxpath', None)
41 selection = req.SESSION.get('objects_selection', [])
42
43 if pptool :
44 buyable = bool(pptool.getPrintingOptionsFor(photo))
45 sd = context.session_data_manager.getSessionData(create=1)
46 cart = sd.get('cart', None)
47 if cart and cart.locked :
48 buyable = False
49 else :
50 buyable = False
51 options['buyable'] = buyable
52
53 infos = []
54 posOfPhoto = 0
55
56 if ajax == True :
57 try : posOfPhoto = selection.index(photouid)
58 except ValueError : pass
59 options['backToContextUrl'] = '%s?b_start:int=%s' % (selectionUrl, posOfPhoto/bsize*bsize)
60 photoPath = list(photo.getPhysicalPath())
61 photoPath.insert(portalDepth, 'selectioncontext')
62 options['lastBcUrl'] = toUrl(photoPath)
63 app = context.restrictedTraverse('/')
64 meth = app.restrictedTraverse(photo.getPhysicalPath() + ('photo_view_ajax_template',))
65 return meth(req, resp, **options)
66
67
68 for i, uid in enumerate(selection) :
69 b = uidh.getBrain(uid)
70 size = b.getThumbnailSize
71 size = {'width':int(size['width']/2.0), 'height':int(size['height']/2.0)}
72
73 if uid == photouid :
74 className = 'selected displayed'
75 posOfPhoto = i
76 else :
77 className = 'selected'
78
79 path = b.getPath().split('/')
80 path.insert(portalDepth, 'selectioncontext')
81
82 d = {'src': '%s/getThumbnail' % b.getURL()
83 ,'href': toUrl(path)
84 ,'thumbSize': size
85 ,'title' : b.Description
86 ,'className' : className
87 ,'displayed' : uid == photouid
88 , 'index': i
89 }
90 infos.append(d)
91
92 if posOfPhoto > 0 :
93 previous = infos[posOfPhoto - 1]['href']
94 else :
95 previous = infos[0]['href']
96
97 if posOfPhoto < len(infos) -1 :
98 next = infos[posOfPhoto + 1]['href']
99 else :
100 next = infos[-1]['href']
101
102
103 contextInfos = {'infos':infos,
104 'isSelected':True,
105 'backUrl' : '%s?b_start:int=%s' % (selectionUrl, posOfPhoto/bsize*bsize),
106 'index' : posOfPhoto,
107 'previous' : previous,
108 'next' : next,
109 'reBaseCtxUrl':'/^%s/' % (portal_url+'/selectioncontext/').replace('/', '\/'),
110 'canonicalUrl': "'%s/'" % portal_url}
111
112 options['contextInfos'] = contextInfos
113
114 # breadcrumbs customization
115 if selectionIsLightbox :
116 lastBcTitle = '%s (%s)' % (_('My selection'), lightbox.title_or_id())
117 else :
118 lastBcTitle = _('My selection')
119
120 breadcrumbs = [
121 { 'id' : 'root'
122 , 'title' : portal.title
123 , 'url' : portal_url},
124
125 {'id' : 'selection_view'
126 ,'title' : lastBcTitle
127 , 'url' : selectionUrl},
128
129 {'id' : photo.getId()
130 ,'title' : photo.title_or_id()
131 , 'url' : req.ACTUAL_URL}
132 ]
133
134 options['breadcrumbs'] = breadcrumbs
135
136
137 ti = photo.getTypeInfo()
138 method_id = ti.queryMethodID('view', context=photo)
139 app = context.restrictedTraverse('/')
140 meth = app.restrictedTraverse(photo.getPhysicalPath() + (method_id,))
141 return meth(req, resp, **options)