comparison MoinMoin/themes/__init__.py @ 1832:ff503e7ea7a0 namespaces

merged default into namespaces branch, some stuff XXX BROKEN
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 06 Oct 2012 17:46:24 +0200
parents 005e12de8b6a f6afb9aeb861
children 63f5fc2031f4
comparison
equal deleted inserted replaced
1316:7657798d40f3 1832:ff503e7ea7a0
9 9
10 10
11 import urllib 11 import urllib
12 12
13 from json import dumps 13 from json import dumps
14 from operator import itemgetter 14 from operator import attrgetter
15 15
16 from flask import current_app as app 16 from flask import current_app as app
17 from flask import g as flaskg 17 from flask import g as flaskg
18 from flask import url_for, request 18 from flask import url_for, request
19 from flaskext.themes import get_theme, render_theme_template 19 from flask.ext.themes import get_theme, render_theme_template
20 20
21 from MoinMoin import log 21 from MoinMoin import log
22 logging = log.getLogger(__name__) 22 logging = log.getLogger(__name__)
23 23
24 from MoinMoin.i18n import _, L_, N_ 24 from MoinMoin.i18n import _, L_, N_
26 from MoinMoin.config import USERID, ADDRESS, HOSTNAME 26 from MoinMoin.config import USERID, ADDRESS, HOSTNAME
27 from MoinMoin.search import SearchForm 27 from MoinMoin.search import SearchForm
28 from MoinMoin.util.interwiki import split_interwiki, getInterwikiHome, is_local_wiki, is_known_wiki, url_for_item 28 from MoinMoin.util.interwiki import split_interwiki, getInterwikiHome, is_local_wiki, is_known_wiki, url_for_item
29 from MoinMoin.util.crypto import cache_key 29 from MoinMoin.util.crypto import cache_key
30 from MoinMoin.util.forms import make_generator 30 from MoinMoin.util.forms import make_generator
31 from MoinMoin.util.clock import timed
32 from MoinMoin.util.mime import Type
31 33
32 34
33 def get_current_theme(): 35 def get_current_theme():
34 # this might be called at a time when flaskg.user is not setup yet: 36 # this might be called at a time when flaskg.user is not setup yet:
35 u = getattr(flaskg, 'user', None) 37 u = getattr(flaskg, 'user', None)
38 else: 40 else:
39 theme_name = app.cfg.theme_default 41 theme_name = app.cfg.theme_default
40 try: 42 try:
41 return get_theme(theme_name) 43 return get_theme(theme_name)
42 except KeyError: 44 except KeyError:
43 logging.warning("Theme {0} was not found; using default of {1} instead.".format(theme_name, app.cfg.theme_default)) 45 logging.warning("Theme {0!r} was not found; using default of {1!r} instead.".format(theme_name, app.cfg.theme_default))
44 theme_name = app.cfg.theme_default 46 theme_name = app.cfg.theme_default
45 return get_theme(theme_name) 47 return get_theme(theme_name)
46 48
47 49
48 50 @timed()
49 def render_template(template, **context): 51 def render_template(template, **context):
50 flaskg.clock.start('render_template') 52 return render_theme_template(get_current_theme(), template, **context)
51 output = render_theme_template(get_current_theme(), template, **context)
52 flaskg.clock.stop('render_template')
53 return output
54 53
55 def themed_error(e): 54 def themed_error(e):
56 item_name = request.view_args.get('item_name', u'') 55 item_name = request.view_args.get('item_name', u'')
57 if e.code == 403: 56 if e.code == 403:
58 title = L_('Access denied') 57 title = L_('Access denied')
124 :rtype: list 123 :rtype: list
125 :returns: list of item tuples (item_name, item_title, item_mime_type, has_children) 124 :returns: list of item tuples (item_name, item_title, item_mime_type, has_children)
126 """ 125 """
127 from MoinMoin.items import Item 126 from MoinMoin.items import Item
128 item = Item.create(item_name) 127 item = Item.create(item_name)
129 item_index = item.get_detailed_index(item.flat_index()) 128 return item.get_mixed_index()
130 # Sort items by whether or not they have children, then by name:
131 item_index = sorted(item_index, key=itemgetter(-1, 0))
132 return item_index
133 129
134 def userhome(self): 130 def userhome(self):
135 """ 131 """
136 Assemble arguments used to build user homepage link 132 Assemble arguments used to build user homepage link
137 133
140 """ 136 """
141 user = self.user 137 user = self.user
142 name = user.name0 138 name = user.name0
143 display_name = user.display_name or name 139 display_name = user.display_name or name
144 wikiname, itemname = getInterwikiHome(name) 140 wikiname, itemname = getInterwikiHome(name)
145 title = "{0} @ {1}".format(display_name, wikiname) 141 title = u"{0} @ {1}".format(display_name, wikiname)
146 # link to (interwiki) user homepage 142 # link to (interwiki) user homepage
147 if is_local_wiki(wikiname): 143 if is_local_wiki(wikiname):
148 exists = self.storage.has_item(itemname) 144 exists = self.storage.has_item(itemname)
149 else: 145 else:
150 # We cannot check if wiki pages exists in remote wikis 146 # We cannot check if wiki pages exists in remote wikis
201 href = url_for_item(item_name, namespace=namespace, wiki_name=wiki_name) 197 href = url_for_item(item_name, namespace=namespace, wiki_name=wiki_name)
202 if not title: 198 if not title:
203 title = item_name 199 title = item_name
204 return href, title, wiki_name 200 return href, title, wiki_name
205 201
202 @timed()
206 def navibar(self, item_name): 203 def navibar(self, item_name):
207 """ 204 """
208 Assemble the navibar 205 Assemble the navibar
209 206
210 :rtype: list 207 :rtype: list
211 :returns: list of tuples (css_class, url, link_text, title) 208 :returns: list of tuples (css_class, url, link_text, title)
212 """ 209 """
213 flaskg.clock.start('navibar')
214 current = item_name 210 current = item_name
215 # Process config navi_bar 211 # Process config navi_bar
216 items = [(cls, url_for(endpoint, **args), link_text, title) 212 items = [(cls, url_for(endpoint, **args), link_text, title)
217 for cls, endpoint, args, link_text, title in self.cfg.navi_bar] 213 for cls, endpoint, args, link_text, title in self.cfg.navi_bar]
218 214
241 sisteritems[item_name.decode('utf-8')] = item_url 237 sisteritems[item_name.decode('utf-8')] = item_url
242 except: 238 except:
243 pass # ignore invalid lines 239 pass # ignore invalid lines
244 f.close() 240 f.close()
245 app.cache.set(cid, sisteritems) 241 app.cache.set(cid, sisteritems)
246 logging.info("Site: {0} Status: Updated. Pages: {1}".format(sistername, len(sisteritems))) 242 logging.info("Site: {0!r} Status: Updated. Pages: {1}".format(sistername, len(sisteritems)))
247 except IOError as err: 243 except IOError as err:
248 (title, code, msg, headers) = err.args # code e.g. 304 244 (title, code, msg, headers) = err.args # code e.g. 304
249 logging.warning("Site: {0} Status: Not updated.".format(sistername)) 245 logging.warning("Site: {0!r} Status: Not updated.".format(sistername))
250 logging.exception("exception was:") 246 logging.exception("exception was:")
251 if current in sisteritems: 247 if current in sisteritems:
252 url = sisteritems[current] 248 url = sisteritems[current]
253 items.append(('sisterwiki', url, sistername, '')) 249 items.append(('sisterwiki', url, sistername, ''))
254 flaskg.clock.stop('navibar')
255 return items 250 return items
256 251
257 def parent_item(self, item_name): 252 def parent_item(self, item_name):
258 """ 253 """
259 Return name of parent item for the current item 254 Return name of parent item for the current item
295 email = None # pure email address of author 290 email = None # pure email address of author
296 if app.cfg.show_hosts and addr: 291 if app.cfg.show_hosts and addr:
297 # only tell ip / hostname if show_hosts is True 292 # only tell ip / hostname if show_hosts is True
298 if hostname: 293 if hostname:
299 text = hostname[:15] # 15 = len(ipaddr) 294 text = hostname[:15] # 15 = len(ipaddr)
300 name = title = '{0}[{1}]'.format(hostname, addr) 295 name = title = u'{0}[{1}]'.format(hostname, addr)
301 css = 'editor host' 296 css = 'editor host'
302 else: 297 else:
303 name = text = addr 298 name = text = addr
304 title = '[{0}]'.format(addr) 299 title = u'[{0}]'.format(addr)
305 css = 'editor ip' 300 css = 'editor ip'
306 301
307 userid = meta.get(USERID) 302 userid = meta.get(USERID)
308 if userid: 303 if userid:
309 u = user.User(userid) 304 u = user.User(userid)
310 name = u.name0 305 name = u.name0
311 text = name 306 text = name
312 display_name = u.display_name or name 307 display_name = u.display_name or name
313 if title: 308 if title:
314 # we already have some address info 309 # we already have some address info
315 title = "{0} @ {1}".format(display_name, title) 310 title = u"{0} @ {1}".format(display_name, title)
316 else: 311 else:
317 title = display_name 312 title = display_name
318 if u.mailto_author and u.email: 313 if u.mailto_author and u.email:
319 email = u.email 314 email = u.email
320 css = 'editor mail' 315 css = 'editor mail'
413 app.jinja_env.globals.update({ 408 app.jinja_env.globals.update({
414 # please note that flask-babel/jinja2.ext installs: 409 # please note that flask-babel/jinja2.ext installs:
415 # _, gettext, ngettext 410 # _, gettext, ngettext
416 'isinstance': isinstance, 411 'isinstance': isinstance,
417 'list': list, 412 'list': list,
413 'Type': Type,
418 # please note that flask-themes installs: 414 # please note that flask-themes installs:
419 # theme, theme_static 415 # theme, theme_static
420 'theme_supp': ThemeSupport(app.cfg), 416 'theme_supp': ThemeSupport(app.cfg),
421 'user': flaskg.user, 417 'user': flaskg.user,
422 'storage': flaskg.storage, 418 'storage': flaskg.storage,
427 'get_editor_info': lambda meta: get_editor_info(meta), 423 'get_editor_info': lambda meta: get_editor_info(meta),
428 'utctimestamp': lambda dt: utctimestamp(dt), 424 'utctimestamp': lambda dt: utctimestamp(dt),
429 'gen': make_generator(), 425 'gen': make_generator(),
430 'search_form': SearchForm.from_defaults(), 426 'search_form': SearchForm.from_defaults(),
431 }) 427 })
432