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