1.1 --- a/actions/EventAggregatorNewEvent.py Sat Jun 06 02:17:51 2009 +0200
1.2 +++ b/actions/EventAggregatorNewEvent.py Sat Jun 06 23:37:14 2009 +0200
1.3 @@ -25,17 +25,14 @@
1.4
1.5 "An event creation dialogue requesting various parameters."
1.6
1.7 - # NOTE: These patterns, used to replace text in the template, must
1.8 - # NOTE: correspond with the template and the recognised labels found in
1.9 - # NOTE: EventAggregatorSummary.getEventDetails.
1.10 + def _get_selected(self, month, input_month):
1.11 + return input_month is not None and month == input_month and 'selected="selected"' or ''
1.12
1.13 - start_pattern = re.compile("(?<= start:: ).*?(?P<date>yyyy-mm-dd)")
1.14 - end_pattern = re.compile("(?<= end:: ).*?(?P<date>yyyy-mm-dd)")
1.15 - topics_pattern = re.compile(r"(?<= categories:: )\s*(.*?)$|(?<= topics:: )\s*(.*?)$", re.MULTILINE | re.UNICODE)
1.16 - category_pattern = re.compile(r"^\s*((Category\S+)(\s+Category\S+)*)\s*$", re.MULTILINE | re.UNICODE)
1.17 -
1.18 - def _get_selected(self, month, input_month):
1.19 - return (("%02d" % month) == input_month) and 'selected="selected"' or ''
1.20 + def _get_input(self, form, name, default, error):
1.21 + try:
1.22 + return int(form.get(name, [None])[0] or default)
1.23 + except ValueError:
1.24 + return error
1.25
1.26 def get_form_html(self, buttons_html):
1.27 _ = self._
1.28 @@ -58,8 +55,10 @@
1.29 end_month_list = []
1.30 end_month_list.append('<option value=""></option>')
1.31
1.32 - start_month = form.get("start-month", [None])[0]
1.33 - end_month = form.get("end-month", [None])[0]
1.34 + start_month = self._get_input(form, "start-month", 0, None)
1.35 + end_month = self._get_input(form, "end-month", 0, None)
1.36 + start_year = self._get_input(form, "start-year", EventAggregatorSupport.getCurrentYear(), None)
1.37 + end_year = self._get_input(form, "end-year", 0, None)
1.38
1.39 # Prepare month lists, selecting specified months.
1.40
1.41 @@ -78,7 +77,7 @@
1.42 "end_month_list" : "\n".join(end_month_list),
1.43 "start_label" : _("Start date (day, month, year)"),
1.44 "start_day_default" : form.get("start-day", [""])[0],
1.45 - "start_year_default" : form.get("start-year", [""])[0],
1.46 + "start_year_default" : start_year,
1.47 "end_label" : _("End date (day, month, year)"),
1.48 "end_day_default" : form.get("end-day", [""])[0],
1.49 "end_year_default" : form.get("end-year", [""])[0],
1.50 @@ -159,17 +158,18 @@
1.51 "Create an event page using the 'request'."
1.52
1.53 _ = request.getText
1.54 + form = request.form
1.55
1.56 - category_names = request.form.get("category", [])
1.57 - title = request.form.get("title", [None])[0]
1.58 + category_names = form.get("category", [])
1.59 + title = form.get("title", [None])[0]
1.60
1.61 try:
1.62 - start_day = int(request.form.get("start-day", [None])[0] or 0)
1.63 - start_month = int(request.form.get("start-month", [None])[0] or 0)
1.64 - start_year = int(request.form.get("start-year", [None])[0] or 0)
1.65 - end_day = int(request.form.get("end-day", [None])[0] or start_day)
1.66 - end_month = int(request.form.get("end-month", [None])[0] or start_month)
1.67 - end_year = int(request.form.get("end-year", [None])[0] or start_year)
1.68 + start_day = self._get_input(form, "start-day", 0, None)
1.69 + start_month = self._get_input(form, "start-month", 0, None)
1.70 + start_year = self._get_input(form, "start-year", 0, None)
1.71 + end_day = self._get_input(form, "end-day", start_day, None)
1.72 + end_month = self._get_input(form, "end-month", start_month, None)
1.73 + end_year = self._get_input(form, "end-year", start_year, None)
1.74
1.75 start_year, start_month, start_day = self.constrain_date(start_year, start_month, start_day)
1.76 end_year, end_month, end_day = self.constrain_date(end_year, end_month, end_day)
1.77 @@ -186,35 +186,25 @@
1.78 page = PageEditor(request, getattr(request.cfg, "event_aggregator_new_event_template", "EventTemplate"))
1.79 body = page.get_raw_body()
1.80
1.81 - # Load the new page and replace the placeholders.
1.82 + # Load the new page and replace the event details in the body.
1.83
1.84 new_page = PageEditor(request, title)
1.85
1.86 if new_page.exists():
1.87 return 0, _("The specified page already exists. Please choose another name.")
1.88
1.89 - # NOTE: The placeholders must match the template.
1.90 -
1.91 - body = self.replace_placeholder(body, self.start_pattern, start_date, 1)
1.92 - body = self.replace_placeholder(body, self.end_pattern, end_date, 1)
1.93 - body = self.replace_placeholder(body, self.category_pattern, " ".join(category_names))
1.94 -
1.95 - new_page.saveText(body, 0)
1.96 + if EventAggregatorSupport.getFormat(page) == "wiki":
1.97 + event_details = {"start" : start_date, "end" : end_date, "title" : title, "summary" : title}
1.98 + body = EventAggregatorSupport.setEventDetails(body, event_details)
1.99 + body = EventAggregatorSupport.setCategoryMembership(body, category_names)
1.100 + new_page.saveText(body, 0)
1.101
1.102 # Redirect and return success.
1.103
1.104 - query = {'action' : 'edit', 'backto' : self.pagename}
1.105 + query = {'action' : 'edit'}
1.106 request.http_redirect(new_page.url(request, query))
1.107 return 1, None
1.108
1.109 - def replace_placeholder(self, body, pattern, value, lower=0):
1.110 - match = pattern.search(lower and body.lower() or body)
1.111 - if match:
1.112 - start, end = match.span()
1.113 - return body[:start] + value + body[end:]
1.114 - else:
1.115 - return body
1.116 -
1.117 def make_date_string(self, year, month, day):
1.118 return "%s-%s-%s" % (
1.119 year and ("%04d" % year) or "YYYY",