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