# HG changeset patch # User Paul Boddie # Date 1505140760 -7200 # Node ID c0ee74128fca71ac67efc0b26a849fc38b6b8468 # Parent 792f00676d622d4e935532510e685d39580e4ab6 Moved form period serialisation into the data module, removing redundant methods from the resource module. diff -r 792f00676d62 -r c0ee74128fca imipweb/data.py --- a/imipweb/data.py Sun Sep 10 23:45:01 2017 +0200 +++ b/imipweb/data.py Mon Sep 11 16:39:20 2017 +0200 @@ -367,11 +367,11 @@ return all_values -def set_date_control_values(args, name, formdates, tzid_name=None): +def set_date_control_values(formdates, args, name, tzid_name=None): """ - Replace form fields in 'args' starting with 'name' using the values of the - given 'formdates'. + Using the values of the given 'formdates', replace form fields in 'args' + starting with 'name'. If 'tzid_name' is specified, the time zone information will be stored in fields starting with 'tzid_name' instead of 'name'. @@ -396,7 +396,7 @@ replaced_name=None, tzid=None): """ - Return period values from fields found in 'args' containing the given + Return period values from fields found in 'args' prefixed with the given 'start_name' (for start dates), 'end_name' (for end dates), 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' (to enable times for periods). @@ -429,11 +429,15 @@ all_starts = get_date_control_values(args, start_name, True, tzid=tzid) all_ends = get_date_control_values(args, end_name, True, start_name, tzid=tzid) + # Construct period objects for each start, end, origin combination. + periods = [] for index, (start, end, found_origin) in \ enumerate(map(None, all_starts, all_ends, all_origins)): + # Obtain period settings from separate controls. + end_enabled = str(index) in all_end_enabled times_enabled = str(index) in all_times_enabled replaced = str(index) in all_replaced @@ -449,4 +453,64 @@ else: return periods +def set_period_control_values(periods, args, start_name, end_name, + end_enabled_name, times_enabled_name, + origin_name=None, replaced_name=None): + + """ + Using the given 'periods', replace form fields in 'args' prefixed with the + given 'start_name' (for start dates), 'end_name' (for end dates), + 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' + (to enable times for periods). + + If 'origin_name' is specified, fields containing the name will provide + origin information, and fields containing 'replaced_name' will indicate + periods that are replaced. + """ + + # Record period settings separately. + + args[end_enabled_name] = [] + args[times_enabled_name] = [] + + # Record origin and replacement information if naming is defined. + + if origin_name: + args[origin_name] = [] + + if replaced_name: + args[replaced_name] = [] + + all_starts = [] + all_ends = [] + + for index, period in enumerate(periods): + + # Encode period settings in controls. + + if period.end_enabled: + args[end_enabled_name].append(str(index)) + if period.times_enabled: + args[times_enabled_name].append(str(index)) + + # Add origin information where controls are present to record it. + + if origin_name: + args[origin_name].append(period.origin or "") + + # Add replacement information where controls are present to record it. + + if replaced_name and period.replaced: + args[replaced_name].append(str(index)) + + # Collect form date information for addition below. + + all_starts.append(period.get_form_start()) + all_ends.append(period.get_form_end()) + + # Set the controls for the dates. + + set_date_control_values(all_starts, args, start_name) + set_date_control_values(all_ends, args, end_name, tzid_name=start_name) + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 792f00676d62 -r c0ee74128fca imipweb/event.py --- a/imipweb/event.py Sun Sep 10 23:45:01 2017 +0200 +++ b/imipweb/event.py Mon Sep 11 16:39:20 2017 +0200 @@ -24,7 +24,8 @@ from imiptools.dates import format_datetime, to_timezone from imiptools.mail import Messenger from imipweb.data import EventPeriod, event_period_from_period, \ - get_period_control_values, PeriodError + get_period_control_values, set_period_control_values, \ + PeriodError from imipweb.resource import DateTimeFormUtilities, FormUtilities, ResourceClientForObject # Fake gettext method for strings to be translated later. @@ -1027,30 +1028,10 @@ "Set the recurrences defined in the event form." - args = self.env.get_args() - - args["dtend-control-recur"] = [] - args["dttimes-control-recur"] = [] - args["recur-origin"] = [] - args["recur-replaced"] = [] - - all_starts = [] - all_ends = [] - - for index, period in enumerate(recurrences): - if period.end_enabled: - args["dtend-control-recur"].append(str(index)) - if period.times_enabled: - args["dttimes-control-recur"].append(str(index)) - if period.replaced: - args["recur-replaced"].append(str(index)) - args["recur-origin"].append(period.origin or "") - - all_starts.append(period.get_form_start()) - all_ends.append(period.get_form_end()) - - self.set_date_control_values("dtstart-recur", all_starts) - self.set_date_control_values("dtend-recur", all_ends, tzid_name="dtstart-recur") + set_period_control_values(recurrences, self.env.get_args(), + "dtstart-recur", "dtend-recur", + "dtend-control-recur", "dttimes-control-recur", + "recur-origin", "recur-replaced") def get_removed_periods(self, periods): diff -r 792f00676d62 -r c0ee74128fca imipweb/resource.py --- a/imipweb/resource.py Sun Sep 10 23:45:01 2017 +0200 +++ b/imipweb/resource.py Mon Sep 11 16:39:20 2017 +0200 @@ -25,7 +25,6 @@ from imiptools.dates import format_datetime, to_date from imiptools.freebusy import FreeBusyCollection from imipweb.data import event_period_from_period, form_period_from_period, \ - get_date_control_values, set_date_control_values, \ PeriodError from imipweb.env import CGIEnvironment from urllib import urlencode @@ -621,29 +620,4 @@ else: page.td("(Unrecognised date)") - def get_date_control_values(self, name, multiple=False, tzid_name=None): - - """ - Return a form date object representing fields starting with 'name'. If - 'multiple' is set to a true value, many date objects will be returned - corresponding to a collection of datetimes. - - If 'tzid_name' is specified, the time zone information will be acquired - from fields starting with 'tzid_name' instead of 'name'. - """ - - get_date_control_values(self.env.get_args(), name, multiple, tzid_name, self.get_tzid()) - - def set_date_control_values(self, name, formdates, tzid_name=None): - - """ - Replace form fields starting with 'name' using the values of the given - 'formdates'. - - If 'tzid_name' is specified, the time zone information will be stored in - fields starting with 'tzid_name' instead of 'name'. - """ - - set_date_control_values(self.env.get_args(), name, formdates, tzid_name) - # vim: tabstop=4 expandtab shiftwidth=4