paul@46 | 1 | # -*- coding: iso-8859-1 -*- |
paul@46 | 2 | """ |
paul@46 | 3 | MoinMoin - EventAggregatorNewEvent Action |
paul@46 | 4 | |
paul@137 | 5 | @copyright: 2008, 2009, 2010, 2011 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@238 | 16 | from MoinMoin import config |
paul@168 | 17 | from EventAggregatorSupport import * |
paul@137 | 18 | |
paul@99 | 19 | try: |
paul@99 | 20 | import pytz |
paul@99 | 21 | except ImportError: |
paul@99 | 22 | pytz = None |
paul@99 | 23 | |
paul@46 | 24 | Dependencies = ['pages'] |
paul@46 | 25 | |
paul@46 | 26 | # Action class and supporting functions. |
paul@46 | 27 | |
paul@168 | 28 | class EventAggregatorNewEvent(ActionBase, ActionSupport): |
paul@46 | 29 | |
paul@46 | 30 | "An event creation dialogue requesting various parameters." |
paul@46 | 31 | |
paul@46 | 32 | def get_form_html(self, buttons_html): |
paul@46 | 33 | _ = self._ |
paul@46 | 34 | request = self.request |
paul@109 | 35 | form = self.get_form() |
paul@46 | 36 | |
paul@99 | 37 | # Handle advanced and basic forms, and enable/disable certain fields. |
paul@73 | 38 | |
paul@73 | 39 | show_advanced = form.get("advanced") and not form.get("basic") |
paul@99 | 40 | show_end_date = form.get("end-day") and not form.get("hide-end-date") or form.get("show-end-date") |
paul@99 | 41 | show_times = (form.get("start-hour") or form.get("end-hour")) and not form.get("hide-times") or form.get("show-times") |
paul@294 | 42 | show_location = form.get("show-location") or form.get("new-location") and not form.get("hide-location") |
paul@73 | 43 | |
paul@73 | 44 | # Prepare the category list. |
paul@73 | 45 | |
paul@46 | 46 | category_list = [] |
paul@48 | 47 | category_pagenames = form.get("category", []) |
paul@48 | 48 | |
paul@168 | 49 | for category_name, category_pagename in getCategoryMapping(getCategories(request), request): |
paul@46 | 50 | |
paul@48 | 51 | selected = self._get_selected_for_list(category_pagename, category_pagenames) |
paul@73 | 52 | |
paul@73 | 53 | # In the advanced view, populate a menu. |
paul@73 | 54 | |
paul@73 | 55 | if show_advanced: |
paul@137 | 56 | category_list.append('<option value="%s" %s>%s</option>' % ( |
paul@137 | 57 | escattr(category_pagename), selected, escape(category_name))) |
paul@73 | 58 | |
paul@73 | 59 | # In the basic view, use hidden fields. |
paul@73 | 60 | |
paul@73 | 61 | elif selected: |
paul@137 | 62 | category_list.append('<input value="%s" name="category" type="hidden" />' % escattr(category_pagename)) |
paul@46 | 63 | |
paul@92 | 64 | # Prepare the topics list. |
paul@92 | 65 | |
paul@92 | 66 | topics = form.get("topics", []) |
paul@92 | 67 | |
paul@92 | 68 | if form.get("add-topic"): |
paul@92 | 69 | topics.append("") |
paul@92 | 70 | else: |
paul@92 | 71 | for i in range(0, len(topics)): |
paul@92 | 72 | if form.get("remove-topic-%d" % i): |
paul@92 | 73 | del topics[i] |
paul@92 | 74 | break |
paul@92 | 75 | |
paul@46 | 76 | # Initialise month lists. |
paul@46 | 77 | |
paul@114 | 78 | start_month_list, end_month_list = self.get_month_lists(default_as_current=1) |
paul@114 | 79 | start_year_default, end_year_default = self.get_year_defaults(default_as_current=1) |
paul@46 | 80 | |
paul@294 | 81 | # Initialise the locations list. |
paul@294 | 82 | |
paul@294 | 83 | locations_page = getLocationsPage(request) |
paul@294 | 84 | locations = getWikiDict(locations_page, request) |
paul@294 | 85 | |
paul@294 | 86 | locations_list = [] |
paul@294 | 87 | locations_list.append('<option value=""></option>') |
paul@294 | 88 | |
paul@294 | 89 | location = (form.get("location") or form.get("new-location") or [""])[0] |
paul@294 | 90 | |
paul@294 | 91 | # Prepare the locations list, selecting the specified location. |
paul@294 | 92 | |
paul@294 | 93 | if locations: |
paul@294 | 94 | locations_list += self.get_option_list(location, locations) or [] |
paul@294 | 95 | |
paul@294 | 96 | locations_list.sort() |
paul@294 | 97 | |
paul@294 | 98 | # Initialise the time regime from the location. |
paul@294 | 99 | |
paul@294 | 100 | regime = form.get("regime", [None])[0] |
paul@294 | 101 | |
paul@294 | 102 | if not regime: |
paul@294 | 103 | regime = Location(location, locations).getTimeRegime() |
paul@294 | 104 | |
paul@99 | 105 | # Initialise regime lists. |
paul@99 | 106 | |
paul@99 | 107 | regime_list = [] |
paul@294 | 108 | regime_list.append('<option value="">%s</option>' % escape(_("<The event location (if known)>"))) |
paul@99 | 109 | |
paul@99 | 110 | # Prepare regime lists, selecting specified regimes. |
paul@99 | 111 | |
paul@99 | 112 | if pytz is not None: |
paul@294 | 113 | regime_list += self.get_option_list(regime, pytz.common_timezones) |
paul@294 | 114 | |
paul@294 | 115 | # Show time zone-related information depending on various fields. |
paul@294 | 116 | |
paul@294 | 117 | show_zone_regime = ( |
paul@294 | 118 | (form.get("regime") or regime) # have a regime value |
paul@294 | 119 | and not form.get("show-offsets") # are not switching to offsets |
paul@294 | 120 | and not form.get("hide-zone") # are not removing zone information |
paul@294 | 121 | or form.get("show-regime") # are switching to a regime |
paul@294 | 122 | ) |
paul@294 | 123 | show_zone_offsets = ( |
paul@294 | 124 | form.get("start-offset") # have an offset |
paul@294 | 125 | and not form.get("show-regime") # are not switching to a regime |
paul@294 | 126 | and not form.get("hide-zone") # are not removing zone information |
paul@294 | 127 | or form.get("show-offsets") # are switching to offsets |
paul@294 | 128 | ) |
paul@294 | 129 | |
paul@294 | 130 | show_zones = show_zone_regime or show_zone_offsets |
paul@99 | 131 | |
paul@49 | 132 | # Permitting configuration of the template name. |
paul@49 | 133 | |
paul@49 | 134 | template_default = getattr(request.cfg, "event_aggregator_new_event_template", "EventTemplate") |
paul@49 | 135 | |
paul@46 | 136 | d = { |
paul@49 | 137 | "buttons_html" : buttons_html, |
paul@239 | 138 | "form_trigger" : escattr(self.form_trigger), |
paul@239 | 139 | "form_cancel" : escattr(self.form_cancel), |
paul@239 | 140 | |
paul@137 | 141 | "category_label" : escape(_("Categories")), |
paul@49 | 142 | "category_list" : "\n".join(category_list), |
paul@96 | 143 | |
paul@49 | 144 | "start_month_list" : "\n".join(start_month_list), |
paul@99 | 145 | "end_month_list" : "\n".join(end_month_list), |
paul@99 | 146 | |
paul@99 | 147 | "regime_list" : "\n".join(regime_list), |
paul@294 | 148 | "use_regime_label" : escape(_("Using local time in...")), |
paul@96 | 149 | |
paul@137 | 150 | "show_end_date_label" : escape(_("Specify end date")), |
paul@137 | 151 | "hide_end_date_label" : escape(_("End event on same day")), |
paul@99 | 152 | |
paul@137 | 153 | "show_times_label" : escape(_("Specify times")), |
paul@137 | 154 | "hide_times_label" : escape(_("No start and end times")), |
paul@99 | 155 | |
paul@137 | 156 | "show_offsets_label" : escape(_("Specify UTC offsets")), |
paul@294 | 157 | "show_regime_label" : escape(_("Use local time")), |
paul@137 | 158 | "hide_zone_label" : escape(_("Make times apply everywhere")), |
paul@96 | 159 | |
paul@137 | 160 | "start_label" : escape(_("Start date (day, month, year)")), |
paul@137 | 161 | "start_day_default" : escattr(form.get("start-day", [""])[0]), |
paul@148 | 162 | "start_year_default" : escattr(start_year_default), |
paul@137 | 163 | "start_time_label" : escape(_("Start time (hour, minute, second)")), |
paul@137 | 164 | "start_hour_default" : escattr(form.get("start-hour", [""])[0]), |
paul@137 | 165 | "start_minute_default" : escattr(form.get("start-minute", [""])[0]), |
paul@137 | 166 | "start_second_default" : escattr(form.get("start-second", [""])[0]), |
paul@137 | 167 | "start_offset_default" : escattr(form.get("start-offset", [""])[0]), |
paul@96 | 168 | |
paul@137 | 169 | "end_label" : escape(_("End date (day, month, year) - if different")), |
paul@243 | 170 | "end_day_default" : escattr(form.get("end-day", [""])[0].strip() or form.get("start-day", [""])[0]), |
paul@148 | 171 | "end_year_default" : escattr(end_year_default), |
paul@137 | 172 | "end_time_label" : escape(_("End time (hour, minute, second)")), |
paul@137 | 173 | "end_hour_default" : escattr(form.get("end-hour", [""])[0]), |
paul@137 | 174 | "end_minute_default" : escattr(form.get("end-minute", [""])[0]), |
paul@137 | 175 | "end_second_default" : escattr(form.get("end-second", [""])[0]), |
paul@243 | 176 | "end_offset_default" : escattr(form.get("end-offset", [""])[0].strip() or form.get("start-offset", [""])[0]), |
paul@96 | 177 | |
paul@137 | 178 | "title_label" : escape(_("Event title/summary")), |
paul@137 | 179 | "title_default" : escattr(form.get("title", [""])[0]), |
paul@137 | 180 | "description_label" : escape(_("Event description")), |
paul@137 | 181 | "description_default" : escattr(form.get("description", [""])[0]), |
paul@294 | 182 | |
paul@137 | 183 | "location_label" : escape(_("Event location")), |
paul@294 | 184 | "locations_list" : "\n".join(locations_list), |
paul@294 | 185 | "show_location_label" : escattr(_("Specify a different location")), |
paul@294 | 186 | "hide_location_label" : escattr(_("Choose a known location")), |
paul@294 | 187 | "new_location" : escattr(form.get("new-location", [location])[0]), |
paul@294 | 188 | |
paul@230 | 189 | "latitude_label" : escape(_("Latitude")), |
paul@230 | 190 | "latitude_default" : escattr(form.get("latitude", [""])[0]), |
paul@230 | 191 | "longitude_label" : escape(_("Longitude")), |
paul@230 | 192 | "longitude_default" : escattr(form.get("longitude", [""])[0]), |
paul@243 | 193 | "link_label" : escape(_("External URL")), |
paul@137 | 194 | "link_default" : escattr(form.get("link", [""])[0]), |
paul@96 | 195 | |
paul@137 | 196 | "topics_label" : escape(_("Topics")), |
paul@137 | 197 | "add_topic_label" : escape(_("Add topic")), |
paul@137 | 198 | "remove_topic_label" : escape(_("Remove topic")), |
paul@96 | 199 | |
paul@137 | 200 | "template_label" : escape(_("Event template")), |
paul@243 | 201 | "template_default" : escattr(form.get("template", [""])[0].strip() or template_default), |
paul@137 | 202 | "parent_label" : escape(_("Parent page")), |
paul@137 | 203 | "parent_default" : escattr(form.get("parent", [""])[0]), |
paul@96 | 204 | |
paul@137 | 205 | "advanced_label" : escape(_("Show advanced options")), |
paul@137 | 206 | "basic_label" : escape(_("Hide advanced options")), |
paul@46 | 207 | } |
paul@46 | 208 | |
paul@73 | 209 | # Prepare the output HTML. |
paul@73 | 210 | |
paul@73 | 211 | html = ''' |
paul@238 | 212 | <input name="update-form-only" value="false" type="hidden" /> |
paul@46 | 213 | <table> |
paul@46 | 214 | <tr> |
paul@46 | 215 | <td class="label"><label>%(title_label)s</label></td> |
paul@99 | 216 | <td colspan="2"> |
paul@48 | 217 | <input name="title" type="text" size="40" value="%(title_default)s" /> |
paul@48 | 218 | </td> |
paul@294 | 219 | </tr>''' % d |
paul@294 | 220 | |
paul@294 | 221 | # Location options. |
paul@294 | 222 | |
paul@294 | 223 | html += ''' |
paul@294 | 224 | <tr> |
paul@294 | 225 | <td class="label"><label>%(location_label)s</label></td> |
paul@294 | 226 | <td colspan="2">''' % d |
paul@294 | 227 | |
paul@294 | 228 | if not show_location: |
paul@294 | 229 | html += ''' |
paul@294 | 230 | <select name="location"> |
paul@294 | 231 | %(locations_list)s |
paul@294 | 232 | </select> |
paul@294 | 233 | <input name="show-location" type="submit" value="%(show_location_label)s" />''' % d |
paul@294 | 234 | |
paul@294 | 235 | else: |
paul@294 | 236 | html += ''' |
paul@294 | 237 | <input name="new-location" type="text" size="30" value="%(new_location)s" /> |
paul@294 | 238 | <input name="hide-location" type="submit" value="%(hide_location_label)s" />''' % d |
paul@294 | 239 | |
paul@294 | 240 | html += ''' |
paul@294 | 241 | </td> |
paul@294 | 242 | </tr>''' % d |
paul@294 | 243 | |
paul@294 | 244 | # Latitude and longitude. |
paul@294 | 245 | |
paul@294 | 246 | if show_location: |
paul@294 | 247 | html += ''' |
paul@294 | 248 | <tr> |
paul@294 | 249 | <td class="label"><label>%(latitude_label)s</label></td> |
paul@294 | 250 | <td colspan="2"> |
paul@294 | 251 | <input name="latitude" type="text" size="40" value="%(latitude_default)s" /> |
paul@294 | 252 | </td> |
paul@48 | 253 | </tr> |
paul@46 | 254 | <tr> |
paul@294 | 255 | <td class="label"><label>%(longitude_label)s</label></td> |
paul@294 | 256 | <td colspan="2"> |
paul@294 | 257 | <input name="longitude" type="text" size="40" value="%(longitude_default)s" /> |
paul@294 | 258 | </td> |
paul@294 | 259 | </tr>''' % d |
paul@294 | 260 | |
paul@294 | 261 | # Start date controls. |
paul@294 | 262 | |
paul@294 | 263 | html += ''' |
paul@294 | 264 | <tr> |
paul@46 | 265 | <td class="label"><label>%(start_label)s</label></td> |
paul@99 | 266 | <td colspan="2" style="white-space: nowrap"> |
paul@46 | 267 | <input name="start-day" type="text" value="%(start_day_default)s" size="2" /> |
paul@46 | 268 | <select name="start-month"> |
paul@46 | 269 | %(start_month_list)s |
paul@46 | 270 | </select> |
paul@46 | 271 | <input name="start-year" type="text" value="%(start_year_default)s" size="4" /> |
paul@46 | 272 | </td> |
paul@99 | 273 | </tr>''' % d |
paul@99 | 274 | |
paul@99 | 275 | # End date controls. |
paul@99 | 276 | |
paul@99 | 277 | if show_end_date: |
paul@99 | 278 | html += ''' |
paul@96 | 279 | <tr> |
paul@46 | 280 | <td class="label"><label>%(end_label)s</label></td> |
paul@99 | 281 | <td colspan="2" style="white-space: nowrap"> |
paul@46 | 282 | <input name="end-day" type="text" value="%(end_day_default)s" size="2" /> |
paul@46 | 283 | <select name="end-month"> |
paul@46 | 284 | %(end_month_list)s |
paul@46 | 285 | </select> |
paul@46 | 286 | <input name="end-year" type="text" value="%(end_year_default)s" size="4" /> |
paul@46 | 287 | </td> |
paul@46 | 288 | </tr> |
paul@46 | 289 | <tr> |
paul@99 | 290 | <td class="label"> |
paul@99 | 291 | <input name="hide-end-date" type="submit" value="%(hide_end_date_label)s" /> |
paul@99 | 292 | </td> |
paul@99 | 293 | </tr>''' % d |
paul@99 | 294 | else: |
paul@99 | 295 | html += ''' |
paul@99 | 296 | <tr> |
paul@99 | 297 | <td class="label"> |
paul@99 | 298 | <input name="show-end-date" type="submit" value="%(show_end_date_label)s" /> |
paul@99 | 299 | </td> |
paul@99 | 300 | </tr>''' % d |
paul@99 | 301 | |
paul@99 | 302 | # Generic time information. |
paul@99 | 303 | |
paul@99 | 304 | if show_times: |
paul@99 | 305 | |
paul@99 | 306 | # Start time controls. |
paul@99 | 307 | |
paul@99 | 308 | html += ''' |
paul@99 | 309 | <tr> |
paul@99 | 310 | <td class="label"><label>%(start_time_label)s</label></td> |
paul@99 | 311 | <td style="white-space: nowrap"> |
paul@99 | 312 | <input name="start-hour" type="text" value="%(start_hour_default)s" size="2" maxlength="2" /> |
paul@99 | 313 | <input name="start-minute" type="text" value="%(start_minute_default)s" size="2" maxlength="2" /> |
paul@99 | 314 | <input name="start-second" type="text" value="%(start_second_default)s" size="2" maxlength="2" /> |
paul@99 | 315 | </td>''' % d |
paul@99 | 316 | |
paul@99 | 317 | # Offset information displayed. |
paul@99 | 318 | |
paul@99 | 319 | if show_zone_offsets: |
paul@99 | 320 | html += ''' |
paul@96 | 321 | <td> |
paul@99 | 322 | UTC <input name="start-offset" type="text" value="%(start_offset_default)s" size="6" maxlength="6" /> |
paul@99 | 323 | </td>''' % d |
paul@99 | 324 | |
paul@99 | 325 | # Regime information displayed. |
paul@99 | 326 | |
paul@99 | 327 | elif show_zone_regime: |
paul@99 | 328 | html += ''' |
paul@294 | 329 | <td class="event-regime-selection" rowspan="2"> |
paul@294 | 330 | <label>%(use_regime_label)s</label><br/> |
paul@294 | 331 | <select name="regime"> |
paul@294 | 332 | %(regime_list)s |
paul@294 | 333 | </select> |
paul@99 | 334 | </td>''' % d |
paul@99 | 335 | |
paul@99 | 336 | html += ''' |
paul@99 | 337 | </tr>''' |
paul@99 | 338 | |
paul@99 | 339 | # End time controls. |
paul@99 | 340 | |
paul@99 | 341 | html += ''' |
paul@99 | 342 | <tr> |
paul@99 | 343 | <td class="label"><label>%(end_time_label)s</label></td> |
paul@99 | 344 | <td style="white-space: nowrap"> |
paul@99 | 345 | <input name="end-hour" type="text" value="%(end_hour_default)s" size="2" maxlength="2" /> |
paul@99 | 346 | <input name="end-minute" type="text" value="%(end_minute_default)s" size="2" maxlength="2" /> |
paul@99 | 347 | <input name="end-second" type="text" value="%(end_second_default)s" size="2" maxlength="2" /> |
paul@99 | 348 | </td>''' % d |
paul@99 | 349 | |
paul@99 | 350 | # Offset information displayed. |
paul@99 | 351 | |
paul@99 | 352 | if show_zone_offsets: |
paul@99 | 353 | html += ''' |
paul@99 | 354 | <td> |
paul@99 | 355 | UTC <input name="end-offset" type="text" value="%(end_offset_default)s" size="6" maxlength="6" /> |
paul@99 | 356 | </td>''' % d |
paul@99 | 357 | |
paul@99 | 358 | # Controls for removing times. |
paul@99 | 359 | |
paul@99 | 360 | html += ''' |
paul@96 | 361 | </tr> |
paul@96 | 362 | <tr> |
paul@99 | 363 | <td class="label"> |
paul@99 | 364 | <input name="hide-times" type="submit" value="%(hide_times_label)s" /> |
paul@99 | 365 | </td> |
paul@294 | 366 | <td></td> |
paul@294 | 367 | <td>''' % d |
paul@99 | 368 | |
paul@99 | 369 | # Time zone controls. |
paul@99 | 370 | |
paul@99 | 371 | if show_zones: |
paul@99 | 372 | |
paul@99 | 373 | # To remove zone information. |
paul@99 | 374 | |
paul@99 | 375 | html += ''' |
paul@294 | 376 | <input name="hide-zone" type="submit" value="%(hide_zone_label)s" />''' % d |
paul@99 | 377 | |
paul@99 | 378 | # No time zone information shown. |
paul@99 | 379 | |
paul@99 | 380 | else: |
paul@99 | 381 | html += ''' |
paul@99 | 382 | <input name="show-regime" type="submit" value="%(show_regime_label)s" /> |
paul@294 | 383 | <input name="show-offsets" type="submit" value="%(show_offsets_label)s" />''' % d |
paul@99 | 384 | |
paul@99 | 385 | html += ''' |
paul@294 | 386 | </td> |
paul@99 | 387 | </tr>''' |
paul@99 | 388 | |
paul@99 | 389 | # Controls for adding times. |
paul@99 | 390 | |
paul@99 | 391 | else: |
paul@99 | 392 | html += ''' |
paul@99 | 393 | <tr> |
paul@99 | 394 | <td class="label"> |
paul@99 | 395 | <input name="show-times" type="submit" value="%(show_times_label)s" /> |
paul@99 | 396 | </td> |
paul@99 | 397 | </tr>''' % d |
paul@99 | 398 | |
paul@99 | 399 | |
paul@99 | 400 | # Various basic controls. |
paul@99 | 401 | |
paul@99 | 402 | html += ''' |
paul@99 | 403 | <tr> |
paul@49 | 404 | <td class="label"><label>%(description_label)s</label></td> |
paul@99 | 405 | <td colspan="2"> |
paul@49 | 406 | <input name="description" type="text" size="40" value="%(description_default)s" /> |
paul@49 | 407 | </td> |
paul@92 | 408 | </tr> |
paul@92 | 409 | <tr> |
paul@92 | 410 | <td class="label"><label>%(link_label)s</label></td> |
paul@99 | 411 | <td colspan="2"> |
paul@92 | 412 | <input name="link" type="text" size="40" value="%(link_default)s" /> |
paul@92 | 413 | </td> |
paul@73 | 414 | </tr>''' % d |
paul@73 | 415 | |
paul@92 | 416 | # Topics. |
paul@92 | 417 | |
paul@92 | 418 | for i, topic in enumerate(topics): |
paul@137 | 419 | d["topic"] = escattr(topic) |
paul@92 | 420 | d["topic_number"] = i |
paul@92 | 421 | html += ''' |
paul@92 | 422 | <tr> |
paul@92 | 423 | <td class="label"><label>%(topics_label)s</label></td> |
paul@99 | 424 | <td colspan="2"> |
paul@92 | 425 | <input name="topics" type="text" size="20" value="%(topic)s" /> |
paul@92 | 426 | <input name="remove-topic-%(topic_number)s" type="submit" value="%(remove_topic_label)s" /> |
paul@92 | 427 | </td> |
paul@92 | 428 | </tr>''' % d |
paul@92 | 429 | |
paul@92 | 430 | html += ''' |
paul@92 | 431 | <tr> |
paul@92 | 432 | <td></td> |
paul@99 | 433 | <td colspan="2"> |
paul@92 | 434 | <input name="add-topic" type="submit" value="%(add_topic_label)s" /> |
paul@92 | 435 | </td> |
paul@92 | 436 | </tr>''' % d |
paul@92 | 437 | |
paul@92 | 438 | # Advanced options. |
paul@92 | 439 | |
paul@73 | 440 | if show_advanced: |
paul@73 | 441 | html += ''' |
paul@73 | 442 | <tr> |
paul@73 | 443 | <td></td> |
paul@99 | 444 | <td colspan="2"> |
paul@73 | 445 | <input name="basic" type="submit" value="%(basic_label)s" /> |
paul@73 | 446 | <input name="advanced" type="hidden" value="true" /> |
paul@73 | 447 | </td> |
paul@73 | 448 | </tr> |
paul@73 | 449 | <tr> |
paul@73 | 450 | <td class="label"><label>%(category_label)s</label></td> |
paul@99 | 451 | <td colspan="2" class="content"> |
paul@73 | 452 | <select multiple="multiple" name="category"> |
paul@73 | 453 | %(category_list)s |
paul@73 | 454 | </select> |
paul@73 | 455 | </td> |
paul@49 | 456 | </tr> |
paul@49 | 457 | <tr> |
paul@49 | 458 | <td class="label"><label>%(template_label)s</label></td> |
paul@99 | 459 | <td colspan="2"> |
paul@49 | 460 | <input name="template" type="text" size="40" value="%(template_default)s" /> |
paul@49 | 461 | </td> |
paul@49 | 462 | </tr> |
paul@49 | 463 | <tr> |
paul@55 | 464 | <td class="label"><label>%(parent_label)s</label></td> |
paul@99 | 465 | <td colspan="2"> |
paul@55 | 466 | <input name="parent" type="text" size="40" value="%(parent_default)s" /> |
paul@55 | 467 | </td> |
paul@55 | 468 | </tr> |
paul@55 | 469 | <tr> |
paul@46 | 470 | <td></td> |
paul@99 | 471 | <td colspan="2" class="buttons"> |
paul@46 | 472 | %(buttons_html)s |
paul@46 | 473 | </td> |
paul@46 | 474 | </tr> |
paul@73 | 475 | </table>''' % d |
paul@73 | 476 | else: |
paul@73 | 477 | html += ''' |
paul@73 | 478 | <tr> |
paul@73 | 479 | <td></td> |
paul@99 | 480 | <td colspan="2"> |
paul@73 | 481 | <input name="advanced" type="submit" value="%(advanced_label)s" /> |
paul@73 | 482 | %(category_list)s |
paul@73 | 483 | <input name="parent" type="hidden" value="%(parent_default)s" /> |
paul@73 | 484 | <input name="template" type="hidden" value="%(template_default)s" /> |
paul@73 | 485 | </td> |
paul@73 | 486 | </tr> |
paul@73 | 487 | <tr> |
paul@73 | 488 | <td></td> |
paul@99 | 489 | <td colspan="2" class="buttons"> |
paul@73 | 490 | %(buttons_html)s |
paul@73 | 491 | </td> |
paul@73 | 492 | </tr> |
paul@238 | 493 | </table> |
paul@238 | 494 | <script type="text/javascript"> |
paul@238 | 495 | function replaceDialog(url, button) { |
paul@238 | 496 | var form = findForm(); |
paul@238 | 497 | var dialog = findDialog(document); |
paul@238 | 498 | if (form != null && dialog != null) { |
paul@238 | 499 | var xmlhttp = new XMLHttpRequest(); |
paul@238 | 500 | xmlhttp.open("POST", url, false); |
paul@238 | 501 | xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); |
paul@238 | 502 | |
paul@238 | 503 | var requestBody = encodeURIComponent(button.name) + "=" + encodeURIComponent(button.value); |
paul@238 | 504 | for (var i = 0; i < form.elements.length; i++) { |
paul@238 | 505 | var element = form.elements[i]; |
paul@238 | 506 | if (element.type != "submit") { |
paul@238 | 507 | requestBody += "&" + encodeURIComponent(element.name) + "=" + encodeURIComponent(element.value); |
paul@238 | 508 | } |
paul@238 | 509 | } |
paul@238 | 510 | xmlhttp.send(requestBody); |
paul@238 | 511 | |
paul@238 | 512 | var newDialog = xmlhttp.responseText; |
paul@238 | 513 | |
paul@238 | 514 | if (newDialog != null) { |
paul@238 | 515 | dialog.parentNode.innerHTML = newDialog; |
paul@238 | 516 | initForm(); |
paul@238 | 517 | return false; |
paul@238 | 518 | } |
paul@238 | 519 | } |
paul@238 | 520 | } |
paul@238 | 521 | |
paul@238 | 522 | function findDialog(d) { |
paul@238 | 523 | var elements = d.getElementsByTagName("div"); |
paul@238 | 524 | for (var i = 0; i < elements.length; i++) { |
paul@238 | 525 | var element = elements[i]; |
paul@238 | 526 | var cls = element.getAttribute("class"); |
paul@238 | 527 | if (cls == "dialog") { |
paul@238 | 528 | return element; |
paul@238 | 529 | } |
paul@238 | 530 | } |
paul@238 | 531 | return null; |
paul@238 | 532 | } |
paul@238 | 533 | |
paul@238 | 534 | function findForm() { |
paul@238 | 535 | for (var i = 0; i < document.forms.length; i++) { |
paul@238 | 536 | var form = document.forms[i]; |
paul@238 | 537 | if (form["update-form-only"] != null) { |
paul@238 | 538 | return form; |
paul@238 | 539 | } |
paul@238 | 540 | } |
paul@238 | 541 | return null; |
paul@238 | 542 | } |
paul@238 | 543 | |
paul@238 | 544 | function initForm() { |
paul@238 | 545 | var form = findForm(); |
paul@238 | 546 | var url = form.getAttribute("action"); |
paul@238 | 547 | form["update-form-only"].value = "true"; |
paul@238 | 548 | for (var i = 0; i < form.length; i++) { |
paul@238 | 549 | var element = form[i]; |
paul@239 | 550 | if (element.type == "submit" && element.name != "%(form_trigger)s" && element.name != "%(form_cancel)s") { |
paul@238 | 551 | element.setAttribute("onclick", "return replaceDialog('" + url + "', this);"); |
paul@238 | 552 | } |
paul@238 | 553 | } |
paul@238 | 554 | } |
paul@238 | 555 | |
paul@238 | 556 | initForm(); |
paul@238 | 557 | </script>''' % d |
paul@73 | 558 | |
paul@73 | 559 | return html |
paul@46 | 560 | |
paul@46 | 561 | def do_action(self): |
paul@46 | 562 | |
paul@46 | 563 | "Create the new event." |
paul@46 | 564 | |
paul@46 | 565 | _ = self._ |
paul@109 | 566 | form = self.get_form() |
paul@46 | 567 | |
paul@46 | 568 | # If no title exists in the request, an error message is returned. |
paul@46 | 569 | |
paul@46 | 570 | title = form.get("title", [None])[0] |
paul@49 | 571 | template = form.get("template", [None])[0] |
paul@46 | 572 | |
paul@46 | 573 | if not title: |
paul@46 | 574 | return 0, _("No event title specified.") |
paul@46 | 575 | |
paul@49 | 576 | if not template: |
paul@49 | 577 | return 0, _("No page template specified.") |
paul@49 | 578 | |
paul@46 | 579 | return self.create_event(self.request) |
paul@46 | 580 | |
paul@238 | 581 | def render_msg(self, msg, msgtype): |
paul@238 | 582 | |
paul@238 | 583 | """ |
paul@238 | 584 | Render 'msg' and 'msgtype'. If 'msgtype' is "dialog" then the form is |
paul@238 | 585 | rendered, and if only part of the form is being requested, the output |
paul@238 | 586 | will be only the form HTML fragment and not the entire page. |
paul@238 | 587 | """ |
paul@238 | 588 | |
paul@238 | 589 | # Either render the form as a fragment of a page. |
paul@238 | 590 | |
paul@243 | 591 | form = self.get_form() |
paul@243 | 592 | update_form_only = form.get("update-form-only", ["false"])[0] == "true" |
paul@243 | 593 | action_attempted = form.has_key(self.form_trigger) |
paul@243 | 594 | |
paul@243 | 595 | if msgtype == "dialog" and update_form_only and not action_attempted: |
paul@238 | 596 | send_headers = get_send_headers(self.request) |
paul@238 | 597 | send_headers(["Content-Type: text/html; charset=%s" % config.charset]) |
paul@238 | 598 | self.request.write(msg.render()) |
paul@238 | 599 | |
paul@238 | 600 | # Or render the message/form within an entire page. |
paul@238 | 601 | |
paul@238 | 602 | else: |
paul@238 | 603 | ActionBase.render_msg(self, msg, msgtype) |
paul@238 | 604 | |
paul@238 | 605 | def render_success(self, msg, msgtype): |
paul@46 | 606 | |
paul@46 | 607 | """ |
paul@46 | 608 | Render neither 'msg' nor 'msgtype' since redirection should occur |
paul@46 | 609 | instead. |
paul@46 | 610 | """ |
paul@46 | 611 | |
paul@46 | 612 | pass |
paul@46 | 613 | |
paul@46 | 614 | def create_event(self, request): |
paul@46 | 615 | |
paul@46 | 616 | "Create an event page using the 'request'." |
paul@46 | 617 | |
paul@46 | 618 | _ = request.getText |
paul@109 | 619 | form = self.get_form() |
paul@46 | 620 | |
paul@48 | 621 | category_pagenames = form.get("category", []) |
paul@49 | 622 | description = form.get("description", [None])[0] |
paul@92 | 623 | location = form.get("location", [None])[0] |
paul@230 | 624 | latitude = form.get("latitude", [None])[0] |
paul@230 | 625 | longitude = form.get("longitude", [None])[0] |
paul@92 | 626 | link = form.get("link", [None])[0] |
paul@92 | 627 | topics = form.get("topics", []) |
paul@92 | 628 | |
paul@99 | 629 | regime = form.get("regime", [None])[0] |
paul@99 | 630 | start_offset = form.get("start-offset", [None])[0] |
paul@99 | 631 | end_offset = form.get("end-offset", [None])[0] |
paul@99 | 632 | |
paul@99 | 633 | start_zone = regime or start_offset |
paul@99 | 634 | end_zone = regime or end_offset |
paul@96 | 635 | |
paul@92 | 636 | # Validate certain fields. |
paul@46 | 637 | |
paul@243 | 638 | title = form.get("title", [""])[0].strip() |
paul@243 | 639 | template = form.get("template", [""])[0].strip() |
paul@243 | 640 | parent = form.get("parent", [""])[0].strip() |
paul@243 | 641 | |
paul@243 | 642 | if not title: |
paul@243 | 643 | return 0, _("No event title specified.") |
paul@243 | 644 | if not template: |
paul@243 | 645 | return 0, _("No event template specified.") |
paul@49 | 646 | |
paul@96 | 647 | try: |
paul@97 | 648 | start_day = self._get_input(form, "start-day") |
paul@97 | 649 | start_month = self._get_input(form, "start-month") |
paul@97 | 650 | start_year = self._get_input(form, "start-year") |
paul@97 | 651 | |
paul@97 | 652 | if not start_day or not start_month or not start_year: |
paul@97 | 653 | return 0, _("A start date must be specified.") |
paul@97 | 654 | |
paul@48 | 655 | end_day = self._get_input(form, "end-day", start_day) |
paul@48 | 656 | end_month = self._get_input(form, "end-month", start_month) |
paul@48 | 657 | end_year = self._get_input(form, "end-year", start_year) |
paul@46 | 658 | |
paul@48 | 659 | except (TypeError, ValueError): |
paul@46 | 660 | return 0, _("Days and years must be numbers yielding a valid date!") |
paul@46 | 661 | |
paul@96 | 662 | try: |
paul@96 | 663 | start_hour = self._get_input(form, "start-hour") |
paul@96 | 664 | start_minute = self._get_input(form, "start-minute") |
paul@96 | 665 | start_second = self._get_input(form, "start-second") |
paul@96 | 666 | |
paul@96 | 667 | end_hour = self._get_input(form, "end-hour") |
paul@96 | 668 | end_minute = self._get_input(form, "end-minute") |
paul@96 | 669 | end_second = self._get_input(form, "end-second") |
paul@49 | 670 | |
paul@96 | 671 | except (TypeError, ValueError): |
paul@96 | 672 | return 0, _("Hours, minutes and seconds must be numbers yielding a valid time!") |
paul@96 | 673 | |
paul@168 | 674 | start_date = DateTime( |
paul@96 | 675 | (start_year, start_month, start_day, start_hour, start_minute, start_second, start_zone) |
paul@96 | 676 | ) |
paul@96 | 677 | start_date.constrain() |
paul@96 | 678 | |
paul@168 | 679 | end_date = DateTime( |
paul@96 | 680 | (end_year, end_month, end_day, end_hour, end_minute, end_second, end_zone) |
paul@96 | 681 | ) |
paul@96 | 682 | end_date.constrain() |
paul@46 | 683 | |
paul@78 | 684 | # An elementary date ordering check. |
paul@78 | 685 | |
paul@96 | 686 | if (start_date.as_date() != end_date.as_date() or start_date.has_time() and end_date.has_time()) and start_date > end_date: |
paul@78 | 687 | start_date, end_date = end_date, start_date |
paul@78 | 688 | |
paul@88 | 689 | event_details = { |
paul@96 | 690 | "start" : str(start_date), "end" : str(end_date), |
paul@88 | 691 | "title" : title, "summary" : title, |
paul@92 | 692 | "description" : description, "location" : location, "link" : link, |
paul@92 | 693 | "topics" : [topic for topic in topics if topic] |
paul@88 | 694 | } |
paul@88 | 695 | |
paul@230 | 696 | if latitude and longitude: |
paul@230 | 697 | event_details["geo"] = latitude, longitude |
paul@230 | 698 | |
paul@46 | 699 | # Copy the template. |
paul@49 | 700 | |
paul@88 | 701 | template_page = PageEditor(request, template) |
paul@46 | 702 | |
paul@88 | 703 | if not template_page.exists(): |
paul@49 | 704 | return 0, _("Event template not available.") |
paul@49 | 705 | |
paul@55 | 706 | # Use any parent page information. |
paul@55 | 707 | |
paul@168 | 708 | full_title = getFullPageName(parent, title) |
paul@55 | 709 | |
paul@47 | 710 | # Load the new page and replace the event details in the body. |
paul@46 | 711 | |
paul@64 | 712 | new_page = PageEditor(request, full_title) |
paul@46 | 713 | |
paul@46 | 714 | if new_page.exists(): |
paul@46 | 715 | return 0, _("The specified page already exists. Please choose another name.") |
paul@46 | 716 | |
paul@255 | 717 | # Complete the new page and return its body. |
paul@67 | 718 | |
paul@255 | 719 | body = fillEventPageFromTemplate(template_page, new_page, event_details, category_pagenames) |
paul@46 | 720 | |
paul@255 | 721 | # Open the page editor on the new page. |
paul@255 | 722 | # NOTE: Replacing the revision in the request to prevent Moin from |
paul@255 | 723 | # NOTE: attempting to use the queued changes page's revision. |
paul@255 | 724 | # NOTE: Replacing the action and page in the request to avoid issues |
paul@255 | 725 | # NOTE: with editing tickets. |
paul@46 | 726 | |
paul@255 | 727 | request.rev = 0 |
paul@255 | 728 | request.action = "edit" |
paul@255 | 729 | request.page = new_page |
paul@255 | 730 | new_page.sendEditor(preview=body, staytop=True) |
paul@255 | 731 | |
paul@255 | 732 | # Return success. |
paul@255 | 733 | |
paul@46 | 734 | return 1, None |
paul@46 | 735 | |
paul@46 | 736 | # Action function. |
paul@46 | 737 | |
paul@46 | 738 | def execute(pagename, request): |
paul@46 | 739 | EventAggregatorNewEvent(pagename, request).render() |
paul@46 | 740 | |
paul@46 | 741 | # vim: tabstop=4 expandtab shiftwidth=4 |