paul@497 | 1 | #!/usr/bin/env python |
paul@497 | 2 | |
paul@497 | 3 | """ |
paul@1346 | 4 | Web user interface operations. |
paul@497 | 5 | |
paul@1244 | 6 | Copyright (C) 2014, 2015, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@497 | 7 | |
paul@497 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@497 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@497 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@497 | 11 | version. |
paul@497 | 12 | |
paul@497 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@497 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@497 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@497 | 16 | details. |
paul@497 | 17 | |
paul@497 | 18 | You should have received a copy of the GNU General Public License along with |
paul@497 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@497 | 20 | """ |
paul@497 | 21 | |
paul@1346 | 22 | from imiptools.editing import FormPeriod |
paul@1257 | 23 | |
paul@1244 | 24 | # Form field extraction and serialisation. |
paul@1244 | 25 | |
paul@1246 | 26 | def get_date_control_inputs(args, name, tzid_name=None): |
paul@1246 | 27 | |
paul@1246 | 28 | """ |
paul@1246 | 29 | Return a tuple of date control inputs taken from 'args' for field names |
paul@1246 | 30 | starting with 'name'. |
paul@1246 | 31 | |
paul@1246 | 32 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@1246 | 33 | from fields starting with 'tzid_name' instead of 'name'. |
paul@1246 | 34 | """ |
paul@1246 | 35 | |
paul@1246 | 36 | return args.get("%s-date" % name, []), \ |
paul@1246 | 37 | args.get("%s-hour" % name, []), \ |
paul@1246 | 38 | args.get("%s-minute" % name, []), \ |
paul@1246 | 39 | args.get("%s-second" % name, []), \ |
paul@1246 | 40 | args.get("%s-tzid" % (tzid_name or name), []) |
paul@1246 | 41 | |
paul@1244 | 42 | def get_date_control_values(args, name, multiple=False, tzid_name=None, tzid=None): |
paul@1244 | 43 | |
paul@1244 | 44 | """ |
paul@1244 | 45 | Return a form date object representing fields taken from 'args' starting |
paul@1244 | 46 | with 'name'. |
paul@1244 | 47 | |
paul@1244 | 48 | If 'multiple' is set to a true value, many date objects will be returned |
paul@1244 | 49 | corresponding to a collection of datetimes. |
paul@1244 | 50 | |
paul@1244 | 51 | If 'tzid_name' is specified, the time zone information will be acquired |
paul@1244 | 52 | from fields starting with 'tzid_name' instead of 'name'. |
paul@1244 | 53 | |
paul@1244 | 54 | If 'tzid' is specified, it will provide the time zone where no explicit |
paul@1244 | 55 | time zone information is indicated in the field data. |
paul@1244 | 56 | """ |
paul@1244 | 57 | |
paul@1246 | 58 | dates, hours, minutes, seconds, tzids = get_date_control_inputs(args, name, tzid_name) |
paul@1244 | 59 | |
paul@1244 | 60 | # Handle absent values by employing None values. |
paul@1244 | 61 | |
paul@1244 | 62 | field_values = map(None, dates, hours, minutes, seconds, tzids) |
paul@1244 | 63 | |
paul@1244 | 64 | if not field_values and not multiple: |
paul@1244 | 65 | all_values = FormDate() |
paul@1244 | 66 | else: |
paul@1244 | 67 | all_values = [] |
paul@1244 | 68 | for date, hour, minute, second, tzid_field in field_values: |
paul@1244 | 69 | value = FormDate(date, hour, minute, second, tzid_field or tzid) |
paul@1244 | 70 | |
paul@1244 | 71 | # Return a single value or append to a collection of all values. |
paul@1244 | 72 | |
paul@1244 | 73 | if not multiple: |
paul@1244 | 74 | return value |
paul@1244 | 75 | else: |
paul@1244 | 76 | all_values.append(value) |
paul@1244 | 77 | |
paul@1244 | 78 | return all_values |
paul@1244 | 79 | |
paul@1245 | 80 | def set_date_control_values(formdates, args, name, tzid_name=None): |
paul@1244 | 81 | |
paul@1244 | 82 | """ |
paul@1245 | 83 | Using the values of the given 'formdates', replace form fields in 'args' |
paul@1245 | 84 | starting with 'name'. |
paul@1244 | 85 | |
paul@1244 | 86 | If 'tzid_name' is specified, the time zone information will be stored in |
paul@1244 | 87 | fields starting with 'tzid_name' instead of 'name'. |
paul@1244 | 88 | """ |
paul@1244 | 89 | |
paul@1244 | 90 | args["%s-date" % name] = [] |
paul@1244 | 91 | args["%s-hour" % name] = [] |
paul@1244 | 92 | args["%s-minute" % name] = [] |
paul@1244 | 93 | args["%s-second" % name] = [] |
paul@1244 | 94 | args["%s-tzid" % (tzid_name or name)] = [] |
paul@1244 | 95 | |
paul@1244 | 96 | for d in formdates: |
paul@1246 | 97 | args["%s-date" % name].append(d and d.date or "") |
paul@1246 | 98 | args["%s-hour" % name].append(d and d.hour or "") |
paul@1246 | 99 | args["%s-minute" % name].append(d and d.minute or "") |
paul@1246 | 100 | args["%s-second" % name].append(d and d.second or "") |
paul@1246 | 101 | args["%s-tzid" % (tzid_name or name)].append(d and d.tzid or "") |
paul@1244 | 102 | |
paul@1244 | 103 | def get_period_control_values(args, start_name, end_name, |
paul@1244 | 104 | end_enabled_name, times_enabled_name, |
paul@1244 | 105 | origin=None, origin_name=None, |
paul@1268 | 106 | replacement_name=None, cancelled_name=None, |
paul@1268 | 107 | recurrenceid_name=None, tzid=None): |
paul@1244 | 108 | |
paul@1244 | 109 | """ |
paul@1245 | 110 | Return period values from fields found in 'args' prefixed with the given |
paul@1244 | 111 | 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1244 | 112 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1244 | 113 | (to enable times for periods). |
paul@1244 | 114 | |
paul@1244 | 115 | If 'origin' is specified, a single period with the given origin is |
paul@1244 | 116 | returned. If 'origin_name' is specified, fields containing the name will |
paul@1268 | 117 | provide origin information. |
paul@1268 | 118 | |
paul@1268 | 119 | If specified, fields containing 'replacement_name' will indicate periods |
paul@1268 | 120 | provided by separate recurrences, fields containing 'cancelled_name' |
paul@1268 | 121 | will indicate periods that are replacements and cancelled, and fields |
paul@1268 | 122 | containing 'recurrenceid_name' will indicate periods that have existing |
paul@1268 | 123 | recurrence details from an event. |
paul@1244 | 124 | |
paul@1244 | 125 | If 'tzid' is specified, it will provide the time zone where no explicit |
paul@1244 | 126 | time zone information is indicated in the field data. |
paul@1244 | 127 | """ |
paul@1244 | 128 | |
paul@1244 | 129 | # Get the end datetime and time presence settings. |
paul@1244 | 130 | |
paul@1244 | 131 | all_end_enabled = args.get(end_enabled_name, []) |
paul@1244 | 132 | all_times_enabled = args.get(times_enabled_name, []) |
paul@1244 | 133 | |
paul@1268 | 134 | # Get the origins of period data and whether the periods are replacements. |
paul@1244 | 135 | |
paul@1244 | 136 | if origin: |
paul@1244 | 137 | all_origins = [origin] |
paul@1244 | 138 | else: |
paul@1244 | 139 | all_origins = origin_name and args.get(origin_name, []) or [] |
paul@1244 | 140 | |
paul@1268 | 141 | all_replacements = replacement_name and args.get(replacement_name, []) or [] |
paul@1268 | 142 | all_cancelled = cancelled_name and args.get(cancelled_name, []) or [] |
paul@1266 | 143 | all_recurrenceids = recurrenceid_name and args.get(recurrenceid_name, []) or [] |
paul@1244 | 144 | |
paul@1244 | 145 | # Get the start and end datetimes. |
paul@1244 | 146 | |
paul@1244 | 147 | all_starts = get_date_control_values(args, start_name, True, tzid=tzid) |
paul@1244 | 148 | all_ends = get_date_control_values(args, end_name, True, start_name, tzid=tzid) |
paul@1244 | 149 | |
paul@1245 | 150 | # Construct period objects for each start, end, origin combination. |
paul@1245 | 151 | |
paul@1244 | 152 | periods = [] |
paul@1244 | 153 | |
paul@1266 | 154 | for index, (start, end, found_origin, recurrenceid) in \ |
paul@1266 | 155 | enumerate(map(None, all_starts, all_ends, all_origins, all_recurrenceids)): |
paul@1244 | 156 | |
paul@1245 | 157 | # Obtain period settings from separate controls. |
paul@1245 | 158 | |
paul@1244 | 159 | end_enabled = str(index) in all_end_enabled |
paul@1244 | 160 | times_enabled = str(index) in all_times_enabled |
paul@1268 | 161 | replacement = str(index) in all_replacements |
paul@1268 | 162 | cancelled = str(index) in all_cancelled |
paul@1244 | 163 | |
paul@1244 | 164 | period = FormPeriod(start, end, end_enabled, times_enabled, tzid, |
paul@1268 | 165 | found_origin or origin, replacement, cancelled, |
paul@1268 | 166 | recurrenceid) |
paul@1244 | 167 | periods.append(period) |
paul@1244 | 168 | |
paul@1244 | 169 | # Return a single period if a single origin was specified. |
paul@1244 | 170 | |
paul@1244 | 171 | if origin: |
paul@1244 | 172 | return periods[0] |
paul@1244 | 173 | else: |
paul@1244 | 174 | return periods |
paul@1244 | 175 | |
paul@1245 | 176 | def set_period_control_values(periods, args, start_name, end_name, |
paul@1245 | 177 | end_enabled_name, times_enabled_name, |
paul@1268 | 178 | origin_name=None, replacement_name=None, |
paul@1268 | 179 | cancelled_name=None, recurrenceid_name=None): |
paul@1245 | 180 | |
paul@1245 | 181 | """ |
paul@1245 | 182 | Using the given 'periods', replace form fields in 'args' prefixed with the |
paul@1245 | 183 | given 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1245 | 184 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1245 | 185 | (to enable times for periods). |
paul@1245 | 186 | |
paul@1245 | 187 | If 'origin_name' is specified, fields containing the name will provide |
paul@1268 | 188 | origin information, fields containing 'replacement_name' will indicate |
paul@1268 | 189 | periods provided by separate recurrences, fields containing 'cancelled_name' |
paul@1268 | 190 | will indicate periods that are replacements and cancelled, and fields |
paul@1268 | 191 | containing 'recurrenceid_name' will indicate periods that have existing |
paul@1268 | 192 | recurrence details from an event. |
paul@1245 | 193 | """ |
paul@1245 | 194 | |
paul@1245 | 195 | # Record period settings separately. |
paul@1245 | 196 | |
paul@1245 | 197 | args[end_enabled_name] = [] |
paul@1245 | 198 | args[times_enabled_name] = [] |
paul@1245 | 199 | |
paul@1245 | 200 | # Record origin and replacement information if naming is defined. |
paul@1245 | 201 | |
paul@1245 | 202 | if origin_name: |
paul@1245 | 203 | args[origin_name] = [] |
paul@1245 | 204 | |
paul@1268 | 205 | if replacement_name: |
paul@1268 | 206 | args[replacement_name] = [] |
paul@1268 | 207 | |
paul@1268 | 208 | if cancelled_name: |
paul@1268 | 209 | args[cancelled_name] = [] |
paul@1245 | 210 | |
paul@1266 | 211 | if recurrenceid_name: |
paul@1266 | 212 | args[recurrenceid_name] = [] |
paul@1266 | 213 | |
paul@1245 | 214 | all_starts = [] |
paul@1245 | 215 | all_ends = [] |
paul@1245 | 216 | |
paul@1245 | 217 | for index, period in enumerate(periods): |
paul@1245 | 218 | |
paul@1245 | 219 | # Encode period settings in controls. |
paul@1245 | 220 | |
paul@1245 | 221 | if period.end_enabled: |
paul@1245 | 222 | args[end_enabled_name].append(str(index)) |
paul@1245 | 223 | if period.times_enabled: |
paul@1245 | 224 | args[times_enabled_name].append(str(index)) |
paul@1245 | 225 | |
paul@1245 | 226 | # Add origin information where controls are present to record it. |
paul@1245 | 227 | |
paul@1245 | 228 | if origin_name: |
paul@1245 | 229 | args[origin_name].append(period.origin or "") |
paul@1245 | 230 | |
paul@1245 | 231 | # Add replacement information where controls are present to record it. |
paul@1245 | 232 | |
paul@1268 | 233 | if replacement_name and period.replacement: |
paul@1268 | 234 | args[replacement_name].append(str(index)) |
paul@1268 | 235 | |
paul@1268 | 236 | # Add cancelled recurrence information where controls are present to |
paul@1268 | 237 | # record it. |
paul@1268 | 238 | |
paul@1268 | 239 | if cancelled_name and period.cancelled: |
paul@1268 | 240 | args[cancelled_name].append(str(index)) |
paul@1245 | 241 | |
paul@1266 | 242 | # Add recurrence identifiers where controls are present to record it. |
paul@1266 | 243 | |
paul@1266 | 244 | if recurrenceid_name: |
paul@1266 | 245 | args[recurrenceid_name].append(period.recurrenceid or "") |
paul@1266 | 246 | |
paul@1245 | 247 | # Collect form date information for addition below. |
paul@1245 | 248 | |
paul@1245 | 249 | all_starts.append(period.get_form_start()) |
paul@1245 | 250 | all_ends.append(period.get_form_end()) |
paul@1245 | 251 | |
paul@1245 | 252 | # Set the controls for the dates. |
paul@1245 | 253 | |
paul@1245 | 254 | set_date_control_values(all_starts, args, start_name) |
paul@1245 | 255 | set_date_control_values(all_ends, args, end_name, tzid_name=start_name) |
paul@1245 | 256 | |
paul@1257 | 257 | |
paul@1257 | 258 | |
paul@1257 | 259 | # Utilities. |
paul@1257 | 260 | |
paul@1257 | 261 | def filter_duplicates(l): |
paul@1257 | 262 | |
paul@1257 | 263 | """ |
paul@1257 | 264 | Return collection 'l' filtered for duplicate values, retaining the given |
paul@1257 | 265 | element ordering. |
paul@1257 | 266 | """ |
paul@1257 | 267 | |
paul@1257 | 268 | s = set() |
paul@1257 | 269 | f = [] |
paul@1257 | 270 | |
paul@1257 | 271 | for value in l: |
paul@1257 | 272 | if value not in s: |
paul@1257 | 273 | s.add(value) |
paul@1257 | 274 | f.append(value) |
paul@1257 | 275 | |
paul@1257 | 276 | return f |
paul@1257 | 277 | |
paul@1257 | 278 | def remove_from_collection(l, indexes, fn): |
paul@1257 | 279 | |
paul@1257 | 280 | """ |
paul@1257 | 281 | Remove from collection 'l' all values present at the given 'indexes' where |
paul@1257 | 282 | 'fn' applied to each referenced value returns a true value. Values where |
paul@1257 | 283 | 'fn' returns a false value are added to a list of deferred removals which is |
paul@1257 | 284 | returned. |
paul@1257 | 285 | """ |
paul@1257 | 286 | |
paul@1257 | 287 | still_to_remove = [] |
paul@1257 | 288 | correction = 0 |
paul@1257 | 289 | |
paul@1257 | 290 | for i in indexes: |
paul@1257 | 291 | try: |
paul@1257 | 292 | i = int(i) - correction |
paul@1257 | 293 | value = l[i] |
paul@1257 | 294 | except (IndexError, ValueError): |
paul@1257 | 295 | continue |
paul@1257 | 296 | |
paul@1257 | 297 | if fn(value): |
paul@1257 | 298 | del l[i] |
paul@1257 | 299 | correction += 1 |
paul@1257 | 300 | else: |
paul@1258 | 301 | still_to_remove.append(value) |
paul@1257 | 302 | |
paul@1257 | 303 | return still_to_remove |
paul@1257 | 304 | |
paul@497 | 305 | # vim: tabstop=4 expandtab shiftwidth=4 |