Back to index

moin  1.9.0~rc2
notification.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - Notification preferences
00004 
00005     @copyright: 2001-2004 Juergen Hermann <jh@web.de>,
00006                 2003-2007 MoinMoin:ThomasWaldmann
00007                 2007      MoinMoin:JohannesBerg
00008     @license: GNU GPL, see COPYING for details.
00009 """
00010 
00011 from MoinMoin import events
00012 from MoinMoin.widget import html
00013 from MoinMoin.userprefs import UserPrefBase
00014 
00015 
00016 class Settings(UserPrefBase):
00017     def __init__(self, request):
00018         """ Initialize user settings form. """
00019         UserPrefBase.__init__(self, request)
00020         self.request = request
00021         self._ = request.getText
00022         self.cfg = request.cfg
00023         self.title = self._("Notification")
00024         self.name = 'notification'
00025 
00026     def _decode_pagelist(self, key):
00027         """ Decode list of pages from form input
00028 
00029         Each line is a page name, empty lines ignored.
00030 
00031         @param key: the form key to get
00032         @rtype: list of unicode strings
00033         @return: list of normalized names
00034         """
00035         text = self.request.form.get(key, '')
00036         text = text.replace('\r', '')
00037         items = []
00038         for item in text.split('\n'):
00039             item = item.strip()
00040             if not item:
00041                 continue
00042             items.append(item)
00043         return items
00044 
00045     def _save_notification_settings(self):
00046         _ = self._
00047         form = self.request.form
00048 
00049         if self.request.method != 'POST':
00050             return
00051         theuser = self.request.user
00052         if not theuser:
00053             return
00054 
00055         # subscription for page change notification
00056         theuser.subscribed_pages = self._decode_pagelist('subscribed_pages')
00057 
00058         # subscription to various events
00059         available = events.get_subscribable_events()
00060         theuser.email_subscribed_events = []
00061         theuser.jabber_subscribed_events = []
00062         types = {
00063             'email': theuser.email_subscribed_events,
00064             'jabber': theuser.jabber_subscribed_events
00065         }
00066         for tp in types:
00067             for evt in available:
00068                 fieldname = 'subscribe:%s:%s' % (tp, evt)
00069                 if fieldname in form:
00070                     types[tp].append(evt)
00071         # save data
00072         theuser.save()
00073 
00074         return 'info', _("Notification settings saved!")
00075 
00076 
00077     def handle_form(self):
00078         _ = self._
00079         form = self.request.form
00080 
00081         if form.has_key('cancel'):
00082             return
00083 
00084         if form.has_key('save'): # Save user profile
00085             return self._save_notification_settings()
00086 
00087     # form generation part
00088 
00089     def _event_select(self):
00090         """ Create event subscription list. """
00091         _ = self._
00092 
00093         types = []
00094         if self.cfg.mail_enabled and self.request.user.email:
00095             types.append(('email', _("'''Email'''", wiki=True)))
00096         if self.cfg.jabber_enabled and self.request.user.jid:
00097             types.append(('jabber', _("'''Jabber'''", wiki=True)))
00098 
00099         table = html.TABLE()
00100         header = html.TR()
00101         table.append(header)
00102         for name, descr in types:
00103             header.append(html.TH().append(html.Raw(descr)))
00104         header.append(html.TH(align='left').append(html.Raw(_("'''Event type'''", wiki=True))))
00105 
00106         event_list = events.get_subscribable_events()
00107         super = self.request.user.isSuperUser()
00108 
00109         # Create a list of (value, name) tuples for display as radiobuttons
00110         # Only include super-user visible events if current user has these rights.
00111         # It's cosmetic - the check for super-user rights should be performed
00112         # in event handling code as well!
00113         allowed = []
00114         for key in event_list.keys():
00115             if not event_list[key]['superuser'] or super:
00116                 allowed.append((key, event_list[key]['desc']))
00117 
00118         for evname, evdescr in allowed:
00119             tr = html.TR()
00120             table.append(tr)
00121             for notiftype, notifdescr in types:
00122                 checked = evname in getattr(self.request.user,
00123                                             '%s_subscribed_events' % notiftype)
00124                 tr.append(html.TD().append(html.INPUT(
00125                         type='checkbox',
00126                         checked=checked,
00127                         name='subscribe:%s:%s' % (notiftype, evname))))
00128             tr.append(html.TD().append(html.Raw(self.request.getText(evdescr))))
00129 
00130         return table
00131 
00132     def create_form(self):
00133         """ Create the complete HTML form code. """
00134         _ = self._
00135         self._form = self.make_form(
00136             _('Select the events you want to be notified about.'))
00137 
00138         self._form.append(html.INPUT(type="hidden", name="action", value="userprefs"))
00139         self._form.append(html.INPUT(type="hidden", name="handler", value="prefs"))
00140 
00141         if (not (self.cfg.mail_enabled and self.request.user.email)
00142             and not (self.cfg.jabber_enabled and self.request.user.jid)):
00143             self.make_row('', [html.Text(
00144                 _("Before you can be notified, you need to provide a way"
00145                   " to contact you in the general preferences."))])
00146             self.make_row('', [
00147                 html.INPUT(type="submit", name="cancel", value=_("Cancel"))])
00148             return unicode(self._form)
00149 
00150         self.make_row(_('Subscribed events'), [self._event_select()])
00151 
00152         # Get list of subscribe pages, DO NOT sort! it should
00153         # stay in the order the user entered it in his input
00154         # box.
00155         notifylist = self.request.user.getSubscriptionList()
00156 
00157         self.make_row(
00158             html.Raw(_('Subscribed wiki pages<<BR>>(one regex per line)', wiki=True)),
00159             [html.TEXTAREA(name="subscribed_pages", rows="6", cols="50").append(
00160                 '\n'.join(notifylist)), ],
00161             valign="top"
00162         )
00163 
00164         # Add buttons
00165         self.make_row('', [
00166             html.INPUT(type="submit", name="save", value=_("Save")),
00167             ' ',
00168             html.INPUT(type="submit", name="cancel", value=_("Cancel"))])
00169 
00170         return unicode(self._form)
00171 
00172     def allowed(self):
00173         return UserPrefBase.allowed(self) and (
00174             self.cfg.mail_enabled or self.cfg.jabber_enabled)