1.1 --- a/imipweb/data.py Sun Sep 10 23:45:01 2017 +0200
1.2 +++ b/imipweb/data.py Mon Sep 11 16:39:20 2017 +0200
1.3 @@ -367,11 +367,11 @@
1.4
1.5 return all_values
1.6
1.7 -def set_date_control_values(args, name, formdates, tzid_name=None):
1.8 +def set_date_control_values(formdates, args, name, tzid_name=None):
1.9
1.10 """
1.11 - Replace form fields in 'args' starting with 'name' using the values of the
1.12 - given 'formdates'.
1.13 + Using the values of the given 'formdates', replace form fields in 'args'
1.14 + starting with 'name'.
1.15
1.16 If 'tzid_name' is specified, the time zone information will be stored in
1.17 fields starting with 'tzid_name' instead of 'name'.
1.18 @@ -396,7 +396,7 @@
1.19 replaced_name=None, tzid=None):
1.20
1.21 """
1.22 - Return period values from fields found in 'args' containing the given
1.23 + Return period values from fields found in 'args' prefixed with the given
1.24 'start_name' (for start dates), 'end_name' (for end dates),
1.25 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name'
1.26 (to enable times for periods).
1.27 @@ -429,11 +429,15 @@
1.28 all_starts = get_date_control_values(args, start_name, True, tzid=tzid)
1.29 all_ends = get_date_control_values(args, end_name, True, start_name, tzid=tzid)
1.30
1.31 + # Construct period objects for each start, end, origin combination.
1.32 +
1.33 periods = []
1.34
1.35 for index, (start, end, found_origin) in \
1.36 enumerate(map(None, all_starts, all_ends, all_origins)):
1.37
1.38 + # Obtain period settings from separate controls.
1.39 +
1.40 end_enabled = str(index) in all_end_enabled
1.41 times_enabled = str(index) in all_times_enabled
1.42 replaced = str(index) in all_replaced
1.43 @@ -449,4 +453,64 @@
1.44 else:
1.45 return periods
1.46
1.47 +def set_period_control_values(periods, args, start_name, end_name,
1.48 + end_enabled_name, times_enabled_name,
1.49 + origin_name=None, replaced_name=None):
1.50 +
1.51 + """
1.52 + Using the given 'periods', replace form fields in 'args' prefixed with the
1.53 + given 'start_name' (for start dates), 'end_name' (for end dates),
1.54 + 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name'
1.55 + (to enable times for periods).
1.56 +
1.57 + If 'origin_name' is specified, fields containing the name will provide
1.58 + origin information, and fields containing 'replaced_name' will indicate
1.59 + periods that are replaced.
1.60 + """
1.61 +
1.62 + # Record period settings separately.
1.63 +
1.64 + args[end_enabled_name] = []
1.65 + args[times_enabled_name] = []
1.66 +
1.67 + # Record origin and replacement information if naming is defined.
1.68 +
1.69 + if origin_name:
1.70 + args[origin_name] = []
1.71 +
1.72 + if replaced_name:
1.73 + args[replaced_name] = []
1.74 +
1.75 + all_starts = []
1.76 + all_ends = []
1.77 +
1.78 + for index, period in enumerate(periods):
1.79 +
1.80 + # Encode period settings in controls.
1.81 +
1.82 + if period.end_enabled:
1.83 + args[end_enabled_name].append(str(index))
1.84 + if period.times_enabled:
1.85 + args[times_enabled_name].append(str(index))
1.86 +
1.87 + # Add origin information where controls are present to record it.
1.88 +
1.89 + if origin_name:
1.90 + args[origin_name].append(period.origin or "")
1.91 +
1.92 + # Add replacement information where controls are present to record it.
1.93 +
1.94 + if replaced_name and period.replaced:
1.95 + args[replaced_name].append(str(index))
1.96 +
1.97 + # Collect form date information for addition below.
1.98 +
1.99 + all_starts.append(period.get_form_start())
1.100 + all_ends.append(period.get_form_end())
1.101 +
1.102 + # Set the controls for the dates.
1.103 +
1.104 + set_date_control_values(all_starts, args, start_name)
1.105 + set_date_control_values(all_ends, args, end_name, tzid_name=start_name)
1.106 +
1.107 # vim: tabstop=4 expandtab shiftwidth=4