Début de refactoring du javascript pour rendre l'upload non spécifique à la photo.
[Plinn.git] / skins / photo_theme / folder_contents.py
1 ##parameters=b_start=0, key='', reverse=0, ids=(), items_copy='', items_cut='', items_delete='', items_new='', items_paste='', items_rename='', items_up='', items_down='', items_top='', items_bottom='', items_sort='', template='', macro='', ajax=''
2 ##
3 from Products.Plinn.PloneMisc import Batch
4 from DateTime import DateTime
5 locale_date_fmt = context.locale_date_fmt()
6 from ZTUtils import make_query
7 from Products.CMFCore.utils import getToolByName
8 from Products.CMFDefault.permissions import AddPortalContent
9 from Products.CMFDefault.permissions import DeleteObjects
10 from Products.CMFDefault.permissions import ListFolderContents
11 from Products.CMFDefault.permissions import ManageProperties
12 from Products.CMFDefault.permissions import ViewManagementScreens
13 from Products.CMFDefault.permissions import ModifyPortalContent
14 from Products.CMFDefault.utils import html_marshal
15
16 mtool = getToolByName(script, 'portal_membership')
17 checkPermission = mtool.checkPermission
18 utool = getToolByName(script, 'portal_url')
19 portal_url = utool()
20
21
22 form = context.REQUEST.form
23 default_target = 'object/folderContents'
24 default_kw = {'b_start': b_start, 'key': key, 'reverse': reverse, 'ajax' : ajax}
25 if items_copy :
26 if ajax : default_kw['syncFragments']=['rightCell']
27 if context.validateItemIds(**form) and \
28 context.folder_copy_control(**form) and \
29 context.setRedirect(context, default_target, **default_kw):
30 return
31 elif items_cut :
32 if ajax : default_kw['syncFragments']=['rightCell']
33 if context.validateItemIds(**form) and \
34 context.folder_cut_control(**form) and \
35 context.setRedirect(context, default_target, **default_kw):
36 return
37 elif items_delete and \
38 context.validateItemIds(**form) and \
39 context.folder_delete_control(**form) and \
40 context.setRedirect(context, default_target, **default_kw):
41 return
42 elif items_new and \
43 context.setRedirect(context, 'object/new', **default_kw):
44 return
45 elif items_paste :
46 if ajax : default_kw['syncFragments']=['rightCell']
47 if context.folder_paste_control(**form) and \
48 context.setRedirect(context, default_target, **default_kw):
49 return
50 elif items_rename and \
51 context.validateItemIds(**form) and \
52 context.setRedirect(context, 'object/rename_items', ids=ids,
53 **default_kw):
54 return
55 elif items_sort and \
56 context.folder_sort_control(**form) and \
57 context.setRedirect(context, default_target, b_start=b_start):
58 return
59 elif items_up and \
60 context.validateItemIds(**form) and \
61 context.folder_up_control(**form) and \
62 context.setRedirect(context, default_target, **default_kw):
63 return
64 elif items_down and \
65 context.validateItemIds(**form) and \
66 context.folder_down_control(**form) and \
67 context.setRedirect(context, default_target, **default_kw):
68 return
69 elif items_top and \
70 context.validateItemIds(**form) and \
71 context.folder_top_control(**form) and \
72 context.setRedirect(context, default_target, **default_kw):
73 return
74 elif items_bottom and \
75 context.validateItemIds(**form) and \
76 context.folder_bottom_control(**form) and \
77 context.setRedirect(context, default_target, **default_kw):
78 return
79
80
81 options = {}
82
83 items_add_allowed = checkPermission(AddPortalContent, context)
84 upitems_list_allowed = checkPermission(ListFolderContents, context, 'aq_parent')
85 manage_props_allowed = checkPermission(ManageProperties, context)
86
87 target = context.getActionInfo(default_target)['url']
88
89 if not key:
90 (key, reverse) = context.getDefaultSorting()
91 is_default = 1
92 elif (key, reverse) == context.getDefaultSorting():
93 is_default = 1
94 else:
95 is_default = 0
96
97 columns = ( {'key': 'Lock',
98 'title': '',
99 'width': '16',
100 'colspan': None}
101 , {'key': 'Type',
102 'title': 'Type',
103 'width': None,
104 'colspan': '2'}
105 , {'key': 'id',
106 'title': 'Name',
107 'width': None,
108 'colspan': None}
109 , {'key': 'modified',
110 'title': 'Last Modified',
111 'width': None,
112 'colspan': None}
113 , {'key': 'position',
114 'title': 'Position',
115 'width': None,
116 'colspan': None }
117 )
118
119 for column in columns:
120 images = []
121 if key == column['key'] :
122 if not is_default and manage_props_allowed :
123 images.append( {'src' : getattr(context, 'set_default_sorting.gif').absolute_url(),
124 'alt' : 'Set Sorting as Default',
125 'id' : 'SetSortingAsDefault',
126 'href': '%s?%s' % (target, make_query(items_sort=True,
127 key=key,
128 reverse= (key != 'position' and [reverse] or [False])[0] )
129 )
130 }
131 )
132
133 if key != 'position' :
134 if reverse :
135 toggleImg = getattr(context, 'arrowDown.gif')
136 alt = 'descending sort'
137 else :
138 toggleImg = getattr(context, 'arrowUp.gif')
139 alt = 'ascending sort'
140 query = make_query(key=column['key'], reverse = not reverse)
141 else :
142 toggleImg = getattr(context, 'arrowUp.gif')
143 alt = 'ascending sort'
144 query = make_query(key=column['key'])
145 images.append( {'src' : toggleImg.absolute_url(), 'alt' : alt} )
146 else :
147 if key != 'position' :
148 query = make_query(key=column['key'], reverse = reverse)
149 else :
150 query = make_query(key=column['key'])
151
152 column['url'] = '%s?%s' % (target, query)
153 column['images'] = images
154
155 context.filterCookie()
156 folderfilter = context.REQUEST.get('folderfilter', '')
157 filter = context.decodeFolderFilter(folderfilter)
158 items = context.listCatalogedContents(contentFilter=filter)
159 sort_dir = reverse and 'desc' or 'asc'
160 sortFunc = key in ['Type'] and 'nocase' or 'cmp'
161 items = sequence.sort( items, ((key, sortFunc, sort_dir),) )
162 batch_obj = Batch(items, context.default_batch_size, b_start, orphan=0, quantumleap=1)
163 items = []
164 i = 1
165 display_delete_button = True # TODO : à revoirs
166 for item in batch_obj:
167 item_icon = item.getIcon
168 item_id = item.getId
169 item_position = key == 'position' and str(b_start + i) or '...'
170 i += 1
171 item_url = item.getURL()
172 #try : item_delete_allowed = context.objectIdCanBeDeleted(item_id)
173 #except : item_delete_allowed = checkPermission(DeleteObjects, context) # std zope perm
174 #if not display_delete_button :
175 # display_delete_button = item_delete_allowed
176 items.append(
177 {'lock' : False,
178 'checkbox': True,
179 'icon': item_icon and ( '%s/%s' % (portal_url, item_icon) ) or '',
180 'id': item_id,
181 'modified': item.modified.strftime(locale_date_fmt),
182 'position': item_position,
183 'title_or_id': item.Title or item_id,
184 'type': item.Type or None,
185 'url': item_url } )
186
187 options['batch'] = { 'listColumnInfos': tuple(columns),
188 'listItemInfos': tuple(items),
189 'sort_key' : key,
190 'sort_dir' : sort_dir,
191 'batch_obj': batch_obj }
192
193 hidden_vars = []
194 for name, value in html_marshal(**default_kw):
195 hidden_vars.append( {'name': name, 'value': value} )
196
197 # buttons
198 buttons = []
199 if items_add_allowed and context.allowedContentTypes():
200 buttons.append( {'name': 'items_new', 'value': 'New...'} )
201 if items:
202 buttons.append( {'name': 'items_rename', 'value': 'Rename'} )
203
204 if checkPermission(ViewManagementScreens, context) and items:
205 buttons.append( {'name': 'items_cut', 'value': 'Cut'} )
206 buttons.append( {'name': 'items_copy', 'value': 'Copy'} )
207
208 if items_add_allowed and context.cb_dataValid():
209 buttons.append( {'name': 'items_paste', 'value': 'Paste'} )
210
211 if display_delete_button and items:
212 buttons.append( {'name': 'items_delete', 'value': 'Delete'} )
213
214 length = batch_obj.sequence_length
215 is_orderable = manage_props_allowed and (key == 'position') and length > 1
216 is_sortable = manage_props_allowed and not is_default
217 options['form'] = { 'action': target,
218 'listHiddenVarInfos': tuple(hidden_vars),
219 'listButtonInfos': tuple(buttons),
220 'is_orderable': is_orderable,
221 'is_sortable': is_sortable }
222 if not ajax and is_orderable :
223 deltas = range( 1, min(5, length) ) + range(5, length, 5)
224 options['form']['listDeltas'] = tuple(deltas)
225
226 if template and macro :
227 options['template'] = template
228 options['macro'] = macro
229 return context.use_macro(**options)
230 else :
231 return context.folder_contents_template(**options)
232