1.1 --- a/imiptools/content.py Sun Oct 26 22:21:56 2014 +0100
1.2 +++ b/imiptools/content.py Sun Oct 26 23:10:12 2014 +0100
1.3 @@ -5,10 +5,11 @@
1.4 mechanism employed by specific recipients.
1.5 """
1.6
1.7 -from datetime import date, datetime
1.8 +from datetime import date, datetime, timedelta
1.9 from email.mime.text import MIMEText
1.10 from pytz import timezone, UnknownTimeZoneError
1.11 from vCalendar import parse, ParseError, to_dict
1.12 +from vRecurrence import get_parameters, get_rule
1.13 import imip_store
1.14 import re
1.15
1.16 @@ -149,6 +150,42 @@
1.17 )
1.18 return None
1.19
1.20 +# NOTE: Need to expose the 100 day window for recurring events in the
1.21 +# NOTE: configuration.
1.22 +
1.23 +def get_periods(obj, window_size=100):
1.24 +
1.25 + dtstart = get_utc_datetime(obj, "DTSTART")
1.26 + dtend = get_utc_datetime(obj, "DTEND")
1.27 +
1.28 + # NOTE: Need also DURATION support.
1.29 +
1.30 + duration = dtend - dtstart
1.31 +
1.32 + # Recurrence rules create multiple instances to be checked.
1.33 + # Conflicts may only be assessed within a period defined by policy
1.34 + # for the agent, with instances outside that period being considered
1.35 + # unchecked.
1.36 +
1.37 + window_end = datetime.now() + timedelta(window_size)
1.38 +
1.39 + # NOTE: Need also RDATE and EXDATE support.
1.40 +
1.41 + rrule = get_value(obj, "RRULE")
1.42 +
1.43 + if rrule:
1.44 + selector = get_rule(dtstart, rrule)
1.45 + parameters = get_parameters(rrule)
1.46 + periods = []
1.47 + for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")):
1.48 + start = datetime(*start, tzinfo=timezone("UTC"))
1.49 + end = start + duration
1.50 + periods.append((format_datetime(start), format_datetime(end)))
1.51 + else:
1.52 + periods = [(format_datetime(dtstart), format_datetime(dtend))]
1.53 +
1.54 + return periods
1.55 +
1.56 # Handler mechanism objects.
1.57
1.58 def handle_itip_part(part, recipients, handlers):
1.59 @@ -285,6 +322,9 @@
1.60 def get_utc_datetime(self, name):
1.61 return get_utc_datetime(self.details, name)
1.62
1.63 + def get_periods(self):
1.64 + return get_periods(self.details)
1.65 +
1.66 def filter_by_recipients(self, values):
1.67 return self.recipients.intersection(map(get_address, values))
1.68
2.1 --- a/imiptools/handlers/resource.py Sun Oct 26 22:21:56 2014 +0100
2.2 +++ b/imiptools/handlers/resource.py Sun Oct 26 23:10:12 2014 +0100
2.3 @@ -4,11 +4,9 @@
2.4 Handlers for a resource.
2.5 """
2.6
2.7 -from datetime import date, datetime, timedelta
2.8 from imiptools.content import Handler, format_datetime, to_part
2.9 from imiptools.period import have_conflict, insert_period, remove_period
2.10 from vCalendar import to_node
2.11 -from vRecurrence import get_parameters, get_rule
2.12
2.13 class Event(Handler):
2.14
2.15 @@ -76,36 +74,7 @@
2.16 # If newer than any old version, discard old details from the
2.17 # free/busy record and check for suitability.
2.18
2.19 - dtstart = self.get_utc_datetime("DTSTART")
2.20 - dtend = self.get_utc_datetime("DTEND")
2.21 -
2.22 - # NOTE: Need also DURATION support.
2.23 -
2.24 - duration = dtend - dtstart
2.25 -
2.26 - # Recurrence rules create multiple instances to be checked.
2.27 - # Conflicts may only be assessed within a period defined by policy
2.28 - # for the agent, with instances outside that period being considered
2.29 - # unchecked.
2.30 -
2.31 - # NOTE: Need to expose the 100 day window in the configuration.
2.32 -
2.33 - window_end = datetime.now() + timedelta(100)
2.34 -
2.35 - # NOTE: Need also RDATE and EXDATE support.
2.36 -
2.37 - rrule = self.get_value("RRULE")
2.38 -
2.39 - if rrule:
2.40 - selector = get_rule(dtstart, rrule)
2.41 - parameters = get_parameters(rrule)
2.42 - periods = []
2.43 - for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")):
2.44 - start = datetime(*start, tzinfo=timezone("UTC"))
2.45 - end = start + duration
2.46 - periods.append((format_datetime(start), format_datetime(end)))
2.47 - else:
2.48 - periods = [(format_datetime(dtstart), format_datetime(dtend))]
2.49 + periods = self.get_periods()
2.50
2.51 conflict = False
2.52 freebusy = self.store.get_freebusy(attendee) or []