# HG changeset patch # User Paul Boddie # Date 1505250149 -7200 # Node ID 34a0d4735e9bb7d18e741225a9401ecf266ffd24 # Parent 47093230ddb8418efcb4b46fc70ad01925a7cc99 Moved various functions into the data module. diff -r 47093230ddb8 -r 34a0d4735e9b imipweb/data.py --- a/imipweb/data.py Tue Sep 12 21:58:33 2017 +0200 +++ b/imipweb/data.py Tue Sep 12 23:02:29 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(str(i)) + + return still_to_remove + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 47093230ddb8 -r 34a0d4735e9b imipweb/event.py --- a/imipweb/event.py Tue Sep 12 21:58:33 2017 +0200 +++ b/imipweb/event.py Tue Sep 12 23:02:29 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, set_period_control_values, \ PeriodError from imipweb.resource import DateTimeFormUtilities, FormUtilities, ResourceClientForObject @@ -996,19 +998,6 @@ return [p.as_event_period(i) for i, p in enumerate(self.get_recurrences_from_page())] - 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): @@ -1061,7 +1050,7 @@ # Get all periods that are not replaced. - active_periods = self.get_active_periods(periods) + active_periods = get_active_periods(periods) for i in args.get("recur-remove", []): try: @@ -1109,50 +1098,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(str(i)) - - return still_to_remove - def update_attendees_from_page(self): "Add or remove attendees. This does not affect the stored object." @@ -1185,12 +1130,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) args["remove"] = still_to_remove if add or add_suggested or remove: - attendees = self.filter_duplicates(attendees) + attendees = filter_duplicates(attendees) args["attendee"] = attendees return attendees @@ -1216,12 +1161,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) args["recur-remove"] = still_to_remove if add or remove: - recurrences = self.filter_duplicates(recurrences) + recurrences = filter_duplicates(recurrences) self.set_recurrences_in_page(recurrences) return recurrences