Mercurial > hg > MPIWGFMManager
comparison MPIWGFMItem.py @ 0:957bcf42f206
initial
author | dwinter |
---|---|
date | Thu, 02 May 2013 08:33:53 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:957bcf42f206 |
---|---|
1 from AccessControl import ClassSecurityInfo | |
2 from OFS.Folder import Folder | |
3 from OFS.PropertyManager import PropertyManager | |
4 import OFS.Image | |
5 from Products.PageTemplates.PageTemplateFile import PageTemplateFile | |
6 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate | |
7 from ZPublisher.Converters import type_converters | |
8 | |
9 import Acquisition | |
10 | |
11 import datetime | |
12 import random | |
13 import logging | |
14 | |
15 from OFS.Cache import Cacheable | |
16 | |
17 NS ="{http://www.filemaker.com/xml/fmresultset}" | |
18 | |
19 class MPIWGFMItem(Folder, PropertyManager, Cacheable): | |
20 """ | |
21 | |
22 """ | |
23 | |
24 meta_type = 'MPIWGItem' | |
25 title = "" | |
26 | |
27 _properties = ({'id':'title', 'type':'string'},) | |
28 | |
29 manage_options = ( | |
30 {'label':'Edit', 'action':'manage_editItemForm'}, | |
31 ) + Folder.manage_options + ( | |
32 {'label':'Edit Item', 'action':'manage_editMPIWGItemForm'}, | |
33 ) | |
34 | |
35 #def __init__(self,id, date=None, cat_en="", cat_de="", title_en="", title_de=""): | |
36 | |
37 def findText(self,record,xpath): | |
38 | |
39 tmp = record.find(xpath) | |
40 | |
41 if tmp is not None: | |
42 return unicode(tmp[0].text) | |
43 else: | |
44 return "" | |
45 | |
46 def __init__(self,record,folder): | |
47 self.id = id | |
48 #self.Category = (cat_en,cat_de) | |
49 | |
50 xpathstr = ".//"+NS+"field[@name='%s']" | |
51 | |
52 | |
53 title_de = self.findText(record,xpathstr%"Titel") | |
54 | |
55 title_en = self.findText(record,xpathstr%"EN_Titel") | |
56 | |
57 | |
58 self.Title = (title_en,title_de) | |
59 | |
60 | |
61 date = self.findText(record,xpathstr%"TagderVeranstaltung") | |
62 | |
63 | |
64 if date == "" or date is None: | |
65 date = datetime.date.today().strftime("%d.%m.%Y") | |
66 | |
67 | |
68 | |
69 self.Date = self.parseDate(date) # sorting date | |
70 | |
71 self.Properties = [] # List of 6 Tuples (ID_EN,ID_DE,VALUE_EN,VALUE_DE, WEIGHT,ID) | |
72 # Note: ID_EN is the identifier for the getter-methods | |
73 | |
74 self.title = title_en | |
75 | |
76 self.getDataFromRecord(record, folder) | |
77 | |
78 def index_html(self): | |
79 """index method""" | |
80 # default template is called item_main | |
81 pt = getattr(self, 'item_main', None) | |
82 if pt is not None: | |
83 return pt() | |
84 | |
85 # else try parent | |
86 if hasattr(self.aq_parent, 'index_html'): | |
87 return self.aq_parent.index_html() | |
88 | |
89 | |
90 def parseDate(self, date): | |
91 # parse the date from a DD.MM.YYYY string | |
92 | |
93 try: | |
94 digits = [int(x) for x in date.split(".")] | |
95 return datetime.date(digits[2],digits[1],digits[0]) | |
96 except: #try MM/TT/YYYY | |
97 digits = [int(x) for x in date.split("/")] | |
98 return datetime.date(digits[2],digits[0],digits[1]) | |
99 | |
100 def getDate(self): | |
101 """ return the date """ | |
102 return self.Date.strftime("%d.%m.%Y") | |
103 | |
104 def getProperty(self, id): | |
105 for prop in self.Properties: | |
106 if len(prop)==5: # add ID to the old format | |
107 prop = (prop[0],prop[1],prop[2],prop[3],prop[4],prop[0]) | |
108 if prop[5].lower() == id.lower(): | |
109 return prop | |
110 #if id_en == "category": | |
111 # return ("Category","Kategorie",self.Category[0],self.Category[1],0) | |
112 if id == "title": | |
113 return ("Title", "Titel", self.Title[0], self.Title[1],0,"title") | |
114 if id == "date": | |
115 return ("Date", "Datum", self.Date.strftime("%d.%m.%Y"), self.Date.strftime("%d.%m.%Y"),0,"date") | |
116 return None | |
117 | |
118 | |
119 #def getUrl(self, baseUrl=None): | |
120 # """returns URL to this Department""" | |
121 # if baseUrl is None: | |
122 # return self.absolute_url() | |
123 # | |
124 # return '%s/%s'%(baseUrl, self.getId()) | |
125 | |
126 def getImageIds(self, count=0): | |
127 """ return the ids of images inside this MPIWGItem-object | |
128 not implemented for DB""" | |
129 #ids = self.objectIds(spec="Image") | |
130 #if len(ids) > 1: | |
131 # if count > 0: | |
132 # ids = ids[:count] | |
133 # else: | |
134 # ids = ids[:] | |
135 # ids.sort() | |
136 | |
137 return [] | |
138 | |
139 def getImageUrls(self, count=0, baseUrl=None): | |
140 """ return the urls of images inside this MPIWGItem-object - not implemented for DB""" | |
141 #ids = self.getImageIds(count=count) | |
142 #if baseUrl is None: | |
143 # baseUrl = self.absolute_url() | |
144 | |
145 #return ['%s/%s'%(baseUrl,id) for id in ids] | |
146 return [] | |
147 def getImageUrl(self, idx=0, baseUrl=None): | |
148 """ return the url of an image inside this MPIWGItem-object -not impelemted for DB""" | |
149 #ids = self.getImageIds(count=idx+1) | |
150 #if len(ids) < idx+1: | |
151 # return None | |
152 # | |
153 #if baseUrl is None: | |
154 # baseUrl = self.absolute_url() | |
155 # | |
156 #return '%s/%s'%(baseUrl,ids[idx]) | |
157 return "" | |
158 | |
159 def getFileIds(self, count=0): | |
160 """ return the ids of files inside this MPIWGItem-object --not impelemted for DB""" | |
161 #ids = self.objectIds(spec="File") | |
162 #if len(ids) > 1: | |
163 # if count > 0: | |
164 # ids = ids[:count] | |
165 | |
166 #else: | |
167 # ids = ids[:] | |
168 #ids.sort() | |
169 | |
170 #return ids | |
171 return[] | |
172 | |
173 def getFileUrls(self, baseUrl=None, count=0): | |
174 """ return the urls of files inside this MPIWGItem-object -not impelemted for DB""" | |
175 # ids = self.getFileIds(count=count) | |
176 # if baseUrl is None: | |
177 # baseUrl = self.absolute_url() | |
178 # | |
179 # return ['%s/%s'%(baseUrl,id) for id in ids] | |
180 return [] | |
181 | |
182 def getPropertyKeys(self): | |
183 """ """ | |
184 return [x[5] for x in self.Properties if len(x)==6]+[x[0] for x in self.Properties if len(x)==5] | |
185 | |
186 def getWeight(self, index): | |
187 """ """ | |
188 return self.Properties[index][4] | |
189 | |
190 | |
191 def getByPrefix(self, prefix): | |
192 """ returns all items with the same prefix in the ID """ | |
193 return [ prop for prop in self.Properties if prop[5].find(prefix)==0 ] | |
194 | |
195 | |
196 def getValue(self, id, lang="EN"): | |
197 """ """ | |
198 prop = self.getProperty(id) | |
199 if prop==None: | |
200 return None | |
201 if lang=="DE": | |
202 return prop[3] | |
203 else: | |
204 return prop[2] | |
205 | |
206 getInfo = getValue # compatibility | |
207 | |
208 def getCaption(self, id, lang="EN"): | |
209 """ """ | |
210 prop = self.getProperty(id) | |
211 if prop==None: | |
212 return None | |
213 if lang=="DE": | |
214 return prop[1] | |
215 else: | |
216 return prop[0] | |
217 | |
218 | |
219 def getItems(self, lang="EN", excepts=[]): | |
220 """ returns all the key/value pairs """ | |
221 | |
222 if lang=="DE": | |
223 return [(prop[1],prop[3]) for prop in self.Properties if not prop[0].lower() in excepts] | |
224 else: | |
225 return [(prop[0],prop[2]) for prop in self.Properties if not prop[0].lower() in excepts] | |
226 | |
227 getAllInfo = getItems # compatibility | |
228 | |
229 def addProperty(self, tup=("","","","",0,"")): | |
230 """ """ | |
231 self.Properties.append(tup) | |
232 # force update of the Properties list in the ZopeDB | |
233 self._p_changed = 1 | |
234 | |
235 | |
236 | |
237 def setProperty(self, tup): | |
238 """ set the data of a property; tup is a tuple, structured like the properties """ | |
239 # find the property | |
240 num = -1 | |
241 for i in range(len(self.Properties)): | |
242 if self.Properties[i][5] == tup[5]: | |
243 num = i | |
244 | |
245 # List of 6 Tuples (ID_EN,ID_DE,VALUE_EN,VALUE_DE, WEIGHT,ID) | |
246 # update it | |
247 if num != -1: | |
248 self.Properties[num] = tup | |
249 # force update of the Properties list in the ZopeDB | |
250 self._p_changed = 1 | |
251 | |
252 | |
253 def delProperty(self, id): | |
254 """ remove a property """ | |
255 num = -1 | |
256 for i in range(len(self.Properties)): | |
257 if self.Properties[i][5] == id: | |
258 del self.Properties[i] | |
259 # force update of the Properties list in the ZopeDB | |
260 self._p_changed = 1 | |
261 | |
262 return | |
263 | |
264 def getNextGroupPropertyIndex(self, prefix): | |
265 """ returns the logical next id for a property with a certain 'prefix' | |
266 e.g. if there are properties 'dl_1' and 'dl_2' and the prefix is 'dl_', then the method will yield 'dl_3'. | |
267 """ | |
268 oldproperties = [x[5] for x in self.Properties if x[5].find(prefix)==0 and x[5].split(prefix)[1].isdigit()] | |
269 oldproperties.sort() | |
270 if len(oldproperties)>0: | |
271 lastparam = int(oldproperties[-1].split(prefix)[1]) | |
272 return prefix+str(lastparam+1) | |
273 else: | |
274 return prefix+"1" | |
275 | |
276 def getHighestGroupPropertyWeight(self, prefix): | |
277 """ """ | |
278 propWeights = [x[4] for x in self.Properties if x[5].find(prefix)==0] | |
279 if len(propWeights)>0: | |
280 return max(propWeights) | |
281 else: | |
282 return 0 | |
283 | |
284 def switchPropertyWeights(self, prop1, prop2): | |
285 """ """ | |
286 p1 = list(self.getProperty(prop1)) | |
287 p2 = list(self.getProperty(prop2)) | |
288 | |
289 w1 = p1[4] | |
290 w2 = p2[4] | |
291 | |
292 p1[4] = w2 | |
293 p2[4] = w1 | |
294 | |
295 self.setProperty(tuple(p1)) | |
296 self.setProperty(tuple(p2)) | |
297 | |
298 self._p_changed = 1 | |
299 | |
300 def setTitle(self, title_en, title_de=""): | |
301 """ set the title """ | |
302 self.Title = (title_en, title_de) | |
303 # force update of the Properties list in the ZopeDB | |
304 self._p_changed = 1 | |
305 | |
306 def setDate(self, dateString, dateObject=None): | |
307 """ set the date """ | |
308 if dateObject is not None: | |
309 self.Date = dateObject | |
310 else: | |
311 self.Date = self.parseDate(dateString) | |
312 | |
313 # force update of the Properties list in the ZopeDB | |
314 self._p_changed = 1 | |
315 | |
316 | |
317 def deleteObject(self,id): | |
318 """ delete an object inside the MPIWGItem """ | |
319 if self.hasObject(id): | |
320 self._delObject(id) | |
321 | |
322 | |
323 def manage_addImage(self, id, file, title='', redirect_url=None, precondition='', content_type='', REQUEST=None): | |
324 """ | |
325 Add a new Image object. | |
326 Creates a new Image object 'id' with the contents of 'file'. | |
327 """ | |
328 # we use OFS/Image's method without the request | |
329 OFS.Image.manage_addImage(self, id, file, title=title, precondition=precondition, content_type=content_type) | |
330 | |
331 # and do our own redirect | |
332 if REQUEST is not None: | |
333 if redirect_url is None: | |
334 redirect_url = 'manage_editItemForm?rnd=%s'%''.join(random.sample("abcdefuvwxyz",8)) | |
335 | |
336 REQUEST.RESPONSE.redirect(redirect_url) | |
337 | |
338 manage_addImageForm = OFS.Image.manage_addImageForm | |
339 | |
340 def manage_addFile(self, id, file, title='', redirect_url=None, precondition='', content_type='', REQUEST=None): | |
341 """ | |
342 Add a new File object. | |
343 Creates a new File object 'id' with the contents of 'file'. | |
344 """ | |
345 # we use OFS/Image's method without the request | |
346 OFS.Image.manage_addFile(self, id, file, title=title, precondition=precondition, content_type=content_type) | |
347 | |
348 # and do our own redirect | |
349 if REQUEST is not None: | |
350 if redirect_url is None: | |
351 redirect_url = 'manage_editItemForm?rnd=%s'%''.join(random.sample("abcdefuvwxyz",8)) | |
352 | |
353 REQUEST.RESPONSE.redirect(redirect_url) | |
354 | |
355 manage_addFileForm = OFS.Image.manage_addFileForm | |
356 | |
357 | |
358 | |
359 def manage_editItemForm(self, REQUEST=None): | |
360 """edit item form""" | |
361 # default template is called edit_item_form | |
362 pt = getattr(self, 'edit_item_form', None) | |
363 if pt is not None: | |
364 try: | |
365 return pt() | |
366 except Exception, e: | |
367 logging.error("Problem with edit_item_form: "+str(e)) | |
368 | |
369 # else use builtin template | |
370 return self.manage_editMPIWGItemForm() | |
371 | |
372 | |
373 | |
374 | |
375 | |
376 | |
377 def getDataFromRecord(self,record,folder): | |
378 | |
379 # get predefined properties | |
380 predef = folder.Properties[:] | |
381 | |
382 | |
383 | |
384 for prop in predef: | |
385 self.addProperty(prop) | |
386 | |
387 index =0 | |
388 for property in self.getPropertyKeys(): | |
389 prop = self.Properties[index] # hole alten wert | |
390 | |
391 en_value=self.getValueFromRecord(record,property,'en') | |
392 de_value=self.getValueFromRecord(record,property,'de') | |
393 self.Properties[index]=(prop[0],prop[1],en_value,de_value,prop[4],prop[5]) #vordefinierte werte lassen ausser fuer die werte | |
394 | |
395 | |
396 index+=1 | |
397 | |
398 transform={'Category':'Reihentitel', | |
399 'Time':'Datum als Text','Presentation':'Titel','Address':'Ort','Notes':'Bemerkungen', | |
400 'Organizers':'Veranstaltet von','Presenter':'Vortragende Person'} | |
401 | |
402 def getValueFromRecord(self,record,propertyID,lang): | |
403 | |
404 fieldName = "" | |
405 if lang == "en": | |
406 fieldName="EN_" | |
407 | |
408 | |
409 #fieldName+=self.transform[propertyID] | |
410 fieldName+=propertyID | |
411 | |
412 xpathstr = ".//"+NS+"field[@name='%s']"%fieldName | |
413 | |
414 val = self.findText(record,xpathstr) | |
415 | |
416 if val == "": # leer versuche die andere sprache | |
417 if lang == "en": | |
418 fieldName="" | |
419 else: | |
420 fieldName="EN_" | |
421 | |
422 #fieldName+=self.transform[propertyID] | |
423 fieldName+=propertyID | |
424 xpathstr = ".//"+NS+"field[@name='%s']"%fieldName | |
425 | |
426 val = self.findText(record,xpathstr) | |
427 | |
428 return val | |
429 | |
430 def manage_editMPIWGItem(self, formdata=None, REQUEST=None): | |
431 """ """ | |
432 if REQUEST is not None: | |
433 formdata = REQUEST.form | |
434 | |
435 # update property names and values and weights | |
436 # date,title,category | |
437 try: | |
438 self.Date = self.parseDate(formdata['date']) | |
439 self.Title = (formdata["title_en"],formdata["title_de"]) | |
440 self.title = formdata["title_en"] | |
441 except: | |
442 pass | |
443 #self.Category = (formdata["category_en"],formdata["category_de"]) | |
444 # custom data | |
445 property_ids = [int(x.split('_')[1]) for x in formdata.keys() if x.endswith('_id')] | |
446 for pid in property_ids: | |
447 if pid < len(self.Properties): | |
448 self.Properties[pid] = (formdata["property_%s_en_key"%pid], | |
449 formdata["property_%s_de_key"%pid], | |
450 formdata["property_%s_en_value"%pid], | |
451 formdata["property_%s_de_value"%pid], | |
452 formdata["property_%s_weight"%pid], | |
453 formdata["property_%s_id"%pid]) | |
454 | |
455 # delete properties | |
456 # check if properties need to be removed | |
457 # e.g. "del__property_01" | |
458 del_keys = [int(x.split('_')[-1]) for x in formdata.keys() if x.find("del__") == 0 and formdata[x] == "on"] | |
459 del_keys.sort() | |
460 del_keys.reverse() | |
461 for k in del_keys: | |
462 del self.Properties[k] | |
463 | |
464 # delete images | |
465 del_keys = [x.split('__')[1] for x in formdata.keys() if x.find("delimg__") == 0 and formdata[x] == "on"] | |
466 for k in del_keys: | |
467 self.deleteObject(k) | |
468 | |
469 # delete files | |
470 del_keys = [x.split('__')[1] for x in formdata.keys() if x.find("delfil__") == 0 and formdata[x] == "on"] | |
471 for k in del_keys: | |
472 self.deleteObject(k) | |
473 | |
474 # sort | |
475 try: | |
476 self.Properties.sort(lambda a,b: cmp(int(a[4]),int(b[4])) ) | |
477 except: | |
478 pass | |
479 | |
480 try: | |
481 # add new properties (empty) | |
482 add_new = int(formdata['add_new']) | |
483 | |
484 for x in range(add_new): | |
485 self.addProperty() | |
486 except: | |
487 pass | |
488 | |
489 # force update of the Properties list in the ZopeDB | |
490 self._p_changed = 1 | |
491 self.en.index_html.ZCacheable_invalidate() | |
492 | |
493 if REQUEST is not None: | |
494 REQUEST.RESPONSE.redirect('manage_editItemForm?rnd=%s'%''.join(random.sample("abcdefuvwxyz",8))) | |
495 | |
496 | |
497 manage_editMPIWGItemForm = PageTemplateFile('zpt/editMPIWGItem.pt', globals()) | |
498 | |
499 | |
500 def manage_addMPIWGItem(self,REQUEST=None): | |
501 """ create a new MPIWGItem """ | |
502 | |
503 id = self.getNewID() | |
504 | |
505 # get predefined properties | |
506 predef = self.Properties[:] | |
507 | |
508 newinst = MPIWGItem(id) | |
509 | |
510 for prop in predef: | |
511 newinst.addProperty(prop) | |
512 | |
513 self._setObject(id, newinst) | |
514 | |
515 if REQUEST is not None: | |
516 REQUEST.RESPONSE.redirect('%s/manage_editItemForm'%id) | |
517 | |
518 return id | |
519 | |
520 def manage_addMPIWGItemForm(self, REQUEST): | |
521 """ """ | |
522 manage_addMPIWGItem(self,REQUEST) |