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@1349 | 22 | from imiptools.editing import FormDate, 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@1351 | 107 | recurrenceid_name=None, tzid=None, |
paul@1351 | 108 | start_index=0): |
paul@1244 | 109 | |
paul@1244 | 110 | """ |
paul@1245 | 111 | Return period values from fields found in 'args' prefixed with the given |
paul@1244 | 112 | 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1244 | 113 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1244 | 114 | (to enable times for periods). |
paul@1244 | 115 | |
paul@1244 | 116 | If 'origin' is specified, a single period with the given origin is |
paul@1244 | 117 | returned. If 'origin_name' is specified, fields containing the name will |
paul@1268 | 118 | provide origin information. |
paul@1268 | 119 | |
paul@1268 | 120 | If specified, fields containing 'replacement_name' will indicate periods |
paul@1268 | 121 | provided by separate recurrences, fields containing 'cancelled_name' |
paul@1268 | 122 | will indicate periods that are replacements and cancelled, and fields |
paul@1268 | 123 | containing 'recurrenceid_name' will indicate periods that have existing |
paul@1268 | 124 | recurrence details from an event. |
paul@1244 | 125 | |
paul@1244 | 126 | If 'tzid' is specified, it will provide the time zone where no explicit |
paul@1244 | 127 | time zone information is indicated in the field data. |
paul@1244 | 128 | """ |
paul@1244 | 129 | |
paul@1244 | 130 | # Get the end datetime and time presence settings. |
paul@1244 | 131 | |
paul@1244 | 132 | all_end_enabled = args.get(end_enabled_name, []) |
paul@1244 | 133 | all_times_enabled = args.get(times_enabled_name, []) |
paul@1244 | 134 | |
paul@1268 | 135 | # Get the origins of period data and whether the periods are replacements. |
paul@1244 | 136 | |
paul@1244 | 137 | if origin: |
paul@1244 | 138 | all_origins = [origin] |
paul@1244 | 139 | else: |
paul@1244 | 140 | all_origins = origin_name and args.get(origin_name, []) or [] |
paul@1244 | 141 | |
paul@1268 | 142 | all_replacements = replacement_name and args.get(replacement_name, []) or [] |
paul@1268 | 143 | all_cancelled = cancelled_name and args.get(cancelled_name, []) or [] |
paul@1266 | 144 | all_recurrenceids = recurrenceid_name and args.get(recurrenceid_name, []) or [] |
paul@1244 | 145 | |
paul@1244 | 146 | # Get the start and end datetimes. |
paul@1244 | 147 | |
paul@1244 | 148 | all_starts = get_date_control_values(args, start_name, True, tzid=tzid) |
paul@1244 | 149 | all_ends = get_date_control_values(args, end_name, True, start_name, tzid=tzid) |
paul@1244 | 150 | |
paul@1245 | 151 | # Construct period objects for each start, end, origin combination. |
paul@1245 | 152 | |
paul@1244 | 153 | periods = [] |
paul@1244 | 154 | |
paul@1266 | 155 | for index, (start, end, found_origin, recurrenceid) in \ |
paul@1351 | 156 | enumerate(map(None, all_starts, all_ends, all_origins, all_recurrenceids), |
paul@1351 | 157 | start_index): |
paul@1244 | 158 | |
paul@1245 | 159 | # Obtain period settings from separate controls. |
paul@1245 | 160 | |
paul@1244 | 161 | end_enabled = str(index) in all_end_enabled |
paul@1244 | 162 | times_enabled = str(index) in all_times_enabled |
paul@1268 | 163 | replacement = str(index) in all_replacements |
paul@1268 | 164 | cancelled = str(index) in all_cancelled |
paul@1244 | 165 | |
paul@1244 | 166 | period = FormPeriod(start, end, end_enabled, times_enabled, tzid, |
paul@1268 | 167 | found_origin or origin, replacement, cancelled, |
paul@1268 | 168 | recurrenceid) |
paul@1244 | 169 | periods.append(period) |
paul@1244 | 170 | |
paul@1244 | 171 | # Return a single period if a single origin was specified. |
paul@1244 | 172 | |
paul@1244 | 173 | if origin: |
paul@1244 | 174 | return periods[0] |
paul@1244 | 175 | else: |
paul@1244 | 176 | return periods |
paul@1244 | 177 | |
paul@1245 | 178 | def set_period_control_values(periods, args, start_name, end_name, |
paul@1245 | 179 | end_enabled_name, times_enabled_name, |
paul@1268 | 180 | origin_name=None, replacement_name=None, |
paul@1268 | 181 | cancelled_name=None, recurrenceid_name=None): |
paul@1245 | 182 | |
paul@1245 | 183 | """ |
paul@1245 | 184 | Using the given 'periods', replace form fields in 'args' prefixed with the |
paul@1245 | 185 | given 'start_name' (for start dates), 'end_name' (for end dates), |
paul@1245 | 186 | 'end_enabled_name' (to enable end dates for periods), 'times_enabled_name' |
paul@1245 | 187 | (to enable times for periods). |
paul@1245 | 188 | |
paul@1245 | 189 | If 'origin_name' is specified, fields containing the name will provide |
paul@1268 | 190 | origin information, fields containing 'replacement_name' will indicate |
paul@1268 | 191 | periods provided by separate recurrences, fields containing 'cancelled_name' |
paul@1268 | 192 | will indicate periods that are replacements and cancelled, and fields |
paul@1268 | 193 | containing 'recurrenceid_name' will indicate periods that have existing |
paul@1268 | 194 | recurrence details from an event. |
paul@1245 | 195 | """ |
paul@1245 | 196 | |
paul@1245 | 197 | # Record period settings separately. |
paul@1245 | 198 | |
paul@1245 | 199 | args[end_enabled_name] = [] |
paul@1245 | 200 | args[times_enabled_name] = [] |
paul@1245 | 201 | |
paul@1245 | 202 | # Record origin and replacement information if naming is defined. |
paul@1245 | 203 | |
paul@1245 | 204 | if origin_name: |
paul@1245 | 205 | args[origin_name] = [] |
paul@1245 | 206 | |
paul@1268 | 207 | if replacement_name: |
paul@1268 | 208 | args[replacement_name] = [] |
paul@1268 | 209 | |
paul@1268 | 210 | if cancelled_name: |
paul@1268 | 211 | args[cancelled_name] = [] |
paul@1245 | 212 | |
paul@1266 | 213 | if recurrenceid_name: |
paul@1266 | 214 | args[recurrenceid_name] = [] |
paul@1266 | 215 | |
paul@1245 | 216 | all_starts = [] |
paul@1245 | 217 | all_ends = [] |
paul@1245 | 218 | |
paul@1245 | 219 | for index, period in enumerate(periods): |
paul@1245 | 220 | |
paul@1245 | 221 | # Encode period settings in controls. |
paul@1245 | 222 | |
paul@1245 | 223 | if period.end_enabled: |
paul@1245 | 224 | args[end_enabled_name].append(str(index)) |
paul@1245 | 225 | if period.times_enabled: |
paul@1245 | 226 | args[times_enabled_name].append(str(index)) |
paul@1245 | 227 | |
paul@1245 | 228 | # Add origin information where controls are present to record it. |
paul@1245 | 229 | |
paul@1245 | 230 | if origin_name: |
paul@1245 | 231 | args[origin_name].append(period.origin or "") |
paul@1245 | 232 | |
paul@1245 | 233 | # Add replacement information where controls are present to record it. |
paul@1245 | 234 | |
paul@1268 | 235 | if replacement_name and period.replacement: |
paul@1268 | 236 | args[replacement_name].append(str(index)) |
paul@1268 | 237 | |
paul@1268 | 238 | # Add cancelled recurrence information where controls are present to |
paul@1268 | 239 | # record it. |
paul@1268 | 240 | |
paul@1268 | 241 | if cancelled_name and period.cancelled: |
paul@1268 | 242 | args[cancelled_name].append(str(index)) |
paul@1245 | 243 | |
paul@1266 | 244 | # Add recurrence identifiers where controls are present to record it. |
paul@1266 | 245 | |
paul@1266 | 246 | if recurrenceid_name: |
paul@1266 | 247 | args[recurrenceid_name].append(period.recurrenceid or "") |
paul@1266 | 248 | |
paul@1245 | 249 | # Collect form date information for addition below. |
paul@1245 | 250 | |
paul@1245 | 251 | all_starts.append(period.get_form_start()) |
paul@1245 | 252 | all_ends.append(period.get_form_end()) |
paul@1245 | 253 | |
paul@1245 | 254 | # Set the controls for the dates. |
paul@1245 | 255 | |
paul@1245 | 256 | set_date_control_values(all_starts, args, start_name) |
paul@1245 | 257 | set_date_control_values(all_ends, args, end_name, tzid_name=start_name) |
paul@1245 | 258 | |
paul@1257 | 259 | |
paul@1257 | 260 | |
paul@1257 | 261 | # Utilities. |
paul@1257 | 262 | |
paul@1257 | 263 | def filter_duplicates(l): |
paul@1257 | 264 | |
paul@1257 | 265 | """ |
paul@1257 | 266 | Return collection 'l' filtered for duplicate values, retaining the given |
paul@1257 | 267 | element ordering. |
paul@1257 | 268 | """ |
paul@1257 | 269 | |
paul@1257 | 270 | s = set() |
paul@1257 | 271 | f = [] |
paul@1257 | 272 | |
paul@1257 | 273 | for value in l: |
paul@1257 | 274 | if value not in s: |
paul@1257 | 275 | s.add(value) |
paul@1257 | 276 | f.append(value) |
paul@1257 | 277 | |
paul@1257 | 278 | return f |
paul@1257 | 279 | |
paul@1257 | 280 | def remove_from_collection(l, indexes, fn): |
paul@1257 | 281 | |
paul@1257 | 282 | """ |
paul@1257 | 283 | Remove from collection 'l' all values present at the given 'indexes' where |
paul@1257 | 284 | 'fn' applied to each referenced value returns a true value. Values where |
paul@1257 | 285 | 'fn' returns a false value are added to a list of deferred removals which is |
paul@1257 | 286 | returned. |
paul@1257 | 287 | """ |
paul@1257 | 288 | |
paul@1257 | 289 | still_to_remove = [] |
paul@1257 | 290 | correction = 0 |
paul@1257 | 291 | |
paul@1257 | 292 | for i in indexes: |
paul@1257 | 293 | try: |
paul@1257 | 294 | i = int(i) - correction |
paul@1257 | 295 | value = l[i] |
paul@1257 | 296 | except (IndexError, ValueError): |
paul@1257 | 297 | continue |
paul@1257 | 298 | |
paul@1257 | 299 | if fn(value): |
paul@1257 | 300 | del l[i] |
paul@1257 | 301 | correction += 1 |
paul@1257 | 302 | else: |
paul@1258 | 303 | still_to_remove.append(value) |
paul@1257 | 304 | |
paul@1257 | 305 | return still_to_remove |
paul@1257 | 306 | |
paul@497 | 307 | # vim: tabstop=4 expandtab shiftwidth=4 |