1.1 --- a/imipweb/data.py Wed Jun 14 00:30:32 2017 +0200
1.2 +++ b/imipweb/data.py Sun Sep 10 23:45:01 2017 +0200
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Web interface data abstractions.
1.6
1.7 -Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2014, 2015, 2017 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU General Public License as published by the Free Software
1.12 @@ -323,4 +323,130 @@
1.13 else:
1.14 return event_period_from_period(period).as_form_period()
1.15
1.16 +# Form field extraction and serialisation.
1.17 +
1.18 +def get_date_control_values(args, name, multiple=False, tzid_name=None, tzid=None):
1.19 +
1.20 + """
1.21 + Return a form date object representing fields taken from 'args' starting
1.22 + with 'name'.
1.23 +
1.24 + If 'multiple' is set to a true value, many date objects will be returned
1.25 + corresponding to a collection of datetimes.
1.26 +
1.27 + If 'tzid_name' is specified, the time zone information will be acquired
1.28 + from fields starting with 'tzid_name' instead of 'name'.
1.29 +
1.30 + If 'tzid' is specified, it will provide the time zone where no explicit
1.31 + time zone information is indicated in the field data.
1.32 + """
1.33 +
1.34 + dates = args.get("%s-date" % name, [])
1.35 + hours = args.get("%s-hour" % name, [])
1.36 + minutes = args.get("%s-minute" % name, [])
1.37 + seconds = args.get("%s-second" % name, [])
1.38 + tzids = args.get("%s-tzid" % (tzid_name or name), [])
1.39 +
1.40 + # Handle absent values by employing None values.
1.41 +
1.42 + field_values = map(None, dates, hours, minutes, seconds, tzids)
1.43 +
1.44 + if not field_values and not multiple:
1.45 + all_values = FormDate()
1.46 + else:
1.47 + all_values = []
1.48 + for date, hour, minute, second, tzid_field in field_values:
1.49 + value = FormDate(date, hour, minute, second, tzid_field or tzid)
1.50 +
1.51 + # Return a single value or append to a collection of all values.
1.52 +
1.53 + if not multiple:
1.54 + return value
1.55 + else:
1.56 + all_values.append(value)
1.57 +
1.58 + return all_values
1.59 +
1.60 +def set_date_control_values(args, name, formdates, tzid_name=None):
1.61 +
1.62 + """
1.63 + Replace form fields in 'args' starting with 'name' using the values of the
1.64 + given 'formdates'.
1.65 +
1.66 + If 'tzid_name' is specified, the time zone information will be stored in
1.67 + fields starting with 'tzid_name' instead of 'name'.
1.68 + """
1.69 +
1.70 + args["%s-date" % name] = []
1.71 + args["%s-hour" % name] = []
1.72 + args["%s-minute" % name] = []
1.73 + args["%s-second" % name] = []
1.74 + args["%s-tzid" % (tzid_name or name)] = []
1.75 +
1.76 + for d in formdates:
1.77 + args["%s-date" % name].append(d.date)
1.78 + args["%s-hour" % name].append(d.hour)
1.79 + args["%s-minute" % name].append(d.minute)
1.80 + args["%s-second" % name].append(d.second)
1.81 + args["%s-tzid" % (tzid_name or name)].append(d.tzid)
1.82 +
1.83 +def get_period_control_values(args, start_name, end_name,
1.84 + end_enabled_name, times_enabled_name,
1.85 + origin=None, origin_name=None,
1.86 + replaced_name=None, tzid=None):
1.87 +
1.88 + """
1.89 + Return period values from fields found in 'args' containing the given
1.90 + 'start_name' (for start dates), 'end_name' (for end dates),
1.91 + 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name'
1.92 + (to enable times for periods).
1.93 +
1.94 + If 'origin' is specified, a single period with the given origin is
1.95 + returned. If 'origin_name' is specified, fields containing the name will
1.96 + provide origin information, and fields containing 'replaced_name' will
1.97 + indicate periods that are replaced.
1.98 +
1.99 + If 'tzid' is specified, it will provide the time zone where no explicit
1.100 + time zone information is indicated in the field data.
1.101 + """
1.102 +
1.103 + # Get the end datetime and time presence settings.
1.104 +
1.105 + all_end_enabled = args.get(end_enabled_name, [])
1.106 + all_times_enabled = args.get(times_enabled_name, [])
1.107 +
1.108 + # Get the origins of period data and whether the periods are replaced.
1.109 +
1.110 + if origin:
1.111 + all_origins = [origin]
1.112 + else:
1.113 + all_origins = origin_name and args.get(origin_name, []) or []
1.114 +
1.115 + all_replaced = replaced_name and args.get(replaced_name, []) or []
1.116 +
1.117 + # Get the start and end datetimes.
1.118 +
1.119 + all_starts = get_date_control_values(args, start_name, True, tzid=tzid)
1.120 + all_ends = get_date_control_values(args, end_name, True, start_name, tzid=tzid)
1.121 +
1.122 + periods = []
1.123 +
1.124 + for index, (start, end, found_origin) in \
1.125 + enumerate(map(None, all_starts, all_ends, all_origins)):
1.126 +
1.127 + end_enabled = str(index) in all_end_enabled
1.128 + times_enabled = str(index) in all_times_enabled
1.129 + replaced = str(index) in all_replaced
1.130 +
1.131 + period = FormPeriod(start, end, end_enabled, times_enabled, tzid,
1.132 + found_origin or origin, replaced)
1.133 + periods.append(period)
1.134 +
1.135 + # Return a single period if a single origin was specified.
1.136 +
1.137 + if origin:
1.138 + return periods[0]
1.139 + else:
1.140 + return periods
1.141 +
1.142 # vim: tabstop=4 expandtab shiftwidth=4