# HG changeset patch # User Paul Boddie # Date 1414349021 -3600 # Node ID 5815cad7939619fe200b116fac6a2d75b810b712 # Parent 83c4cbfe4673beb4bb8effb9cb62333cc7eed166 Moved conflict detection into a separate function that can return the conflicting periods. diff -r 83c4cbfe4673 -r 5815cad79396 imiptools/handlers/resource.py --- a/imiptools/handlers/resource.py Sun Oct 26 19:42:40 2014 +0100 +++ b/imiptools/handlers/resource.py Sun Oct 26 19:43:41 2014 +0100 @@ -6,7 +6,7 @@ from datetime import date, datetime, timedelta from imiptools.content import Handler, format_datetime, to_part -from imiptools.period import insert_period, period_overlaps, remove_period +from imiptools.period import have_conflict, insert_period, remove_period from vCalendar import to_node from vRecurrence import get_parameters, get_rule @@ -108,18 +108,11 @@ periods = [(format_datetime(dtstart), format_datetime(dtend))] conflict = False - freebusy = self.store.get_freebusy(attendee) + freebusy = self.store.get_freebusy(attendee) or [] if freebusy: remove_period(freebusy, self.uid) - conflict = True - for start, end in periods: - if period_overlaps(freebusy, (start, end)): - break - else: - conflict = False - else: - freebusy = [] + conflict = have_conflict(freebusy, periods) # If the event can be scheduled, it is registered and a reply sent # accepting the event. (The attendee has PARTSTAT=ACCEPTED as an diff -r 83c4cbfe4673 -r 5815cad79396 imiptools/period.py --- a/imiptools/period.py Sun Oct 26 19:42:40 2014 +0100 +++ b/imiptools/period.py Sun Oct 26 19:43:41 2014 +0100 @@ -4,6 +4,27 @@ # Time management. +def have_conflict(freebusy, periods, get_conflicts=False): + + """ + Return whether any period in 'freebusy' overlaps with the given 'periods', + returning a collection of such overlapping periods if 'get_conflicts' is + set to a true value. + """ + + conflicts = [] + for start, end in periods: + period = period_overlaps(freebusy, (start, end)) + if period: + if get_conflicts: + conflicts.append(period) + else: + return True + if get_conflicts: + return conflicts + else: + return False + def insert_period(freebusy, period): insort_left(freebusy, period) @@ -17,12 +38,20 @@ i += 1 def period_overlaps(freebusy, period): + + """ + Return from 'freebusy' any period overlapping with the given 'period', or + None if no overlap occurs. + """ + dtstart, dtend = period[:2] i = bisect_left(freebusy, (dtstart, dtend, None)) return ( - i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend) + i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend) and freebusy[i] or - i > 0 and freebusy[i - 1][1] > dtstart + i > 0 and freebusy[i - 1][1] > dtstart and freebusy[i - 1] + or + None ) # vim: tabstop=4 expandtab shiftwidth=4