Ticket #6071: utils.py

File utils.py, 33.8 KB (added by hans.rauch@…, 17 years ago)
Line 
1#-*-coding: utf-8 -*-
2"""
3/dms/utils.py
4
5.. enthaelt Hilfefunktionen fuer
6 Django content Management System
7
8Hans Rauch
9
10hans.rauch@gmx.net
11
12Die Programme des dms-Systems koennen frei genutzt und den spezifischen
13Beduerfnissen entsprechend angepasst werden.
14
150.01 13.01.2007 Beginn der Dokumentation
160.02 16.01.2007 check_name
170.03 20.1.2007 base_site_url
180.04 04.02.2007 bei Vortraegen wird der Titel in der breadcrum angezeigt
190.05 01.03.2007 send_control_email
200.06 17.05.2007 show_link - info=''
210.07 01.06.2007 get_folderish_actions ueberarbeitet
220.08 10.09.2007 add_if_app_available
230.09 02.10.2007 manage_site_mode, manage_domain_mode
240.10 03.10.2007 get_site_actions
250.11 17.10.2007 get_link_by_item_container fuer Redirects in Lernarchiven angepasst
260.12 23.10.2007 get_item_container_data_object_by_id
270.13 31.10.2007 PROJECT_FOLDER
280.14 10.11.2007 is_protected
29"""
30
31import string
32import datetime
33
34from django.utils.translation import ugettext as _
35
36from math import floor
37from datetime import date
38
39from django.utils.translation import ugettext as _
40
41from django.utils.encoding import smart_unicode
42from django import newforms as forms
43
44from django.utils.translation import ugettext as _
45
46from dms.settings import DOWNLOAD_URL
47from dms.settings import MY_DOMAINS
48
49from dms.queries import get_top_item
50from dms.queries import get_parent_item_container
51from dms.queries import get_menuitems_navmenu_top
52from dms.queries import get_menuitems_navmenu_left
53from dms.queries import is_file_by_item_container
54from dms.queries import get_prev_parent_item_containers
55from dms.queries import get_next_parent_item_containers
56from dms.queries import get_licenses
57from dms.queries import get_site_url
58from dms.queries import get_base_site_url
59from dms.queries import get_faecher
60from dms.queries import get_schularten
61from dms.queries import is_app_available
62from dms.queries import get_item_container_data_object_by_id
63from dms.queries import is_protected
64
65from dms.encode_decode import decode_html
66from dms.encode_decode import encode_html
67
68from dms.text_icons import FOLDER_ICON, PROJECT_ICON
69from dms.text_icons import EXTERN_ICON, NEW_WINDOW_ICON, FILE_DETAIL, REDIRECT_ICON
70
71# -----------------------------------------------------
72# E-Mail-Adressen verschluesseln
73
74# Created by jmeile
75# Anpassung an zecom: Hans Rauch, 3.12.2002, 04.01-2006
76
77def encode_email(emailvalue, namevalue=None,rClass=None,info=None):
78 #Taken from http://www.happysnax.com.au/testemail.php
79 #Original comments:
80 '''this function creates the hexadecimal equivalent to
81 "document.write('<a href="mailto:emailaddress">name</a>')"
82 ie it effectively encrpts this as far as spam-bots are concerned
83 the Javascript to unencrypt it simply changes the hex back into ascii
84 then executes the code using the 'eval' statement
85 /... and ta da ... you've got a normal mailto address displayed in the browser.
86 //Written by Jeff Robson of Cynergic Net Solutions www.cynergic.net jeff.robson@cynergic.net'''
87 if emailvalue == '' :
88 return namevalue
89 if namevalue == None :
90 namevalue = emailvalue
91 if rClass != None :
92 cClass = ' class="' + rClass + '"'
93 else :
94 cClass = ''
95 if info != None :
96 cInfo = ' title="' + info + '"'
97 else :
98 cInfo = ''
99 new_text = hex_string ( 'document.write(\'<a' + cClass + ' href=\"mailto:'+emailvalue+'\"' + \
100 cInfo + '>'+namevalue+'</a>\')' )
101 return '<script type=\"text/javascript\">eval(unescape(\''+new_text+'\'))</script>'
102
103def hex_string(mystring):
104 #Taken from http://www.happysnax.com.au/testemail.php
105 '''Written by Jeff Robson of Cynergic Net Solutions www.cynergic.net jeff.robson@cynergic.net'''
106 newstring=''
107 length=len(mystring)
108 for i in range(length):
109 newstring=newstring+'%'+tohex(ord(mystring[i]))
110 return newstring
111
112def tohex(n):
113 #Taken from http://www.happysnax.com.au/testemail.php
114 '''Written by Jeff Robson of Cynergic Net Solutions www.cynergic.net jeff.robson@cynergic.net'''
115 hs='0123456789ABCDEF'
116 return hs[int(floor(n/16))]+hs[n%16]
117
118# -----------------------------------------------------
119def get_footer_email(object, use_string_1=False):
120 """ liefert Namen und Adresse des Autors """
121 if smart_unicode(str(type(object))) == "<class 'dms.models.DmsItem'>":
122 item = object
123 else:
124 item = object.item
125 try:
126 my_ext_item = get_ext_item(item)
127 return encode_email(my_ext_item.owner_email, my_ext_item.owner, 'navLink')
128 except:
129 if use_string_1:
130 return encode_email(item.string_2, item.string_1, 'navLink')
131 else:
132 return encode_email(item.owner.email, item.owner.get_full_name(), 'navLink')
133
134def get_author_email(author, email):
135 """ liefert Namen und Adresse des Autors """
136 return encode_email(email, author, 'nav')
137
138# -----------------------------------------------------
139def get_tabbed_form(rFormItems, rFormHelp, rHelpName, rFormData,
140 do_tab=True, tab_cluster={}, valign=True, max_cols=1,
141 show_errors=True):
142 """ baut Tabbed-Formulare zusammen: Quelle ??? """
143 from dms.form_system import form_system
144 from django.template.loader import get_template
145 from django.template import Context
146
147 t = get_template('utils/yui_tab_base.html')
148 content = ''
149 headers = []
150 tabs = []
151 tab_id = 'tab_'
152 tab_no = 0
153 for item in rFormItems:
154 this_tab_id = tab_id + str(tab_no)
155 tab_no += 1
156 headers.append ( {'tab_id': this_tab_id,
157 'text': rFormHelp[item[0]]['title'],
158 'selected': tab_no == 1
159 } )
160 tabs.append ( {'tab_id': this_tab_id,
161 'info': rFormHelp[item[0]]['info'],
162 'content': form_system().get_form(item[1], rFormHelp,
163 rHelpName, rFormData,
164 tab_cluster, valign, max_cols, show_errors)
165 } )
166 c = Context ( {'headers': headers, 'tabs': tabs } )
167 return t.render(c)
168
169# -----------------------------------------------------
170def info_slot_to_header(text) :
171 """ wandelt rechten Infoslot in HTML fuer WYSIWYG-Editor um """
172 HEADER_START = '<!-- header start -->'
173 HEADER_END = '<!-- header end -->'
174 INFO_START = '<!-- info start -->'
175 INFO_END = '<!-- info end -->'
176 ret = ''
177 nStart = 0
178 nHStart = string.find(text, HEADER_START, nStart)
179 while nHStart >= 0 :
180 nHEnd = string.find ( text, HEADER_END, nHStart )
181 header = text[nHStart+len(HEADER_START):nHEnd]
182 nIStart = string.find ( text, INFO_START, nHEnd )
183 nIEnd = string.find ( text, INFO_END, nIStart )
184 info = text[nIStart+len(INFO_START):nIEnd]
185 ret += '<h4>'+header+'</h4>\n'
186 ret += info + '\n'
187 nHStart = string.find(text, HEADER_START, nIEnd)
188 return ret
189
190# -----------------------------------------------------
191def get_section_choices(sections):
192 """ wandelt Textzeilen in Liste um """
193 ret = []
194 sections = string.splitfields(decode_html(sections), '\n')
195 for s in sections :
196 s = string.strip(s)
197 ret.append((encode_html(s), s))
198 return ret
199 # if s != '':
200 # yield(encode_html(s), s)
201
202# -----------------------------------------------------
203def get_parent_section_choices(my_item_container):
204 item_container = get_parent_item_container(my_item_container)
205 return get_section_choices(item_container.container.sections)
206
207# -----------------------------------------------------
208def get_license_choices(my_item_container):
209 #return get_section_choices(get_licenses())
210 ret = []
211 licenses = get_licenses()
212 for lic in licenses:
213 if lic.url != '':
214 l = show_link(lic.url, lic.name)
215 else:
216 l = lic.name
217 ret.append((lic.id, l))
218 return ret
219
220# -----------------------------------------------------
221def get_fach_choices():
222 # --- Liste aller Faecher
223 ret = []
224 ret.append((-1, '---'))
225 faecher = get_faecher()
226 for fach in faecher:
227 ret.append((fach.id, decode_html(fach.name)))
228 return ret
229
230# -----------------------------------------------------
231def get_schulart_choices():
232 # --- Liste aller Faecher
233 ret = []
234 ret.append((-1, '---'))
235 schularten = get_schularten()
236 for schulart in schularten:
237 ret.append((schulart.id, decode_html(schulart.name)))
238 return ret
239
240# -----------------------------------------------------
241def check_name( name, is_name_ok):
242 """ entfernt Umlaute etc. aus Dateinamen """
243 try:
244 name = unicode(name, 'utf-8')
245 except:
246 pass
247 name = name[max(string.rfind(name,'/'),
248 string.rfind(name,'\\'),
249 string.rfind(name,':')
250 )+1:]
251 name = string.replace(name, u"'", u'_')
252 name = string.replace(name, u'ä', u'ae')
253 name = string.replace(name, u'ö', u'oe')
254 name = string.replace(name, u'ü', u'ue')
255 name = string.replace(name, u'Ä', u'Ae')
256 name = string.replace(name, u'Ö', u'Oe')
257 name = string.replace(name, u'Ü', u'Ue')
258 name = string.replace(name, u'ß', u'ss')
259 bad_chars = ' ,;()[]{}*"#%+~!'
260 good_chars = '________________'
261 TRANSMAP = string.maketrans(bad_chars, good_chars)
262 name = name.encode('iso-8859-1')
263 name = string.translate(name, TRANSMAP)
264 if is_name_ok:
265 return name
266 html = '.html'
267 if name[-5:] != html :
268 name += html
269 return name
270
271# -----------------------------------------------------
272def show_link(url, info='', url_extern=False, url_class='', title=''):
273 """ zeigt Verweise an - externe Verweise werden optisch gekennzeichnet """
274 if info == '':
275 if url.startswith('http://'):
276 info += url[7:]
277 else:
278 info = url
279 from dms.settings import MY_DOMAINS
280 from django.template.loader import get_template
281 from django.template import Context
282 t = get_template('utils/show_link.html')
283 target = ''
284 start = ''
285 is_my_domain = False
286 # --- Parameter in URL ausschliessen
287 n_pos = url.find('?')
288 if n_pos > -1:
289 u = url[:n_pos]
290 else:
291 u = url
292 if u.startswith('http://'):
293 for domain in MY_DOMAINS:
294 if u.find(domain) >= 0 :
295 is_my_domain = True
296 break
297 if is_my_domain :
298 if url_extern :
299 target = '_extern'
300 start = NEW_WINDOW_ICON
301 else :
302 target = '_extern'
303 start = EXTERN_ICON
304 else :
305 # Javascript - das Oeffnen in einem eigenen Fenster erfolgt ueber Javascript
306 #target = '_extern'
307 start = NEW_WINDOW_ICON
308 if url_class != '' and not url_class.startswith('class='):
309 url_class = u' class="%s"' % url_class
310 if title != '' and not title.startswith('title='):
311 title = u' title="%s"' % title
312 c = Context ( {'start': start, 'url' : url, 'class': url_class,
313 'target': target, 'info': info, 'title': title } )
314 return t.render(c)
315
316# -----------------------------------------------------
317def get_link_by_item_container(item_container, is_folder=False):
318 """ liefert die entsprechende URL """
319 d_extern = ''
320 d_info = ''
321 d_title = item_container.item.title
322 if item_container.item.app.is_folderish or is_folder:
323 #d_class = 'class="folderLink" '
324 postfix = FOLDER_ICON
325 else :
326 #d_class = ''
327 postfix = ''
328 # --- handelt es sich um ein Datei- oder Ordner-Objekt?
329 is_file = is_file_by_item_container(item_container)
330 is_text = item_container.item.app.name == 'dmsText'
331 if string.find(item_container.item.name, '.html') > 0 or is_file or is_text:
332 if item_container.item.app.name == 'dmsRedirect':
333 d_url = item_container.item.url_more
334 if string.find(item_container.item.url_more, 'http://') >= 0:
335 is_my_domain = False
336 for domain in MY_DOMAINS:
337 if string.find(item_container.item.url_more, domain) >= 0:
338 is_my_domain = True
339 break
340 if is_my_domain:
341 if item_container.item.url_more_extern:
342 d_extern = '_extern'
343 d_extern_icon = NEW_WINDOW_ICON
344 else :
345 d_extern = '_extern'
346 d_extern_icon = EXTERN_ICON
347 elif is_file:
348 d_extern = '_extern'
349 d_extern_icon = NEW_WINDOW_ICON
350 d_info = FILE_DETAIL % get_site_url(item_container, item_container.item.name + '/show/')
351 d_url = DOWNLOAD_URL + item_container.container.path + item_container.item.name
352 else:
353 d_url = get_site_url(item_container, item_container.item.name)
354 else :
355 if item_container.is_data_object:
356 d_url = get_site_url(item_container, 'index.html')
357 else:
358 real_item_container = get_item_container_data_object_by_id(item_container.item.id)
359 if real_item_container == None:
360 d_url = get_site_url(item_container, 'index.html')
361 else:
362 d_url = get_site_url(real_item_container[0], 'index.html')
363 if d_extern:
364 help = _('Wird in einem eigenen Fenster ge&ouml;ffnet ...')
365 this_link = d_extern_icon + u'<a href="%s" target="d_extern" title="%s">%s</a>' % \
366 (d_url, help, d_title)
367 elif item_container.item.app.name == 'dmsRedirect' or not item_container.is_data_object:
368 s_info = REDIRECT_ICON + '&nbsp;'
369 help = _('Springt zu einer anderen Stelle ...')
370 this_link = s_info + u'<a href="%s" title="%s">%s</a>' % (d_url, help, d_title)
371 else:
372 this_link = u'<a href="%s">%s</a>' % (d_url, d_title)
373 if is_protected(item_container):
374 return '<i>' + this_link + d_info + '</i>' + PROJECT_ICON
375 return this_link + d_info + postfix
376
377# -----------------------------------------------------
378def show_more ( url, url_extern, info='Mehr ...' ) :
379 """ erzeugt einen Verweis fuer "mehr ..." """
380 return show_link ( url, info, url_extern )
381
382# -----------------------------------------------------
383def get_breadcrumb(item_container, text_only=False, ignore_own_breadcrumb=False):
384 """ Navigationszeile zusammenbauen """
385 if text_only:
386 ret = ''
387 length = len(item_container.container.site.base_folder)
388 n = 0 # --- Begrenzung der Schachtelungstiefe
389 if not item_container.item.app.is_folderish:
390 item_container = get_parent_item_container(item_container)
391 while item_container != None \
392 and (ignore_own_breadcrumb or \
393 not item_container.item.app.has_own_breadcrumb) \
394 and not item_container.container.is_top_folder \
395 and item_container.parent_item_id != -1 \
396 and n < 20:
397 ret = item_container.container.nav_title + ' | ' + ret
398 n += 1
399 if n >= 20:
400 assert False
401 item_container = get_parent_item_container(item_container)
402 return ret
403 else:
404 from django.template.loader import get_template
405 from django.template import Context
406 t = get_template('utils/nav_item.html')
407 ret = ''
408 length = len(item_container.container.site.base_folder)
409 n = 0 # --- Begrenzung der Schachtelungstiefe
410 if not item_container.item.app.is_folderish:
411 item_container = get_parent_item_container(item_container)
412 while item_container != None \
413 and (ignore_own_breadcrumb or \
414 not item_container.item.app.has_own_breadcrumb) \
415 and not item_container.container.is_top_folder \
416 and item_container.parent_item_id != -1 \
417 and n < 20:
418 c = Context( {'url' : item_container.container.site.url+\
419 item_container.container.path[length:]+'index.html',
420 'title': item_container.item.title,
421 'info' : item_container.container.nav_title,})
422 ret = t.render(c) + ret
423 n += 1
424 if n >= 20:
425 assert False
426 item_container = get_parent_item_container(item_container)
427 if item_container != None:
428 if item_container.item.app.has_own_breadcrumb :
429 c=Context({'url' : get_site_url(item_container, 'index.html'),
430 'title': item_container.item.title,
431 'info' : item_container.container.nav_title,})
432 ret = t.render(c) + ret
433 parent_item_container = get_parent_item_container(item_container)
434 c=Context({'url' : parent_item_container.get_absolute_url(),
435 'title': _('Zur&uuml;ck zu den normalen Web-Seiten'),
436 'info' : _('Ausgang'),})
437 elif item_container.container.is_top_folder:
438 c=Context({'url' : get_site_url(item_container, 'index.html'),
439 'title': item_container.item.title,
440 'info' : item_container.container.nav_title,})
441 ret = t.render(c) + ret
442 item_container = get_top_item()
443 c=Context({'url' : get_site_url(item_container, 'index.html'),
444 'title': item_container.item.title,
445 'info' : item_container.container.nav_title,})
446 else:
447 c=Context({'url' : get_site_url(item_container, 'index.html'),
448 'title': item_container.item.title,
449 'info' : item_container.container.nav_title,})
450 ret = t.render(c) + ret
451 return ret
452 else:
453 return 'item_container == None!!!'
454
455# -----------------------------------------------------
456def get_prev_next(item_container):
457 """ Geschwisterseiten """
458
459 def get_string(line, max_char=30):
460 if len(line) < max_char:
461 return line
462 return line[:max_char] + ' ...'
463
464 items = get_prev_parent_item_containers(item_container)
465 p_len = len(items)
466 if p_len == 0:
467 prev_url = ''
468 prev_info = ''
469 else:
470 p_item = items[p_len-1].item
471 prev_url = p_item.name
472 prev_info = get_string(p_item.title)
473 items = get_next_parent_item_containers(item_container)
474 if len(items) >0:
475 next_url = items[0].item.name
476 next_info = get_string(items[0].item.title)
477 else:
478 next_url = ''
479 next_info = ''
480 return prev_url, prev_info, next_url, next_info
481
482# -----------------------------------------------------
483def get_prev_next_line(item_container):
484 """ Geschwisterseiten anzeigen """
485 from django.template.loader import get_template
486 from django.template import Context
487 t = get_template('utils/prev_next.html')
488 prev_url, prev_info, next_url, next_info = get_prev_next(item_container)
489 c = Context({'prev_url' : prev_url,
490 'prev_info' : prev_info,
491 'complete_url': './index.html/show_complete/',
492 'next_url' : next_url,
493 'next_info' : next_info, })
494 return t.render(c)
495
496# -----------------------------------------------------
497def get_folderish_actions(request, user_perms, item_container, app_name,
498 has_user_folder, dont={}):
499 from django.template.loader import get_template
500 from django.template import Context
501 if not request.user.is_authenticated():
502 return ''
503 t = get_template('app/manage_options_folderish.html')
504 nPos = max ( string.rfind ( request.path, '/add/' ),
505 string.rfind ( request.path, '/edit/' ),
506 string.rfind ( request.path, '/navigation/' ),
507 string.rfind ( request.path, '/navigation_left/' ),
508 string.rfind ( request.path, '/navigation_top/' ),
509 string.rfind ( request.path, '/manage/' ),
510 string.rfind ( request.path, '/manage_browseable/' ),
511 string.rfind ( request.path, '/manage_comments/' ),
512 string.rfind ( request.path, '/import/' ),
513 string.rfind ( request.path, '/export/' ),
514 string.rfind ( request.path, '/manage_site/' ),
515 string.rfind ( request.path, '/manage_user/' ),
516 string.rfind ( request.path, '/sort/' ),
517 string.rfind ( request.path, '/empty_folders/' ),
518 string.rfind ( request.path, '/find_items/' ),
519 )
520 if nPos > -1 or dont != {}:
521 path = request.path[:nPos]
522 show_mode = not dont.has_key('show_mode') and user_perms.perm_read
523 add_mode = not dont.has_key('add_mode') and \
524 user_perms.perm_add and item_container.item.has_user_support
525 edit_mode = not dont.has_key('edit_mode') and \
526 user_perms.perm_edit
527 # --- Stimmen diese Rechte bei ..own..??
528 manage_mode = not dont.has_key('manage_mode') and \
529 ( user_perms.perm_manage or user_perms.perm_edit_own \
530 or user_perms.perm_manage_own )
531 import_mode = not dont.has_key('import_mode') and \
532 user_perms.perm_manage_folderish
533 export_mode = not dont.has_key('export_mode') and \
534 user_perms.perm_manage_folderish
535 browseable_mode = not dont.has_key('browseable_mode') and \
536 user_perms.perm_edit
537 comment_mode = not dont.has_key('comment_mode') and \
538 item_container.item.has_comments and \
539 user_perms.perm_edit
540 user_mode = not dont.has_key('user_mode') and \
541 user_perms.perm_manage_user and has_user_folder
542 navigation_mode = not dont.has_key('navigation_mode') and \
543 user_perms.perm_manage_folderish
544 navigation_top_mode = not dont.has_key('navigation_top_mode') and \
545 user_perms.perm_manage_site and item_container.container.id == 1
546 navigation_left_mode = not dont.has_key('navigation_left_mode') and \
547 user_perms.perm_manage_site and item_container.container.id == 1
548 sort_mode = not dont.has_key('sort_mode') and \
549 user_perms.perm_manage
550 empty_mode = not dont.has_key('empty_mode') and \
551 user_perms.perm_manage
552 search_mode = not dont.has_key('search_mode') and \
553 user_perms.perm_add
554 else :
555 path = request.path
556 show_mode = False
557 add_mode = False
558 edit_mode = False
559 manage_mode = True
560 import_mode = False
561 export_mode = False
562 browseable_mode = False
563 comment_mode = False
564 user_mode = False
565 navigation_mode = False
566 navigation_top_mode = False
567 navigation_left_mode = False
568 sort_mode = False
569 empty_mode = False
570 search_mode = False
571
572 if string.find ( path, 'index.html' ) < 0 :
573 path += 'index.html'
574 if ( string.find(request.path, '/add/') >= 0 ) :
575 edit_mode = False
576 import_mode = False
577 export_mode = False
578 browseable_mode = False
579 comment_mode = False
580 user_mode = False
581 navigation_mode = False
582 navigation_left_mode = False
583 sort_mode = False
584 empty_mode = False
585 search_mode = False
586 elif ( string.find(request.path, '/edit/') >= 0 ) :
587 edit_mode = False
588 user_mode = False
589 elif ( string.find(request.path, '/manage/') >= 0 ) :
590 manage_mode = False
591 elif ( string.find(request.path, '/manage_browseable/') >= 0 ) :
592 browseable_mode = False
593 elif ( string.find(request.path, '/manage_comment/') >= 0 ) :
594 import_mode = False
595 export_mode = False
596 comment_mode = False
597 user_mode = False
598 navigation_mode = False
599 navigation_left_mode = False
600 sort_mode = False
601 elif ( string.find(request.path, '/sort/') >= 0 ) :
602 user_mode = False
603 sort_mode = False
604 elif ( string.find(request.path, '/empty_folders/') >= 0 ):
605 empty_mode = False
606 elif ( string.find(request.path, '/navigation/') >= 0 ) :
607 user_mode = False
608 navigation_mode = False
609 elif ( string.find(request.path, '/navigation_top/') >= 0 ) :
610 user_mode = False
611 navigation_top_mode = False
612 elif ( string.find(request.path, '/navigation_left/') >= 0 ) :
613 user_mode = False
614 navigation_left_mode = False
615 c = Context( {'authenticated' : request.user.is_authenticated(),
616 'app_name' : app_name,
617 'show_mode' : show_mode,
618 'add_mode' : add_mode,
619 'edit_mode' : edit_mode,
620 'manage_mode' : manage_mode,
621 'import_mode' : import_mode,
622 'export_mode' : import_mode,
623 'browseable_mode' : browseable_mode,
624 'comment_mode' : comment_mode,
625 'navigation_mode' : navigation_mode,
626 'navigation_top_mode' : navigation_top_mode,
627 'navigation_left_mode': navigation_left_mode,
628 'sort_mode' : sort_mode,
629 'empty_mode' : empty_mode,
630 'search_mode' : search_mode,
631 'user_mode' : has_user_folder and user_mode,
632 'path' : get_site_url(item_container, 'index.html'),
633 'user_path' : get_site_url(item_container,
634 'acl_users/index.html'),
635 'user_perms' : user_perms,
636 'user_name' : request.user,
637 'base_site_url' : get_base_site_url(),
638 } )
639 return t.render(c).strip()
640
641# -----------------------------------------------------
642def get_item_actions(request, user_perms, item_container, app_name,
643 item_comments, commands={}):
644 from django.template.loader import get_template
645 from django.template import Context
646 t = get_template('app/manage_options_item.html')
647 show_mode = commands.has_key('show_mode')
648 edit_mode = commands.has_key('edit_mode')
649 rss_mode = commands.has_key('rss_mode')
650 image_mode = commands.has_key('image_mode')
651 export_mode = commands.has_key('export_mode')
652 if item_comments != None and item_comments != False and request.user.is_authenticated():
653 has_comments = ( string.find(request.path, '/manage_comments/') < 0 )
654 else:
655 has_comments = False
656 if string.find(request.path, '/change_owner/') >= 0:
657 manage_site_mode = False
658 else:
659 manage_site_mode = request.user.is_authenticated() \
660 and user_perms.perm_manage_site
661 c = Context ( { 'authenticated' : request.user.is_authenticated(),
662 'show_mode' : show_mode,
663 'edit_mode' : edit_mode,
664 'export_mode' : export_mode,
665 'rss_mode' : rss_mode,
666 'manage_comments_mode': has_comments,
667 'image_mode' : image_mode,
668 'manage_site_mode' : manage_site_mode,
669 'user_perms' : user_perms,
670 'user_name' : request.user,
671 'path' : get_site_url(item_container,
672 item_container.item.name), } )
673 return t.render(c).strip()
674
675# -----------------------------------------------------
676def get_site_actions(request, user_perms, item_container, dont={}):
677 from django.template.loader import get_template
678 from django.template import Context
679 if not request.user.is_authenticated():
680 return ''
681 t = get_template('app/manage_options_site.html')
682 nPos = max ( string.rfind ( request.path, '/add/' ),
683 string.rfind ( request.path, '/edit/' ),
684 string.rfind ( request.path, '/navigation/' ),
685 string.rfind ( request.path, '/navigation_left/' ),
686 string.rfind ( request.path, '/navigation_top/' ),
687 string.rfind ( request.path, '/manage/' ),
688 string.rfind ( request.path, '/manage_browseable/' ),
689 string.rfind ( request.path, '/manage_comments/' ),
690 string.rfind ( request.path, '/import/' ),
691 string.rfind ( request.path, '/manage_site/' ),
692 string.rfind ( request.path, '/manage_user/' ),
693 string.rfind ( request.path, '/sort/' ),
694 )
695 if nPos > -1 :
696 path = request.path[:nPos]
697 manage_domain_mode = request.user.is_authenticated() \
698 and not dont.has_key('manage_site_mode') \
699 and user_perms.perm_manage_site \
700 and item_container.item.app.name == 'dmsFolder'
701 manage_site_mode = request.user.is_authenticated() \
702 and not dont.has_key('manage_site_mode') \
703 and user_perms.perm_manage_site
704 else :
705 path = request.path
706 manage_domain_mode = False
707 manage_site_mode = False
708
709 if string.find ( path, 'index.html' ) < 0 :
710 path += 'index.html'
711 if ( string.find(request.path, '/manage_site/') >= 0 ) :
712 manage_site_mode = False
713 user_mode = False
714 elif ( string.find(request.path, '/manage_domain/') >= 0 ) :
715 manage_domain_mode = False
716 user_mode = False
717 c = Context( {'authenticated' : request.user.is_authenticated(),
718 'manage_domain_mode' : manage_domain_mode,
719 'manage_site_mode' : manage_site_mode,
720 'path' : get_site_url(item_container, 'index.html'),
721 'user_path' : get_site_url(item_container,
722 'acl_users/index.html'),
723 'user_perms' : user_perms,
724 'user_name' : request.user,
725 'base_site_url' : get_base_site_url(),
726 } )
727 return t.render(c).strip()
728
729# -----------------------------------------------------
730def get_item_actions(request, user_perms, item_container, app_name,
731 item_comments, commands={}):
732 from django.template.loader import get_template
733 from django.template import Context
734 t = get_template('app/manage_options_item.html')
735 show_mode = commands.has_key('show_mode')
736 edit_mode = commands.has_key('edit_mode')
737 rss_mode = commands.has_key('rss_mode')
738 image_mode = commands.has_key('image_mode')
739 export_mode = commands.has_key('export_mode')
740 if item_comments != None and item_comments != False and request.user.is_authenticated():
741 has_comments = ( string.find(request.path, '/manage_comments/') < 0 )
742 else:
743 has_comments = False
744 if string.find(request.path, '/change_owner/') >= 0:
745 manage_site_mode = False
746 else:
747 manage_site_mode = request.user.is_authenticated() \
748 and user_perms.perm_manage_site
749 c = Context ( { 'authenticated' : request.user.is_authenticated(),
750 'show_mode' : show_mode,
751 'edit_mode' : edit_mode,
752 'export_mode' : export_mode,
753 'rss_mode' : rss_mode,
754 'manage_comments_mode': has_comments,
755 'image_mode' : image_mode,
756 'manage_site_mode' : manage_site_mode,
757 'user_perms' : user_perms,
758 'user_name' : request.user,
759 'path' : get_site_url(item_container,
760 item_container.item.name), } )
761 return t.render(c).strip()
762
763# -----------------------------------------------------
764def get_item_add_actions(request, user_perms, item_container, commands):
765 from django.template.loader import get_template
766 from django.template import Context
767 t = get_template('app/manage_options_item.html')
768 if commands == {}:
769 show_mode = True
770 edit_mode = False
771 rss_mode = False
772 image_mode = False
773 has_comments = False
774 else:
775 show_mode = commands.has_key('show_mode')
776 edit_mode = commands.has_key('edit_mode')
777 rss_mode = commands.has_key('rss_mode')
778 image_mode = commands.has_key('image_mode')
779 #if item_comments != None and item_comments != False and request.user.is_authenticated():
780 # has_comments = ( string.find(request.path, '/manage_comments/') < 0 )
781 #else:
782 has_comments = False
783 c = Context ( { 'authenticated' : request.user.is_authenticated(),
784 'show_mode' : show_mode,
785 'edit_mode' : edit_mode,
786 'rss_mode' : rss_mode,
787 'image_mode' : image_mode,
788 'manage_comments_mode': has_comments,
789 'user_perms' : user_perms,
790 'user_name' : request.user,
791 'path' : get_site_url(item_container, 'index.html'), } )
792 return t.render(c).strip()
793
794# -----------------------------------------------------
795def get_navigation_left(item_container):
796 """ liefert den linken Navigationsbereich """
797 items = get_menuitems_navmenu_left(item_container.container.menu_left_id,
798 item_container.container.nav_name_left)
799 if len(items) > 0:
800 return items[0].navigation
801 else:
802 return '<p>%s<br />%i, %s</p>' % \
803 (_('Navigation fehlt!'), item_container.container.menu_left_id,
804 item_container.container.nav_name_left)
805
806# -----------------------------------------------------
807def get_navigation_top(item_container):
808 """ liefert den oberen Navigationsbereich """
809 items = get_menuitems_navmenu_top(item_container.container.menu_top_id,
810 item_container.container.nav_name_top)
811 n = items[0].navigation
812 if len(items) > 0:
813 return n
814 else:
815 return _('<p>Navigation fehlt!<br />%(menu_left_id)i, %(menu_left_name)s</p>' % \
816 (item_container.container.menu_top_id,
817 item_container.container.nav_name_top) )
818
819# -----------------------------------------------------
820def get_german_date(d):
821 """ d=2003-02-01 10:11:12 -> 01.02.2003 10:11"""
822 arr = string.splitfields(d, ' ')
823 Y, M, D = string.splitfields(arr[0], '-')
824 h, m, s = string.splitfields(arr[1], ':')
825 dt = datetime.datetime(int(Y),int(M),int(D),int(h),int(m))
826 return dt.strftime('%d.%m.%Y %H:%M')
827
828# -----------------------------------------------------
829def remove_link_icons(t):
830 """ entfernt Icons, mit denen Verweise (automatisch) gekennzeichnet werden """
831 t = string.replace(t, EXTERN_ICON + '<a', '<a')
832 t = string.replace(t, NEW_WINDOW_ICON + '<a', '<a')
833 return t
834
835# -----------------------------------------------------
836def add_if_app_available(item_container, add_on, name, url, info):
837 """
838 Welche Applikationsobjekte (in der Management-Sicht) duerfen ergaenzt werden?
839 Falls die Applikation <name> erlaubt ist, wird ein dictionary mit <url> und
840 <info> bei der Liste <add_on> ergaenzt.
841 """
842 if is_app_available(name):
843 add_on.append( { 'url' : get_site_url(item_container, url), 'info': info, } )
844
Back to Top