# HG changeset patch # User Paul Boddie # Date 1423073602 -3600 # Node ID ac813723ed7128daf7eb0c200c0328fb61603970 # Parent c7215c263cb3ea5a5f5324e94c1bbc6d621f7776 Made get_periods a method on the Object class, moving the supporting function to the data module. diff -r c7215c263cb3 -r ac813723ed71 imiptools/content.py --- a/imiptools/content.py Wed Feb 04 15:30:03 2015 +0100 +++ b/imiptools/content.py Wed Feb 04 19:13:22 2015 +0100 @@ -28,7 +28,7 @@ is_new_object, uri_dict, uri_item from imiptools.dates import format_datetime, to_timezone from imiptools.period import can_schedule, insert_period, remove_period, \ - get_periods, remove_from_freebusy, \ + remove_from_freebusy, \ remove_from_freebusy_for_other, \ update_freebusy, update_freebusy_for_other from pytz import timezone @@ -166,9 +166,6 @@ # Access to calendar structures and other data. - def get_periods(self): - return get_periods(self.obj) - def remove_from_freebusy(self, freebusy, attendee): remove_from_freebusy(freebusy, attendee, self.uid, self.store) diff -r c7215c263cb3 -r ac813723ed71 imiptools/data.py --- a/imiptools/data.py Wed Feb 04 15:30:03 2015 +0100 +++ b/imiptools/data.py Wed Feb 04 19:13:22 2015 +0100 @@ -19,9 +19,11 @@ this program. If not, see . """ +from datetime import datetime, timedelta from email.mime.text import MIMEText -from imiptools.dates import get_datetime, to_utc_datetime +from imiptools.dates import format_datetime, get_datetime, to_utc_datetime from vCalendar import iterwrite, parse, ParseError, to_dict, to_node +from vRecurrence import get_parameters, get_rule import email.utils try: @@ -71,6 +73,11 @@ def __delitem__(self, name): del self.details[name] + # Computed results. + + def get_periods(self, window_size=100): + return get_periods(self, window_size) + # Construction and serialisation. def make_calendar(nodes, method=None): @@ -259,4 +266,45 @@ return is_same_sequence and partstat_set or not is_old_sequence +# NOTE: Need to expose the 100 day window for recurring events in the +# NOTE: configuration. + +def get_periods(obj, window_size=100): + + """ + Return periods for the given object 'obj', confining materialised periods + to the given 'window_size' in days starting from the present moment. + """ + + dtstart = obj.get_utc_datetime("DTSTART") + dtend = obj.get_utc_datetime("DTEND") + + # NOTE: Need also DURATION support. + + duration = dtend - dtstart + + # Recurrence rules create multiple instances to be checked. + # Conflicts may only be assessed within a period defined by policy + # for the agent, with instances outside that period being considered + # unchecked. + + window_end = datetime.now() + timedelta(window_size) + + # NOTE: Need also RDATE and EXDATE support. + + rrule = obj.get_value("RRULE") + + if rrule: + selector = get_rule(dtstart, rrule) + parameters = get_parameters(rrule) + periods = [] + for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")): + start = datetime(*start, tzinfo=timezone("UTC")) + end = start + duration + periods.append((format_datetime(start), format_datetime(end))) + else: + periods = [(format_datetime(dtstart), format_datetime(dtend))] + + return periods + # vim: tabstop=4 expandtab shiftwidth=4 diff -r c7215c263cb3 -r ac813723ed71 imiptools/handlers/person.py --- a/imiptools/handlers/person.py Wed Feb 04 15:30:03 2015 +0100 +++ b/imiptools/handlers/person.py Wed Feb 04 19:13:22 2015 +0100 @@ -53,7 +53,7 @@ freebusy = self.store.get_freebusy_for_other(attendee, organiser) if organiser_attr.get("PARTSTAT") != "DECLINED": - self.update_freebusy_for_other(freebusy, attendee, organiser, self.get_periods()) + self.update_freebusy_for_other(freebusy, attendee, organiser, self.obj.get_periods()) else: self.remove_from_freebusy_for_other(freebusy, attendee, organiser) @@ -89,7 +89,7 @@ freebusy = self.store.get_freebusy_for_other(organiser, attendee) if attendee_attr.get("PARTSTAT") != "DECLINED": - self.update_freebusy_for_other(freebusy, organiser, attendee, self.get_periods()) + self.update_freebusy_for_other(freebusy, organiser, attendee, self.obj.get_periods()) else: self.remove_from_freebusy_for_other(freebusy, organiser, attendee) diff -r c7215c263cb3 -r ac813723ed71 imiptools/handlers/person_outgoing.py --- a/imiptools/handlers/person_outgoing.py Wed Feb 04 15:30:03 2015 +0100 +++ b/imiptools/handlers/person_outgoing.py Wed Feb 04 19:13:22 2015 +0100 @@ -68,7 +68,7 @@ # If newer than any old version, discard old details from the # free/busy record and check for suitability. - periods = self.get_periods() + periods = self.obj.get_periods() freebusy = self.store.get_freebusy(identity) if from_organiser or attr.get("PARTSTAT") != "DECLINED": diff -r c7215c263cb3 -r ac813723ed71 imiptools/handlers/resource.py --- a/imiptools/handlers/resource.py Wed Feb 04 15:30:03 2015 +0100 +++ b/imiptools/handlers/resource.py Wed Feb 04 19:13:22 2015 +0100 @@ -59,7 +59,7 @@ # If newer than any old version, discard old details from the # free/busy record and check for suitability. - periods = self.get_periods() + periods = self.obj.get_periods() freebusy = self.store.get_freebusy(attendee) scheduled = self.can_schedule(freebusy, periods) diff -r c7215c263cb3 -r ac813723ed71 imiptools/period.py --- a/imiptools/period.py Wed Feb 04 15:30:03 2015 +0100 +++ b/imiptools/period.py Wed Feb 04 19:13:22 2015 +0100 @@ -20,10 +20,8 @@ """ from bisect import bisect_left, insort_left -from datetime import datetime, timedelta -from imiptools.dates import format_datetime, get_datetime, get_start_of_day, \ - to_timezone -from vRecurrence import get_parameters, get_rule +from datetime import datetime +from imiptools.dates import get_datetime, get_start_of_day, to_timezone # Time management with datetime strings. @@ -330,47 +328,6 @@ return start, end, uid, key -# NOTE: Need to expose the 100 day window for recurring events in the -# NOTE: configuration. - -def get_periods(obj, window_size=100): - - """ - Return periods for the given object 'obj', confining materialised periods - to the given 'window_size' in days starting from the present moment. - """ - - dtstart = obj.get_utc_datetime("DTSTART") - dtend = obj.get_utc_datetime("DTEND") - - # NOTE: Need also DURATION support. - - duration = dtend - dtstart - - # Recurrence rules create multiple instances to be checked. - # Conflicts may only be assessed within a period defined by policy - # for the agent, with instances outside that period being considered - # unchecked. - - window_end = datetime.now() + timedelta(window_size) - - # NOTE: Need also RDATE and EXDATE support. - - rrule = obj.get_value("RRULE") - - if rrule: - selector = get_rule(dtstart, rrule) - parameters = get_parameters(rrule) - periods = [] - for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")): - start = datetime(*start, tzinfo=timezone("UTC")) - end = start + duration - periods.append((format_datetime(start), format_datetime(end))) - else: - periods = [(format_datetime(dtstart), format_datetime(dtend))] - - return periods - def remove_from_freebusy(freebusy, attendee, uid, store): """