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 44 category_pagenames = form.get("category", []) 45 46 for category_name, category_pagename in \ 47 EventAggregatorSupport.getCategoryMapping( 48 EventAggregatorSupport.getCategories(request), 49 request): 50 51 selected = self._get_selected_for_list(category_pagename, category_pagenames) 52 category_list.append('<option value="%s" %s>%s</option>' % (category_pagename, selected, category_name)) 53 54 # Initialise month lists. 55 56 start_month_list = [] 57 start_month_list.append('<option value=""></option>') 58 end_month_list = [] 59 end_month_list.append('<option value=""></option>') 60 61 start_month = self._get_input(form, "start-month", 0) 62 end_month = self._get_input(form, "end-month", 0) 63 64 # Prepare month lists, selecting specified months. 65 66 for month in range(1, 13): 67 month_label = _(EventAggregatorSupport.getMonthLabel(month)) 68 selected = self._get_selected(month, start_month) 69 start_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) 70 selected = self._get_selected(month, end_month) 71 end_month_list.append('<option value="%02d" %s>%s</option>' % (month, selected, month_label)) 72 73 d = { 74 "buttons_html" : buttons_html, 75 "category_label" : _("Categories"), 76 "category_list" : "\n".join(category_list), 77 "start_month_list" : "\n".join(start_month_list), 78 "end_month_list" : "\n".join(end_month_list), 79 "start_label" : _("Start date (day, month, year)"), 80 "start_day_default" : form.get("start-day", [""])[0], 81 "start_year_default" : form.get("start-year", [""])[0] or EventAggregatorSupport.getCurrentYear(), 82 "end_label" : _("End date (day, month, year)"), 83 "end_day_default" : form.get("end-day", [""])[0], 84 "end_year_default" : form.get("end-year", [""])[0], 85 "title_label" : _("Event title/summary"), 86 "title_default" : form.get("title", [""])[0], 87 } 88 89 return ''' 90 <table> 91 <tr> 92 <td class="label"><label>%(title_label)s</label></td> 93 <td> 94 <input name="title" type="text" size="40" value="%(title_default)s" /> 95 </td> 96 </tr> 97 <tr> 98 <td class="label"><label>%(category_label)s</label></td> 99 <td class="content"> 100 <select multiple="multiple" name="category"> 101 %(category_list)s 102 </select> 103 </td> 104 </tr> 105 <tr> 106 <td class="label"><label>%(start_label)s</label></td> 107 <td> 108 <input name="start-day" type="text" value="%(start_day_default)s" size="2" /> 109 <select name="start-month"> 110 %(start_month_list)s 111 </select> 112 <input name="start-year" type="text" value="%(start_year_default)s" size="4" /> 113 </td> 114 </tr> 115 <tr> 116 <td class="label"><label>%(end_label)s</label></td> 117 <td> 118 <input name="end-day" type="text" value="%(end_day_default)s" size="2" /> 119 <select name="end-month"> 120 %(end_month_list)s 121 </select> 122 <input name="end-year" type="text" value="%(end_year_default)s" size="4" /> 123 </td> 124 </tr> 125 <tr> 126 <td></td> 127 <td class="buttons"> 128 %(buttons_html)s 129 </td> 130 </tr> 131 </table> 132 ''' % d 133 134 def do_action(self): 135 136 "Create the new event." 137 138 _ = self._ 139 form = self.request.form 140 141 # If no title exists in the request, an error message is returned. 142 143 title = form.get("title", [None])[0] 144 145 if not title: 146 return 0, _("No event title specified.") 147 148 return self.create_event(self.request) 149 150 def render_success(self, msg, msgtype=None): 151 152 """ 153 Render neither 'msg' nor 'msgtype' since redirection should occur 154 instead. 155 NOTE: msgtype is optional because MoinMoin 1.5.x does not support it. 156 """ 157 158 pass 159 160 def create_event(self, request): 161 162 "Create an event page using the 'request'." 163 164 _ = request.getText 165 form = request.form 166 167 category_pagenames = form.get("category", []) 168 title = form.get("title", [None])[0] 169 170 try: 171 start_day = self._get_input(form, "start-day", 0) 172 start_month = self._get_input(form, "start-month", 0) 173 start_year = self._get_input(form, "start-year", 0) 174 end_day = self._get_input(form, "end-day", start_day) 175 end_month = self._get_input(form, "end-month", start_month) 176 end_year = self._get_input(form, "end-year", start_year) 177 178 start_year, start_month, start_day = self.constrain_date(start_year, start_month, start_day) 179 end_year, end_month, end_day = self.constrain_date(end_year, end_month, end_day) 180 181 except (TypeError, ValueError): 182 return 0, _("Days and years must be numbers yielding a valid date!") 183 184 start_date = self.make_date_string(start_year, start_month, start_day) 185 end_date = self.make_date_string(end_year, end_month, end_day) 186 187 # Copy the template. 188 # Permitting configuration of the template name. 189 190 page = PageEditor(request, getattr(request.cfg, "event_aggregator_new_event_template", "EventTemplate")) 191 body = page.get_raw_body() 192 193 # Load the new page and replace the event details in the body. 194 195 new_page = PageEditor(request, title) 196 197 if new_page.exists(): 198 return 0, _("The specified page already exists. Please choose another name.") 199 200 if EventAggregatorSupport.getFormat(page) == "wiki": 201 event_details = {"start" : start_date, "end" : end_date, "title" : title, "summary" : title} 202 body = EventAggregatorSupport.setEventDetails(body, event_details) 203 body = EventAggregatorSupport.setCategoryMembership(body, category_pagenames) 204 new_page.saveText(body, 0) 205 206 # Redirect and return success. 207 208 query = {'action' : 'edit'} 209 request.http_redirect(new_page.url(request, query)) 210 return 1, None 211 212 def make_date_string(self, year, month, day): 213 return "%s-%s-%s" % ( 214 year and ("%04d" % year) or "YYYY", 215 month and ("%02d" % month) or "MM", 216 day and ("%02d" % day) or "DD" 217 ) 218 219 def constrain_date(self, year, month, day): 220 if year and month and day: 221 wd, last_day = calendar.monthrange(year, month) 222 day = max(min(day, last_day), 1) 223 return year, month, day 224 225 # Action function. 226 227 def execute(pagename, request): 228 EventAggregatorNewEvent(pagename, request).render() 229 230 # vim: tabstop=4 expandtab shiftwidth=4