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