EventAggregator

actions/EventAggregatorSummary.py

26:c59f92f89613
2009-03-31 Paul Boddie Introduced a support function for getting events in order. Added iCalendar download links and tidied up link generation.
     1 # -*- coding: iso-8859-1 -*-     2 """     3     MoinMoin - EventAggregatorSummary 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 import config    15 from MoinMoin.Page import Page    16 import EventAggregatorSupport    17     18 Dependencies = ['pages']    19     20 # Action class and supporting functions.    21     22 class EventAggregatorSummary(ActionBase):    23     24     "A summary dialogue requesting various parameters."    25     26     def get_form_html(self, buttons_html):    27         _ = self._    28         request = self.request    29     30         category_list = []    31     32         for category_name, category_pagename in \    33             EventAggregatorSupport.getCategoryMapping(    34                 EventAggregatorSupport.getCategories(request),    35                 request):    36     37             category_list.append('<option value="%s">%s</option>' % (category_pagename, category_name))    38     39         month_list = []    40         month_list.append('<option value=""></option>')    41     42         for month in range(1, 13):    43             month_label = _(EventAggregatorSupport.getMonthLabel(month))    44             month_list.append('<option value="%02d">%s</option>' % (month, month_label))    45     46         year_label = []    47     48         d = {    49             "buttons_html" : buttons_html,    50             "category_label" : _("Categories"),    51             "category_list" : "\n".join(category_list),    52             "month_list" : "\n".join(month_list),    53             "start_label" : _("Start year and month"),    54             "start_year_default" : "",    55             "end_label" : _("End year and month"),    56             "end_year_default" : "",    57             }    58     59         return '''    60 <table>    61     <tr>    62         <td class="label"><label>%(category_label)s</label></td>    63         <td class="content">    64             <select multiple="multiple" name="category">    65                 %(category_list)s    66             </select>    67         </td>    68     </tr>    69     <tr>    70         <td class="label"><label>%(start_label)s</label></td>    71         <td>    72             <select name="start-month">    73                 %(month_list)s    74             </select>    75             <input name="start-year" type="text" value="%(start_year_default)s" size="4" />    76         </td>    77     </tr>    78     <tr>    79         <td class="label"><label>%(end_label)s</label></td>    80         <td>    81             <select name="end-month">    82                 %(month_list)s    83             </select>    84             <input name="end-year" type="text" value="%(end_year_default)s" size="4" />    85         </td>    86     </tr>    87     <tr>    88         <td></td>    89         <td class="buttons">    90             %(buttons_html)s    91         </td>    92     </tr>    93 </table>    94 ''' % d    95     96     def do_action(self):    97     98         "Write the iCalendar resource."    99    100         _ = self._   101         form = self.request.form   102    103         # If no category names exist in the request, an error message is   104         # returned.   105    106         category_names = form.get("category", [])   107    108         if not category_names:   109             return 0, _("No categories specified.")   110    111         write_resource(self.request)   112         return 1, None   113    114     def render_success(self, msg, msgtype):   115    116         """   117         Render neither 'msg' nor 'msgtype' since a resource has already been   118         produced.   119         """   120    121         pass   122    123 def getQuotedText(text):   124    125     "Return the 'text' quoted for iCalendar purposes."   126    127     return text.replace(";", r"\;").replace(",", r"\,")   128    129 def write_resource(request):   130    131     """   132     For the given 'request', write an iCalendar summary of the event data found   133     in the categories specified via the "category" request parameter, using the   134     "start" and "end" parameters (if specified). Multiple "category" parameters   135     can be specified.   136     """   137    138     category_names = request.form.get("category", [])   139    140     # Otherwise, produce an iCalendar resource.   141    142     calendar_start = EventAggregatorSupport.getFormMonth(request, None, "start")   143     calendar_end = EventAggregatorSupport.getFormMonth(request, None, "end")   144    145     # Look for separate start and end years and months.   146    147     form = request.form   148    149     if calendar_start is None:   150         calendar_start = EventAggregatorSupport.getFormMonthPair(request, "start-year", "start-month")   151    152     if calendar_end is None:   153         calendar_end = EventAggregatorSupport.getFormMonthPair(request, "end-year", "end-month")   154    155     events, shown_events, all_shown_events, earliest, latest = \   156         EventAggregatorSupport.getEvents(request, category_names, calendar_start, calendar_end)   157    158     # Output iCalendar data...   159    160     request.emit_http_headers(["Content-Type: text/calendar; charset=%s" % config.charset])   161    162     request.write("BEGIN:VCALENDAR\r\n")   163     request.write("PRODID:-//MoinMoin//EventAggregatorSummary\r\n")   164     request.write("VERSION:2.0\r\n")   165    166     for event_page, event_details in all_shown_events:   167    168         # Get the summary and timestamp details.   169    170         event_summary = EventAggregatorSupport.getEventSummary(event_page, event_details)   171    172         # Get the initial revision of the page.   173    174         revisions = event_page.getRevList()   175         event_page_initial = Page(request, event_page.page_name, rev=revisions[-1])   176    177         # Get the created and last modified times.   178    179         created = EventAggregatorSupport.getPageDate(event_page_initial)   180         last_modified = EventAggregatorSupport.getPageDate(event_page)   181         sequence = len(revisions) - 1   182    183         # Output the event details.   184    185         request.write("BEGIN:VEVENT\r\n")   186         request.write("UID:%s\r\n" % request.getQualifiedURL(event_page.url(request)))   187         request.write("URL:%s\r\n" % request.getQualifiedURL(event_page.url(request)))   188         request.write("DTSTAMP:%04d%02d%02dT%02d%02d%02dZ\r\n" % created[:6])   189         request.write("LAST-MODIFIED:%04d%02d%02dT%02d%02d%02dZ\r\n" % last_modified[:6])   190         request.write("SEQUENCE:%d\r\n" % sequence)   191         request.write("DTSTART;VALUE=DATE:%04d%02d%02d\r\n" % event_details["start"])   192         request.write("DTEND;VALUE=DATE:%04d%02d%02d\r\n" % EventAggregatorSupport.nextdate(event_details["end"]))   193         request.write("SUMMARY:%s\r\n" % getQuotedText(event_summary))   194    195         # Optional details.   196    197         if event_details.has_key("topics") or event_details.has_key("categories"):   198             request.write("CATEGORIES:%s\r\n" % ",".join(   199                 [getQuotedText(topic) for topic in event_details.get("topics") or event_details.get("categories")]   200                 ))   201         if event_details.has_key("location"):   202             request.write("LOCATION:%s\r\n" % getQuotedText(event_details["location"]))   203    204         request.write("END:VEVENT\r\n")   205    206     request.write("END:VCALENDAR\r\n")   207    208 # Action function.   209    210 def execute(pagename, request):   211     EventAggregatorSummary(pagename, request).render()   212    213 # vim: tabstop=4 expandtab shiftwidth=4