Back to index

moin  1.9.0~rc2
userprefs.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - user settings action
00004 
00005     @copyright: 2006 Radomir Dopieralski
00006                 2007, 2008 MoinMoin:JohannesBerg
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 from MoinMoin import Page, wikiutil
00011 from MoinMoin.widget import html
00012 
00013 def _handle_submission(request):
00014     """ Handle GET and POST requests of preferences forms.
00015 
00016     Return error msg_class, msg tuple or None, None.
00017     """
00018     _ = request.getText
00019     sub = request.values.get('handler')
00020 
00021     if sub in request.cfg.userprefs_disabled:
00022         return None, None
00023 
00024     try:
00025         cls = wikiutil.importPlugin(request.cfg, 'userprefs', sub, 'Settings')
00026     except wikiutil.PluginMissingError:
00027         # we never show this plugin to click on so no need to
00028         # give a message here
00029         return None, None
00030 
00031     obj = cls(request)
00032     if not obj.allowed():
00033         return None, None
00034     res = obj.handle_form()
00035     if isinstance(res, tuple):
00036         return res
00037     # backward compatibility for userprefs plugins,
00038     # they just get 'dialog'-style messages.
00039     return None, res
00040 
00041 def _create_prefs_page(request, sel=None):
00042     _ = request.getText
00043     plugins = wikiutil.getPlugins('userprefs', request.cfg)
00044     ret = html.P()
00045     ret.append(html.Text(_("Please choose:")))
00046     ret.append(html.BR())
00047     items = html.UL()
00048     ret.append(items)
00049     for sub in plugins:
00050         if sub in request.cfg.userprefs_disabled:
00051             continue
00052         cls = wikiutil.importPlugin(request.cfg, 'userprefs', sub, 'Settings')
00053         obj = cls(request)
00054         if not obj.allowed():
00055             continue
00056         url = request.page.url(request, {'action': 'userprefs', 'sub': sub})
00057         lnk = html.LI().append(html.A(href=url).append(html.Text(obj.title)))
00058         items.append(lnk)
00059     return unicode(ret)
00060 
00061 
00062 def _create_page(request, cancel=False):
00063     # returns text, title, msg_class, msg
00064     pagename = request.page.page_name
00065 
00066     if 'handler' in request.values:
00067         msg_class, msg = _handle_submission(request)
00068     else:
00069         msg_class, msg = None, None
00070 
00071     sub = request.args.get('sub', '')
00072     cls = None
00073     if sub and sub not in request.cfg.userprefs_disabled:
00074         try:
00075             cls = wikiutil.importPlugin(request.cfg, 'userprefs', sub, 'Settings')
00076         except wikiutil.PluginMissingError:
00077             # cls is already None
00078             pass
00079 
00080     obj = cls and cls(request)
00081 
00082     if not obj or not obj.allowed():
00083         return _create_prefs_page(request), None, msg_class, msg
00084 
00085     return obj.create_form(), obj.title, msg_class, msg
00086 
00087 
00088 def execute(pagename, request):
00089     _ = request.getText
00090     if not request.user.valid:
00091         actname = __name__.split('.')[-1]
00092         request.theme.add_msg(_("You must login to use this action: %(action)s.") % {"action": actname}, "error")
00093         return Page.Page(request, pagename).send_page()
00094 
00095     text, title, msg_class, msg = _create_page(request)
00096     if title:
00097         # XXX: we would like to make "Settings" here a link back
00098         #      to the generic userprefs page but that is impossible
00099         #      due to the way the title is emitted and the theme is
00100         #      responsible for doing the linking....
00101         title = _("Settings") + ":" + title
00102     else:
00103         title = _("Settings")
00104     request.theme.add_msg(msg, msg_class)
00105     request.theme.send_title(title, page=request.page, pagename=pagename)
00106     # Start content (important for RTL support)
00107     request.write(request.formatter.startContent("content"))
00108     request.write(text)
00109     request.write(request.formatter.endContent())
00110     request.theme.send_footer(pagename)
00111     request.theme.send_closing_html()