1.1 --- a/imiptools/handlers/resource.py Sun Oct 26 19:42:40 2014 +0100
1.2 +++ b/imiptools/handlers/resource.py Sun Oct 26 19:43:41 2014 +0100
1.3 @@ -6,7 +6,7 @@
1.4
1.5 from datetime import date, datetime, timedelta
1.6 from imiptools.content import Handler, format_datetime, to_part
1.7 -from imiptools.period import insert_period, period_overlaps, remove_period
1.8 +from imiptools.period import have_conflict, insert_period, remove_period
1.9 from vCalendar import to_node
1.10 from vRecurrence import get_parameters, get_rule
1.11
1.12 @@ -108,18 +108,11 @@
1.13 periods = [(format_datetime(dtstart), format_datetime(dtend))]
1.14
1.15 conflict = False
1.16 - freebusy = self.store.get_freebusy(attendee)
1.17 + freebusy = self.store.get_freebusy(attendee) or []
1.18
1.19 if freebusy:
1.20 remove_period(freebusy, self.uid)
1.21 - conflict = True
1.22 - for start, end in periods:
1.23 - if period_overlaps(freebusy, (start, end)):
1.24 - break
1.25 - else:
1.26 - conflict = False
1.27 - else:
1.28 - freebusy = []
1.29 + conflict = have_conflict(freebusy, periods)
1.30
1.31 # If the event can be scheduled, it is registered and a reply sent
1.32 # accepting the event. (The attendee has PARTSTAT=ACCEPTED as an
2.1 --- a/imiptools/period.py Sun Oct 26 19:42:40 2014 +0100
2.2 +++ b/imiptools/period.py Sun Oct 26 19:43:41 2014 +0100
2.3 @@ -4,6 +4,27 @@
2.4
2.5 # Time management.
2.6
2.7 +def have_conflict(freebusy, periods, get_conflicts=False):
2.8 +
2.9 + """
2.10 + Return whether any period in 'freebusy' overlaps with the given 'periods',
2.11 + returning a collection of such overlapping periods if 'get_conflicts' is
2.12 + set to a true value.
2.13 + """
2.14 +
2.15 + conflicts = []
2.16 + for start, end in periods:
2.17 + period = period_overlaps(freebusy, (start, end))
2.18 + if period:
2.19 + if get_conflicts:
2.20 + conflicts.append(period)
2.21 + else:
2.22 + return True
2.23 + if get_conflicts:
2.24 + return conflicts
2.25 + else:
2.26 + return False
2.27 +
2.28 def insert_period(freebusy, period):
2.29 insort_left(freebusy, period)
2.30
2.31 @@ -17,12 +38,20 @@
2.32 i += 1
2.33
2.34 def period_overlaps(freebusy, period):
2.35 +
2.36 + """
2.37 + Return from 'freebusy' any period overlapping with the given 'period', or
2.38 + None if no overlap occurs.
2.39 + """
2.40 +
2.41 dtstart, dtend = period[:2]
2.42 i = bisect_left(freebusy, (dtstart, dtend, None))
2.43 return (
2.44 - i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend)
2.45 + i < len(freebusy) and (dtend is None or freebusy[i][0] < dtend) and freebusy[i]
2.46 or
2.47 - i > 0 and freebusy[i - 1][1] > dtstart
2.48 + i > 0 and freebusy[i - 1][1] > dtstart and freebusy[i - 1]
2.49 + or
2.50 + None
2.51 )
2.52
2.53 # vim: tabstop=4 expandtab shiftwidth=4