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