paul@10 | 1 | # -*- coding: iso-8859-1 -*- |
paul@10 | 2 | """ |
paul@10 | 3 | MoinMoin - EventAggregatorSummary Action |
paul@10 | 4 | |
paul@10 | 5 | @copyright: 2008, 2009 by Paul Boddie <paul@boddie.org.uk> |
paul@10 | 6 | @copyright: 2000-2004 Juergen Hermann <jh@web.de>, |
paul@19 | 7 | 2005-2008 MoinMoin:ThomasWaldmann, |
paul@19 | 8 | 2007 MoinMoin:ReimarBauer. |
paul@10 | 9 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@10 | 10 | """ |
paul@10 | 11 | |
paul@19 | 12 | from MoinMoin.action import ActionBase |
paul@10 | 13 | from MoinMoin import config |
paul@10 | 14 | import EventAggregatorSupport |
paul@10 | 15 | |
paul@10 | 16 | Dependencies = ['pages'] |
paul@10 | 17 | |
paul@19 | 18 | # Action class and supporting functions. |
paul@19 | 19 | |
paul@19 | 20 | class EventAggregatorSummary(ActionBase): |
paul@19 | 21 | |
paul@19 | 22 | "A summary dialogue requesting various parameters." |
paul@19 | 23 | |
paul@19 | 24 | def get_form_html(self, buttons_html): |
paul@19 | 25 | _ = self._ |
paul@19 | 26 | request = self.request |
paul@19 | 27 | |
paul@19 | 28 | category_list = [] |
paul@19 | 29 | |
paul@19 | 30 | for category_name, category_pagename in \ |
paul@19 | 31 | EventAggregatorSupport.getCategoryMapping( |
paul@19 | 32 | EventAggregatorSupport.getCategories(request), |
paul@19 | 33 | request): |
paul@19 | 34 | |
paul@19 | 35 | category_list.append('<option value="%s">%s</option>' % (category_pagename, category_name)) |
paul@19 | 36 | |
paul@23 | 37 | month_list = [] |
paul@23 | 38 | month_list.append('<option value=""></option>') |
paul@23 | 39 | |
paul@23 | 40 | for month in range(1, 13): |
paul@23 | 41 | month_label = _(EventAggregatorSupport.getMonthLabel(month)) |
paul@23 | 42 | month_list.append('<option value="%02d">%s</option>' % (month, month_label)) |
paul@23 | 43 | |
paul@23 | 44 | year_label = [] |
paul@23 | 45 | |
paul@19 | 46 | d = { |
paul@19 | 47 | "buttons_html" : buttons_html, |
paul@19 | 48 | "category_label" : _("Categories"), |
paul@19 | 49 | "category_list" : "\n".join(category_list), |
paul@23 | 50 | "month_list" : "\n".join(month_list), |
paul@19 | 51 | "start_label" : _("Start year and month"), |
paul@23 | 52 | "start_year_default" : "", |
paul@19 | 53 | "end_label" : _("End year and month"), |
paul@23 | 54 | "end_year_default" : "", |
paul@19 | 55 | } |
paul@10 | 56 | |
paul@19 | 57 | return ''' |
paul@19 | 58 | <table> |
paul@19 | 59 | <tr> |
paul@19 | 60 | <td class="label"><label>%(category_label)s</label></td> |
paul@19 | 61 | <td class="content"> |
paul@19 | 62 | <select multiple="multiple" name="category"> |
paul@19 | 63 | %(category_list)s |
paul@19 | 64 | </select> |
paul@19 | 65 | </td> |
paul@19 | 66 | </tr> |
paul@19 | 67 | <tr> |
paul@19 | 68 | <td class="label"><label>%(start_label)s</label></td> |
paul@22 | 69 | <td> |
paul@23 | 70 | <select name="start-month"> |
paul@23 | 71 | %(month_list)s |
paul@23 | 72 | </select> |
paul@23 | 73 | <input name="start-year" type="text" value="%(start_year_default)s" size="4" /> |
paul@19 | 74 | </td> |
paul@19 | 75 | </tr> |
paul@19 | 76 | <tr> |
paul@19 | 77 | <td class="label"><label>%(end_label)s</label></td> |
paul@22 | 78 | <td> |
paul@23 | 79 | <select name="end-month"> |
paul@23 | 80 | %(month_list)s |
paul@23 | 81 | </select> |
paul@23 | 82 | <input name="end-year" type="text" value="%(end_year_default)s" size="4" /> |
paul@19 | 83 | </td> |
paul@19 | 84 | </tr> |
paul@19 | 85 | <tr> |
paul@19 | 86 | <td></td> |
paul@19 | 87 | <td class="buttons"> |
paul@19 | 88 | %(buttons_html)s |
paul@19 | 89 | </td> |
paul@19 | 90 | </tr> |
paul@19 | 91 | </table> |
paul@19 | 92 | ''' % d |
paul@19 | 93 | |
paul@19 | 94 | def do_action(self): |
paul@19 | 95 | |
paul@19 | 96 | "Write the iCalendar resource." |
paul@19 | 97 | |
paul@19 | 98 | _ = self._ |
paul@23 | 99 | form = self.request.form |
paul@19 | 100 | |
paul@19 | 101 | # If no category names exist in the request, an error message is |
paul@19 | 102 | # returned. |
paul@19 | 103 | |
paul@23 | 104 | category_names = form.get("category", []) |
paul@19 | 105 | |
paul@19 | 106 | if not category_names: |
paul@19 | 107 | return 0, _("No categories specified.") |
paul@19 | 108 | |
paul@19 | 109 | write_resource(self.request) |
paul@19 | 110 | return 1, None |
paul@19 | 111 | |
paul@19 | 112 | def render_success(self, msg, msgtype): |
paul@19 | 113 | |
paul@19 | 114 | """ |
paul@19 | 115 | Render neither 'msg' nor 'msgtype' since a resource has already been |
paul@19 | 116 | produced. |
paul@19 | 117 | """ |
paul@19 | 118 | |
paul@19 | 119 | pass |
paul@19 | 120 | |
paul@19 | 121 | def write_resource(request): |
paul@10 | 122 | |
paul@10 | 123 | """ |
paul@19 | 124 | For the given 'request', write an iCalendar summary of the event data found |
paul@19 | 125 | in the categories specified via the "category" request parameter, using the |
paul@19 | 126 | "start" and "end" parameters (if specified). Multiple "category" parameters |
paul@19 | 127 | can be specified. |
paul@10 | 128 | """ |
paul@10 | 129 | |
paul@10 | 130 | category_names = request.form.get("category", []) |
paul@10 | 131 | |
paul@19 | 132 | # Otherwise, produce an iCalendar resource. |
paul@10 | 133 | |
paul@19 | 134 | calendar_start = EventAggregatorSupport.getFormMonth(request, None, "start") |
paul@19 | 135 | calendar_end = EventAggregatorSupport.getFormMonth(request, None, "end") |
paul@10 | 136 | |
paul@23 | 137 | # Look for separate start and end years and months. |
paul@23 | 138 | |
paul@23 | 139 | form = request.form |
paul@23 | 140 | |
paul@23 | 141 | if calendar_start is None: |
paul@23 | 142 | calendar_start = EventAggregatorSupport.getFormMonthPair(request, "start-year", "start-month") |
paul@23 | 143 | |
paul@23 | 144 | if calendar_end is None: |
paul@23 | 145 | calendar_end = EventAggregatorSupport.getFormMonthPair(request, "end-year", "end-month") |
paul@23 | 146 | |
paul@10 | 147 | events, shown_events, all_shown_events, earliest, latest = \ |
paul@10 | 148 | EventAggregatorSupport.getEvents(request, category_names, calendar_start, calendar_end) |
paul@10 | 149 | |
paul@10 | 150 | # Output iCalendar data... |
paul@10 | 151 | |
paul@10 | 152 | request.emit_http_headers(["Content-Type: text/calendar; charset=%s" % config.charset]) |
paul@10 | 153 | |
paul@10 | 154 | request.write("BEGIN:VCALENDAR\r\n") |
paul@10 | 155 | request.write("PRODID:-//MoinMoin//EventAggregatorSummary\r\n") |
paul@10 | 156 | request.write("VERSION:2.0\r\n") |
paul@10 | 157 | |
paul@10 | 158 | for event_page, event_details in all_shown_events: |
paul@10 | 159 | |
paul@19 | 160 | event_summary = EventAggregatorSupport.getEventSummary(event_page, event_details) |
paul@10 | 161 | |
paul@10 | 162 | # Output the event details. |
paul@10 | 163 | |
paul@10 | 164 | request.write("BEGIN:VEVENT\r\n") |
paul@19 | 165 | request.write("SUMMARY:%s\r\n" % event_summary) |
paul@10 | 166 | request.write("UID:%s\r\n" % request.getQualifiedURL(event_page.url(request))) |
paul@10 | 167 | request.write("URL:%s\r\n" % request.getQualifiedURL(event_page.url(request))) |
paul@10 | 168 | request.write("DTSTART;VALUE=DATE:%04d%02d%02d\r\n" % event_details["start"]) |
paul@10 | 169 | request.write("DTEND;VALUE=DATE:%04d%02d%02d\r\n" % EventAggregatorSupport.nextdate(event_details["end"])) |
paul@10 | 170 | if event_details.has_key("topics"): |
paul@10 | 171 | request.write("CATEGORIES:%s\r\n" % ",".join(event_details["topics"])) |
paul@10 | 172 | request.write("END:VEVENT\r\n") |
paul@10 | 173 | |
paul@10 | 174 | request.write("END:VCALENDAR\r\n") |
paul@10 | 175 | |
paul@19 | 176 | # Action function. |
paul@19 | 177 | |
paul@19 | 178 | def execute(pagename, request): |
paul@19 | 179 | EventAggregatorSummary(pagename, request).render() |
paul@19 | 180 | |
paul@10 | 181 | # vim: tabstop=4 expandtab shiftwidth=4 |