EventAggregator

actions/EventAggregatorNewEvent.py

58:fd80caf4722f
2009-11-07 Paul Boddie Restricted the mode parameter to specific calendars where these are named. Added display parameters to the new event links so that the calendar involved retains its settings. Tidied up parameter name handling and retrieval.
     1 # -*- coding: iso-8859-1 -*-     2 """     3     MoinMoin - EventAggregatorNewEvent Action     4      5     @copyright: 2008, 2009 by Paul Boddie <paul@boddie.org.uk>     6     @copyright: 2000-2004 Juergen Hermann <jh@web.de>,     7                 2003-2008 MoinMoin:ThomasWaldmann,     8                 2004-2006 MoinMoin:AlexanderSchremmer,     9                 2007 MoinMoin:ReimarBauer.    10     @license: GNU GPL (v2 or later), see COPYING.txt for details.    11 """    12     13 from MoinMoin.action import ActionBase    14 from MoinMoin.Page import Page    15 from MoinMoin.PageEditor import PageEditor    16 import EventAggregatorSupport    17 import calendar    18 import re    19     20 Dependencies = ['pages']    21     22 # Action class and supporting functions.    23     24 class EventAggregatorNewEvent(ActionBase):    25     26     "An event creation dialogue requesting various parameters."    27     28     def _get_selected(self, value, input_value):    29         return input_value is not None and value == input_value and 'selected="selected"' or ''    30     31     def _get_selected_for_list(self, value, input_values):    32         return value in input_values and 'selected="selected"' or ''    33     34     def _get_input(self, form, name, default):    35         return int(form.get(name, [None])[0] or default)    36     37     def get_form_html(self, buttons_html):    38         _ = self._    39         request = self.request    40         form = request.form    41     42         category_list = []    43         category_pagenames = form.get("category", [])    44     45         for category_name, category_pagename in \    46             EventAggregatorSupport.getCategoryMapping(    47                 EventAggregatorSupport.getCategories(request),    48                 request):    49     50             selected = self._get_selected_for_list(category_pagename, category_pagenames)    51             category_list.append('<option value="%s" %s>%s</option>' % (category_pagename, selected, category_name))    52     53         # Initialise month lists.    54     55         start_month_list = []    56         start_month_list.append('<option value=""></option>')    57         end_month_list = []    58         end_month_list.append('<option value=""></option>')    59     60         start_month = self._get_input(form, "start-month", 0)    61         end_month = self._get_input(form, "end-month", 0)    62     63         # Prepare month lists, selecting specified months.    64     65         for month in range(1, 13):    66             month_label = _(EventAggregatorSupport.getMonthLabel(month))    67             selected = self._get_selected(month, start_month)    68             start_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label))    69             selected = self._get_selected(month, end_month)    70             end_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label))    71     72         # Permitting configuration of the template name.    73     74         template_default = getattr(request.cfg, "event_aggregator_new_event_template", "EventTemplate")    75     76         d = {    77             "buttons_html"          : buttons_html,    78             "category_label"        : _("Categories"),    79             "category_list"         : "\n".join(category_list),    80             "start_month_list"      : "\n".join(start_month_list),    81             "end_month_list"        : "\n".join(end_month_list),    82             "start_label"           : _("Start date (day, month, year)"),    83             "start_day_default"     : form.get("start-day", [""])[0],    84             "start_year_default"    : form.get("start-year", [""])[0] or EventAggregatorSupport.getCurrentYear(),    85             "end_label"             : _("End date (day, month, year) - if different"),    86             "end_day_default"       : form.get("end-day", [""])[0],    87             "end_year_default"      : form.get("end-year", [""])[0],    88             "title_label"           : _("Event title/summary"),    89             "title_default"         : form.get("title", [""])[0],    90             "description_label"     : _("Event description"),    91             "description_default"   : form.get("description", [""])[0],    92             "template_label"        : _("Event template"),    93             "template_default"      : form.get("template", [""])[0] or template_default,    94             "parent_label"          : _("Parent page"),    95             "parent_default"        : form.get("parent", [""])[0],    96             }    97     98         return '''    99 <table>   100     <tr>   101         <td class="label"><label>%(title_label)s</label></td>   102         <td>   103             <input name="title" type="text" size="40" value="%(title_default)s" />   104         </td>   105     </tr>   106     <tr>   107         <td class="label"><label>%(category_label)s</label></td>   108         <td class="content">   109             <select multiple="multiple" name="category">   110                 %(category_list)s   111             </select>   112         </td>   113     </tr>   114     <tr>   115         <td class="label"><label>%(start_label)s</label></td>   116         <td>   117             <input name="start-day" type="text" value="%(start_day_default)s" size="2" />   118             <select name="start-month">   119                 %(start_month_list)s   120             </select>   121             <input name="start-year" type="text" value="%(start_year_default)s" size="4" />   122         </td>   123     </tr>   124     <tr>   125         <td class="label"><label>%(end_label)s</label></td>   126         <td>   127             <input name="end-day" type="text" value="%(end_day_default)s" size="2" />   128             <select name="end-month">   129                 %(end_month_list)s   130             </select>   131             <input name="end-year" type="text" value="%(end_year_default)s" size="4" />   132         </td>   133     </tr>   134     <tr>   135         <td class="label"><label>%(description_label)s</label></td>   136         <td>   137             <input name="description" type="text" size="40" value="%(description_default)s" />   138         </td>   139     </tr>   140     <tr>   141         <td class="label"><label>%(template_label)s</label></td>   142         <td>   143             <input name="template" type="text" size="40" value="%(template_default)s" />   144         </td>   145     </tr>   146     <tr>   147         <td class="label"><label>%(parent_label)s</label></td>   148         <td>   149             <input name="parent" type="text" size="40" value="%(parent_default)s" />   150         </td>   151     </tr>   152     <tr>   153         <td></td>   154         <td class="buttons">   155             %(buttons_html)s   156         </td>   157     </tr>   158 </table>   159 ''' % d   160    161     def do_action(self):   162    163         "Create the new event."   164    165         _ = self._   166         form = self.request.form   167    168         # If no title exists in the request, an error message is returned.   169    170         title = form.get("title", [None])[0]   171         template = form.get("template", [None])[0]   172    173         if not title:   174             return 0, _("No event title specified.")   175    176         if not template:   177             return 0, _("No page template specified.")   178    179         return self.create_event(self.request)   180    181     def render_success(self, msg, msgtype=None):   182    183         """   184         Render neither 'msg' nor 'msgtype' since redirection should occur   185         instead.   186         NOTE: msgtype is optional because MoinMoin 1.5.x does not support it.   187         """   188    189         pass   190    191     def create_event(self, request):   192    193         "Create an event page using the 'request'."   194    195         _ = request.getText   196         form = request.form   197    198         category_pagenames = form.get("category", [])   199         description = form.get("description", [None])[0]   200    201         try:   202             title = form["title"][0]   203             template = form["template"][0]   204             parent = form["parent"][0]   205    206             start_day = self._get_input(form, "start-day", 0)   207             start_month = self._get_input(form, "start-month", 0)   208             start_year = self._get_input(form, "start-year", 0)   209             end_day = self._get_input(form, "end-day", start_day)   210             end_month = self._get_input(form, "end-month", start_month)   211             end_year = self._get_input(form, "end-year", start_year)   212    213             start_year, start_month, start_day = self.constrain_date(start_year, start_month, start_day)   214             end_year, end_month, end_day = self.constrain_date(end_year, end_month, end_day)   215    216         except (TypeError, ValueError):   217             return 0, _("Days and years must be numbers yielding a valid date!")   218    219         except (KeyError, IndexError):   220             return 0, _("Event title or template missing.")   221    222         start_date = self.make_date_string(start_year, start_month, start_day)   223         end_date = self.make_date_string(end_year, end_month, end_day)   224    225         # Copy the template.   226    227         page = PageEditor(request, template)   228    229         if not page.exists():   230             return 0, _("Event template not available.")   231    232         body = page.get_raw_body()   233    234         # Use any parent page information.   235    236         if parent:   237             title = "%s/%s" % (parent.rstrip("/"), title)   238    239         # Load the new page and replace the event details in the body.   240    241         new_page = PageEditor(request, title)   242    243         if new_page.exists():   244             return 0, _("The specified page already exists. Please choose another name.")   245    246         if EventAggregatorSupport.getFormat(page) == "wiki":   247             event_details = {"start" : start_date, "end" : end_date, "title" : title, "summary" : title, "description" : description}   248             body = EventAggregatorSupport.setEventDetails(body, event_details)   249             body = EventAggregatorSupport.setCategoryMembership(body, category_pagenames)   250             new_page.saveText(body, 0)   251    252         # Redirect and return success.   253    254         query = {'action' : 'edit'}   255         request.http_redirect(new_page.url(request, query))   256         return 1, None   257    258     def make_date_string(self, year, month, day):   259         return "%s-%s-%s" % (   260             year and ("%04d" % year) or "YYYY",   261             month and ("%02d" % month) or "MM",   262             day and ("%02d" % day) or "DD"   263             )   264    265     def constrain_date(self, year, month, day):   266         if year and month and day:   267             wd, last_day = calendar.monthrange(year, month)   268             day = max(min(day, last_day), 1)   269         return year, month, day   270    271 # Action function.   272    273 def execute(pagename, request):   274     EventAggregatorNewEvent(pagename, request).render()   275    276 # vim: tabstop=4 expandtab shiftwidth=4