# HG changeset patch # User Paul Boddie # Date 1505250434 -7200 # Node ID 0b18f5594278372e4a35835f72698e2f4319aedb # Parent 64aacdbd40e25fd6cf8b1c0d33c7e48f0dcefc04# Parent 34a0d4735e9bb7d18e741225a9401ecf266ffd24 Merged changes from concurrent branch. diff -r 64aacdbd40e2 -r 0b18f5594278 imipweb/data.py --- a/imipweb/data.py Tue Sep 12 22:09:24 2017 +0200 +++ b/imipweb/data.py Tue Sep 12 23:07:14 2017 +0200 @@ -343,6 +343,23 @@ +# Form period processing. + +def get_active_periods(periods): + + "Return a mapping of non-replaced periods to counts, given 'periods'." + + active_periods = {} + for p in periods: + if not p.replaced: + if not active_periods.has_key(p): + active_periods[p] = 1 + else: + active_periods[p] += 1 + return active_periods + + + # Form field extraction and serialisation. def get_date_control_inputs(args, name, tzid_name=None): @@ -545,4 +562,52 @@ set_date_control_values(all_starts, args, start_name) set_date_control_values(all_ends, args, end_name, tzid_name=start_name) + + +# Utilities. + +def filter_duplicates(l): + + """ + Return collection 'l' filtered for duplicate values, retaining the given + element ordering. + """ + + s = set() + f = [] + + for value in l: + if value not in s: + s.add(value) + f.append(value) + + return f + +def remove_from_collection(l, indexes, fn): + + """ + Remove from collection 'l' all values present at the given 'indexes' where + 'fn' applied to each referenced value returns a true value. Values where + 'fn' returns a false value are added to a list of deferred removals which is + returned. + """ + + still_to_remove = [] + correction = 0 + + for i in indexes: + try: + i = int(i) - correction + value = l[i] + except (IndexError, ValueError): + continue + + if fn(value): + del l[i] + correction += 1 + else: + still_to_remove.append(value) + + return still_to_remove + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 64aacdbd40e2 -r 0b18f5594278 imipweb/event.py --- a/imipweb/event.py Tue Sep 12 22:09:24 2017 +0200 +++ b/imipweb/event.py Tue Sep 12 23:07:14 2017 +0200 @@ -24,6 +24,8 @@ from imiptools.dates import format_datetime, to_timezone from imiptools.mail import Messenger from imipweb.data import EventPeriod, event_period_from_period, \ + filter_duplicates, get_active_periods, \ + remove_from_collection, \ get_period_control_values, \ PeriodError from imipweb.resource import DateTimeFormUtilities, FormUtilities, ResourceClientForObject @@ -1004,19 +1006,6 @@ periods.append(p.as_event_period(i)) return periods - def get_active_periods(self, periods): - - "Return a mapping of non-replaced periods to counts, given 'periods'." - - active_periods = {} - for p in periods: - if not p.replaced: - if not active_periods.has_key(p): - active_periods[p] = 1 - else: - active_periods[p] += 1 - return active_periods - # Access to form-originating object information. def get_main_period_from_page(self): @@ -1065,50 +1054,6 @@ attendee_map = self.obj.get_value_map("ATTENDEE") return [get_verbose_address(value, attendee_map.get(value)) for value in attendees] - def filter_duplicates(self, l): - - """ - Return collection 'l' filtered for duplicate values, retaining the given - element ordering. - """ - - s = set() - f = [] - - for value in l: - if value not in s: - s.add(value) - f.append(value) - - return f - - def remove_from_collection(self, l, indexes, fn): - - """ - Remove from collection 'l' all values having 'indexes' where 'fn' - applied to each referenced value returns a true value. Values where 'fn' - returns a false value are added to a list of deferred removals which is - returned. - """ - - still_to_remove = [] - correction = 0 - - for i in indexes: - try: - i = int(i) - correction - value = l[i] - except (IndexError, ValueError): - continue - - if fn(value): - del l[i] - correction += 1 - else: - still_to_remove.append(value) - - return still_to_remove - def update_attendees_from_page(self): "Add or remove attendees. This does not affect the stored object." @@ -1141,12 +1086,12 @@ remove = args.has_key("remove") if remove: - still_to_remove = self.remove_from_collection(attendees, + still_to_remove = remove_from_collection(attendees, args["remove"], self.can_remove_attendee) self.set_state("remove", still_to_remove) if add or add_suggested or remove: - attendees = self.filter_duplicates(attendees) + attendees = filter_duplicates(attendees) return attendees @@ -1171,12 +1116,12 @@ remove = args.has_key("recur-remove") if remove: - still_to_remove = self.remove_from_collection(recurrences, + still_to_remove = remove_from_collection(recurrences, args["recur-remove"], self.can_remove_recurrence) self.set_state("recur-remove", still_to_remove) if add or remove: - recurrences = self.filter_duplicates(recurrences) + recurrences = filter_duplicates(recurrences) return recurrences @@ -1273,7 +1218,7 @@ # Get all periods that are not replaced. - active_periods = self.get_active_periods(periods) + active_periods = get_active_periods(periods) for period in self.get_state("recur-remove", list): active_periods[period] -= 1