paul@46 | 1 | # -*- coding: iso-8859-1 -*- |
paul@46 | 2 | """ |
paul@46 | 3 | MoinMoin - EventAggregatorNewEvent Action |
paul@46 | 4 | |
paul@67 | 5 | @copyright: 2008, 2009, 2010 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@48 | 28 | def _get_selected(self, value, input_value): |
paul@48 | 29 | return input_value is not None and value == input_value and 'selected="selected"' or '' |
paul@46 | 30 | |
paul@48 | 31 | def _get_selected_for_list(self, value, input_values): |
paul@48 | 32 | return value in input_values and 'selected="selected"' or '' |
paul@48 | 33 | |
paul@48 | 34 | def _get_input(self, form, name, default): |
paul@48 | 35 | return int(form.get(name, [None])[0] or default) |
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@73 | 42 | # Handle advanced and basic forms. |
paul@73 | 43 | |
paul@73 | 44 | show_advanced = form.get("advanced") and not form.get("basic") |
paul@73 | 45 | |
paul@73 | 46 | # Prepare the category list. |
paul@73 | 47 | |
paul@46 | 48 | category_list = [] |
paul@48 | 49 | category_pagenames = form.get("category", []) |
paul@48 | 50 | |
paul@46 | 51 | for category_name, category_pagename in \ |
paul@46 | 52 | EventAggregatorSupport.getCategoryMapping( |
paul@46 | 53 | EventAggregatorSupport.getCategories(request), |
paul@46 | 54 | request): |
paul@46 | 55 | |
paul@48 | 56 | selected = self._get_selected_for_list(category_pagename, category_pagenames) |
paul@73 | 57 | |
paul@73 | 58 | # In the advanced view, populate a menu. |
paul@73 | 59 | |
paul@73 | 60 | if show_advanced: |
paul@73 | 61 | category_list.append('<option value="%s" %s>%s</option>' % (category_pagename, selected, category_name)) |
paul@73 | 62 | |
paul@73 | 63 | # In the basic view, use hidden fields. |
paul@73 | 64 | |
paul@73 | 65 | elif selected: |
paul@73 | 66 | category_list.append('<input value="%s" name="category" type="hidden" />' % category_pagename) |
paul@46 | 67 | |
paul@92 | 68 | # Prepare the topics list. |
paul@92 | 69 | |
paul@92 | 70 | topics = form.get("topics", []) |
paul@92 | 71 | |
paul@92 | 72 | if form.get("add-topic"): |
paul@92 | 73 | topics.append("") |
paul@92 | 74 | else: |
paul@92 | 75 | for i in range(0, len(topics)): |
paul@92 | 76 | if form.get("remove-topic-%d" % i): |
paul@92 | 77 | del topics[i] |
paul@92 | 78 | break |
paul@92 | 79 | |
paul@46 | 80 | # Initialise month lists. |
paul@46 | 81 | |
paul@46 | 82 | start_month_list = [] |
paul@46 | 83 | start_month_list.append('<option value=""></option>') |
paul@46 | 84 | end_month_list = [] |
paul@46 | 85 | end_month_list.append('<option value=""></option>') |
paul@46 | 86 | |
paul@48 | 87 | start_month = self._get_input(form, "start-month", 0) |
paul@48 | 88 | end_month = self._get_input(form, "end-month", 0) |
paul@46 | 89 | |
paul@46 | 90 | # Prepare month lists, selecting specified months. |
paul@46 | 91 | |
paul@46 | 92 | for month in range(1, 13): |
paul@46 | 93 | month_label = _(EventAggregatorSupport.getMonthLabel(month)) |
paul@46 | 94 | selected = self._get_selected(month, start_month) |
paul@46 | 95 | start_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) |
paul@46 | 96 | selected = self._get_selected(month, end_month) |
paul@46 | 97 | end_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) |
paul@46 | 98 | |
paul@49 | 99 | # Permitting configuration of the template name. |
paul@49 | 100 | |
paul@49 | 101 | template_default = getattr(request.cfg, "event_aggregator_new_event_template", "EventTemplate") |
paul@49 | 102 | |
paul@46 | 103 | d = { |
paul@49 | 104 | "buttons_html" : buttons_html, |
paul@49 | 105 | "category_label" : _("Categories"), |
paul@49 | 106 | "category_list" : "\n".join(category_list), |
paul@49 | 107 | "start_month_list" : "\n".join(start_month_list), |
paul@49 | 108 | "end_month_list" : "\n".join(end_month_list), |
paul@49 | 109 | "start_label" : _("Start date (day, month, year)"), |
paul@49 | 110 | "start_day_default" : form.get("start-day", [""])[0], |
paul@49 | 111 | "start_year_default" : form.get("start-year", [""])[0] or EventAggregatorSupport.getCurrentYear(), |
paul@55 | 112 | "end_label" : _("End date (day, month, year) - if different"), |
paul@49 | 113 | "end_day_default" : form.get("end-day", [""])[0], |
paul@49 | 114 | "end_year_default" : form.get("end-year", [""])[0], |
paul@49 | 115 | "title_label" : _("Event title/summary"), |
paul@49 | 116 | "title_default" : form.get("title", [""])[0], |
paul@49 | 117 | "description_label" : _("Event description"), |
paul@49 | 118 | "description_default" : form.get("description", [""])[0], |
paul@92 | 119 | "location_label" : _("Event location"), |
paul@92 | 120 | "location_default" : form.get("location", [""])[0], |
paul@92 | 121 | "link_label" : _("Event URL"), |
paul@92 | 122 | "link_default" : form.get("link", [""])[0], |
paul@92 | 123 | "topics_label" : _("Topics"), |
paul@92 | 124 | "add_topic_label" : _("Add topic"), |
paul@92 | 125 | "remove_topic_label" : _("Remove topic"), |
paul@49 | 126 | "template_label" : _("Event template"), |
paul@49 | 127 | "template_default" : form.get("template", [""])[0] or template_default, |
paul@55 | 128 | "parent_label" : _("Parent page"), |
paul@55 | 129 | "parent_default" : form.get("parent", [""])[0], |
paul@73 | 130 | "advanced_label" : _("Show advanced options"), |
paul@73 | 131 | "basic_label" : _("Show basic options"), |
paul@46 | 132 | } |
paul@46 | 133 | |
paul@73 | 134 | # Prepare the output HTML. |
paul@73 | 135 | |
paul@73 | 136 | html = ''' |
paul@46 | 137 | <table> |
paul@46 | 138 | <tr> |
paul@46 | 139 | <td class="label"><label>%(title_label)s</label></td> |
paul@46 | 140 | <td> |
paul@48 | 141 | <input name="title" type="text" size="40" value="%(title_default)s" /> |
paul@48 | 142 | </td> |
paul@48 | 143 | </tr> |
paul@46 | 144 | <tr> |
paul@46 | 145 | <td class="label"><label>%(start_label)s</label></td> |
paul@46 | 146 | <td> |
paul@46 | 147 | <input name="start-day" type="text" value="%(start_day_default)s" size="2" /> |
paul@46 | 148 | <select name="start-month"> |
paul@46 | 149 | %(start_month_list)s |
paul@46 | 150 | </select> |
paul@46 | 151 | <input name="start-year" type="text" value="%(start_year_default)s" size="4" /> |
paul@46 | 152 | </td> |
paul@46 | 153 | </tr> |
paul@46 | 154 | <tr> |
paul@46 | 155 | <td class="label"><label>%(end_label)s</label></td> |
paul@46 | 156 | <td> |
paul@46 | 157 | <input name="end-day" type="text" value="%(end_day_default)s" size="2" /> |
paul@46 | 158 | <select name="end-month"> |
paul@46 | 159 | %(end_month_list)s |
paul@46 | 160 | </select> |
paul@46 | 161 | <input name="end-year" type="text" value="%(end_year_default)s" size="4" /> |
paul@46 | 162 | </td> |
paul@46 | 163 | </tr> |
paul@46 | 164 | <tr> |
paul@49 | 165 | <td class="label"><label>%(description_label)s</label></td> |
paul@49 | 166 | <td> |
paul@49 | 167 | <input name="description" type="text" size="40" value="%(description_default)s" /> |
paul@49 | 168 | </td> |
paul@92 | 169 | </tr> |
paul@92 | 170 | <tr> |
paul@92 | 171 | <td class="label"><label>%(location_label)s</label></td> |
paul@92 | 172 | <td> |
paul@92 | 173 | <input name="location" type="text" size="40" value="%(location_default)s" /> |
paul@92 | 174 | </td> |
paul@92 | 175 | </tr> |
paul@92 | 176 | <tr> |
paul@92 | 177 | <tr> |
paul@92 | 178 | <td class="label"><label>%(link_label)s</label></td> |
paul@92 | 179 | <td> |
paul@92 | 180 | <input name="link" type="text" size="40" value="%(link_default)s" /> |
paul@92 | 181 | </td> |
paul@73 | 182 | </tr>''' % d |
paul@73 | 183 | |
paul@92 | 184 | # Topics. |
paul@92 | 185 | |
paul@92 | 186 | for i, topic in enumerate(topics): |
paul@92 | 187 | d["topic"] = topic |
paul@92 | 188 | d["topic_number"] = i |
paul@92 | 189 | html += ''' |
paul@92 | 190 | <tr> |
paul@92 | 191 | <td class="label"><label>%(topics_label)s</label></td> |
paul@92 | 192 | <td> |
paul@92 | 193 | <input name="topics" type="text" size="20" value="%(topic)s" /> |
paul@92 | 194 | <input name="remove-topic-%(topic_number)s" type="submit" value="%(remove_topic_label)s" /> |
paul@92 | 195 | </td> |
paul@92 | 196 | </tr>''' % d |
paul@92 | 197 | |
paul@92 | 198 | html += ''' |
paul@92 | 199 | <tr> |
paul@92 | 200 | <td></td> |
paul@92 | 201 | <td> |
paul@92 | 202 | <input name="add-topic" type="submit" value="%(add_topic_label)s" /> |
paul@92 | 203 | </td> |
paul@92 | 204 | </tr>''' % d |
paul@92 | 205 | |
paul@92 | 206 | # Advanced options. |
paul@92 | 207 | |
paul@73 | 208 | if show_advanced: |
paul@73 | 209 | html += ''' |
paul@73 | 210 | <tr> |
paul@73 | 211 | <td></td> |
paul@73 | 212 | <td> |
paul@73 | 213 | <input name="basic" type="submit" value="%(basic_label)s" /> |
paul@73 | 214 | <input name="advanced" type="hidden" value="true" /> |
paul@73 | 215 | </td> |
paul@73 | 216 | </tr> |
paul@73 | 217 | <tr> |
paul@73 | 218 | <td class="label"><label>%(category_label)s</label></td> |
paul@73 | 219 | <td class="content"> |
paul@73 | 220 | <select multiple="multiple" name="category"> |
paul@73 | 221 | %(category_list)s |
paul@73 | 222 | </select> |
paul@73 | 223 | </td> |
paul@49 | 224 | </tr> |
paul@49 | 225 | <tr> |
paul@49 | 226 | <td class="label"><label>%(template_label)s</label></td> |
paul@49 | 227 | <td> |
paul@49 | 228 | <input name="template" type="text" size="40" value="%(template_default)s" /> |
paul@49 | 229 | </td> |
paul@49 | 230 | </tr> |
paul@49 | 231 | <tr> |
paul@55 | 232 | <td class="label"><label>%(parent_label)s</label></td> |
paul@55 | 233 | <td> |
paul@55 | 234 | <input name="parent" type="text" size="40" value="%(parent_default)s" /> |
paul@55 | 235 | </td> |
paul@55 | 236 | </tr> |
paul@55 | 237 | <tr> |
paul@46 | 238 | <td></td> |
paul@46 | 239 | <td class="buttons"> |
paul@46 | 240 | %(buttons_html)s |
paul@46 | 241 | </td> |
paul@46 | 242 | </tr> |
paul@73 | 243 | </table>''' % d |
paul@73 | 244 | else: |
paul@73 | 245 | html += ''' |
paul@73 | 246 | <tr> |
paul@73 | 247 | <td></td> |
paul@73 | 248 | <td> |
paul@73 | 249 | <input name="advanced" type="submit" value="%(advanced_label)s" /> |
paul@73 | 250 | %(category_list)s |
paul@73 | 251 | <input name="parent" type="hidden" value="%(parent_default)s" /> |
paul@73 | 252 | <input name="template" type="hidden" value="%(template_default)s" /> |
paul@73 | 253 | </td> |
paul@73 | 254 | </tr> |
paul@73 | 255 | <tr> |
paul@73 | 256 | <td></td> |
paul@73 | 257 | <td class="buttons"> |
paul@73 | 258 | %(buttons_html)s |
paul@73 | 259 | </td> |
paul@73 | 260 | </tr> |
paul@73 | 261 | </table>''' % d |
paul@73 | 262 | |
paul@73 | 263 | return html |
paul@46 | 264 | |
paul@46 | 265 | def do_action(self): |
paul@46 | 266 | |
paul@46 | 267 | "Create the new event." |
paul@46 | 268 | |
paul@46 | 269 | _ = self._ |
paul@46 | 270 | form = self.request.form |
paul@46 | 271 | |
paul@46 | 272 | # If no title exists in the request, an error message is returned. |
paul@46 | 273 | |
paul@46 | 274 | title = form.get("title", [None])[0] |
paul@49 | 275 | template = form.get("template", [None])[0] |
paul@46 | 276 | |
paul@46 | 277 | if not title: |
paul@46 | 278 | return 0, _("No event title specified.") |
paul@46 | 279 | |
paul@49 | 280 | if not template: |
paul@49 | 281 | return 0, _("No page template specified.") |
paul@49 | 282 | |
paul@46 | 283 | return self.create_event(self.request) |
paul@46 | 284 | |
paul@46 | 285 | def render_success(self, msg, msgtype=None): |
paul@46 | 286 | |
paul@46 | 287 | """ |
paul@46 | 288 | Render neither 'msg' nor 'msgtype' since redirection should occur |
paul@46 | 289 | instead. |
paul@46 | 290 | NOTE: msgtype is optional because MoinMoin 1.5.x does not support it. |
paul@46 | 291 | """ |
paul@46 | 292 | |
paul@46 | 293 | pass |
paul@46 | 294 | |
paul@46 | 295 | def create_event(self, request): |
paul@46 | 296 | |
paul@46 | 297 | "Create an event page using the 'request'." |
paul@46 | 298 | |
paul@46 | 299 | _ = request.getText |
paul@47 | 300 | form = request.form |
paul@46 | 301 | |
paul@48 | 302 | category_pagenames = form.get("category", []) |
paul@49 | 303 | description = form.get("description", [None])[0] |
paul@92 | 304 | location = form.get("location", [None])[0] |
paul@92 | 305 | link = form.get("link", [None])[0] |
paul@92 | 306 | topics = form.get("topics", []) |
paul@92 | 307 | |
paul@92 | 308 | # Validate certain fields. |
paul@46 | 309 | |
paul@46 | 310 | try: |
paul@49 | 311 | title = form["title"][0] |
paul@49 | 312 | template = form["template"][0] |
paul@55 | 313 | parent = form["parent"][0] |
paul@49 | 314 | |
paul@48 | 315 | start_day = self._get_input(form, "start-day", 0) |
paul@48 | 316 | start_month = self._get_input(form, "start-month", 0) |
paul@48 | 317 | start_year = self._get_input(form, "start-year", 0) |
paul@48 | 318 | end_day = self._get_input(form, "end-day", start_day) |
paul@48 | 319 | end_month = self._get_input(form, "end-month", start_month) |
paul@48 | 320 | end_year = self._get_input(form, "end-year", start_year) |
paul@46 | 321 | |
paul@46 | 322 | start_year, start_month, start_day = self.constrain_date(start_year, start_month, start_day) |
paul@46 | 323 | end_year, end_month, end_day = self.constrain_date(end_year, end_month, end_day) |
paul@46 | 324 | |
paul@48 | 325 | except (TypeError, ValueError): |
paul@46 | 326 | return 0, _("Days and years must be numbers yielding a valid date!") |
paul@46 | 327 | |
paul@49 | 328 | except (KeyError, IndexError): |
paul@49 | 329 | return 0, _("Event title or template missing.") |
paul@49 | 330 | |
paul@46 | 331 | start_date = self.make_date_string(start_year, start_month, start_day) |
paul@46 | 332 | end_date = self.make_date_string(end_year, end_month, end_day) |
paul@46 | 333 | |
paul@78 | 334 | # An elementary date ordering check. |
paul@78 | 335 | |
paul@78 | 336 | if start_date > end_date: |
paul@78 | 337 | start_date, end_date = end_date, start_date |
paul@78 | 338 | |
paul@88 | 339 | event_details = { |
paul@88 | 340 | "start" : start_date, "end" : end_date, |
paul@88 | 341 | "title" : title, "summary" : title, |
paul@92 | 342 | "description" : description, "location" : location, "link" : link, |
paul@92 | 343 | "topics" : [topic for topic in topics if topic] |
paul@88 | 344 | } |
paul@88 | 345 | |
paul@46 | 346 | # Copy the template. |
paul@49 | 347 | |
paul@88 | 348 | template_page = PageEditor(request, template) |
paul@46 | 349 | |
paul@88 | 350 | if not template_page.exists(): |
paul@49 | 351 | return 0, _("Event template not available.") |
paul@49 | 352 | |
paul@55 | 353 | # Use any parent page information. |
paul@55 | 354 | |
paul@88 | 355 | full_title = EventAggregatorSupport.getFullPageName(parent, title) |
paul@55 | 356 | |
paul@47 | 357 | # Load the new page and replace the event details in the body. |
paul@46 | 358 | |
paul@64 | 359 | new_page = PageEditor(request, full_title) |
paul@46 | 360 | |
paul@46 | 361 | if new_page.exists(): |
paul@46 | 362 | return 0, _("The specified page already exists. Please choose another name.") |
paul@46 | 363 | |
paul@88 | 364 | # Complete the new page. |
paul@67 | 365 | |
paul@88 | 366 | EventAggregatorSupport.fillEventPageFromTemplate(template_page, |
paul@88 | 367 | new_page, event_details, category_pagenames) |
paul@46 | 368 | |
paul@46 | 369 | # Redirect and return success. |
paul@46 | 370 | |
paul@47 | 371 | query = {'action' : 'edit'} |
paul@46 | 372 | request.http_redirect(new_page.url(request, query)) |
paul@46 | 373 | return 1, None |
paul@46 | 374 | |
paul@46 | 375 | def make_date_string(self, year, month, day): |
paul@46 | 376 | return "%s-%s-%s" % ( |
paul@46 | 377 | year and ("%04d" % year) or "YYYY", |
paul@46 | 378 | month and ("%02d" % month) or "MM", |
paul@46 | 379 | day and ("%02d" % day) or "DD" |
paul@46 | 380 | ) |
paul@46 | 381 | |
paul@46 | 382 | def constrain_date(self, year, month, day): |
paul@46 | 383 | if year and month and day: |
paul@46 | 384 | wd, last_day = calendar.monthrange(year, month) |
paul@46 | 385 | day = max(min(day, last_day), 1) |
paul@46 | 386 | return year, month, day |
paul@46 | 387 | |
paul@46 | 388 | # Action function. |
paul@46 | 389 | |
paul@46 | 390 | def execute(pagename, request): |
paul@46 | 391 | EventAggregatorNewEvent(pagename, request).render() |
paul@46 | 392 | |
paul@46 | 393 | # vim: tabstop=4 expandtab shiftwidth=4 |