1.1 --- a/imiptools/period.py Tue Feb 03 23:16:13 2015 +0100
1.2 +++ b/imiptools/period.py Tue Feb 03 23:16:39 2015 +0100
1.3 @@ -20,8 +20,10 @@
1.4 """
1.5
1.6 from bisect import bisect_left, insort_left
1.7 -from datetime import datetime
1.8 -from imiptools.dates import get_datetime, get_start_of_day, to_timezone
1.9 +from datetime import datetime, timedelta
1.10 +from imiptools.dates import format_datetime, get_datetime, get_start_of_day, \
1.11 + to_timezone
1.12 +from vRecurrence import get_parameters, get_rule
1.13
1.14 # Time management with datetime strings.
1.15
1.16 @@ -328,4 +330,98 @@
1.17
1.18 return start, end, uid, key
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 + """
1.26 + Return periods for the given object 'obj', confining materialised periods
1.27 + to the given 'window_size' in days starting from the present moment.
1.28 + """
1.29 +
1.30 + dtstart = obj.get_utc_datetime("DTSTART")
1.31 + dtend = obj.get_utc_datetime("DTEND")
1.32 +
1.33 + # NOTE: Need also DURATION support.
1.34 +
1.35 + duration = dtend - dtstart
1.36 +
1.37 + # Recurrence rules create multiple instances to be checked.
1.38 + # Conflicts may only be assessed within a period defined by policy
1.39 + # for the agent, with instances outside that period being considered
1.40 + # unchecked.
1.41 +
1.42 + window_end = datetime.now() + timedelta(window_size)
1.43 +
1.44 + # NOTE: Need also RDATE and EXDATE support.
1.45 +
1.46 + rrule = obj.get_value("RRULE")
1.47 +
1.48 + if rrule:
1.49 + selector = get_rule(dtstart, rrule)
1.50 + parameters = get_parameters(rrule)
1.51 + periods = []
1.52 + for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")):
1.53 + start = datetime(*start, tzinfo=timezone("UTC"))
1.54 + end = start + duration
1.55 + periods.append((format_datetime(start), format_datetime(end)))
1.56 + else:
1.57 + periods = [(format_datetime(dtstart), format_datetime(dtend))]
1.58 +
1.59 + return periods
1.60 +
1.61 +def remove_from_freebusy(freebusy, attendee, uid, store):
1.62 +
1.63 + """
1.64 + For the given 'attendee', remove periods from 'freebusy' that are associated
1.65 + with 'uid' in the 'store'.
1.66 + """
1.67 +
1.68 + remove_period(freebusy, uid)
1.69 + store.set_freebusy(attendee, freebusy)
1.70 +
1.71 +def remove_from_freebusy_for_other(freebusy, user, other, uid, store):
1.72 +
1.73 + """
1.74 + For the given 'user', remove for the 'other' party periods from 'freebusy'
1.75 + that are associated with 'uid' in the 'store'.
1.76 + """
1.77 +
1.78 + remove_period(freebusy, uid)
1.79 + store.set_freebusy_for_other(user, freebusy, other)
1.80 +
1.81 +def _update_freebusy(freebusy, periods, transp, uid):
1.82 +
1.83 + """
1.84 + Update the free/busy details with the given 'periods', 'transp' setting and
1.85 + 'uid'.
1.86 + """
1.87 +
1.88 + remove_period(freebusy, uid)
1.89 +
1.90 + for start, end in periods:
1.91 + insert_period(freebusy, (start, end, uid, transp))
1.92 +
1.93 +def update_freebusy(freebusy, attendee, periods, transp, uid, store):
1.94 +
1.95 + """
1.96 + For the given 'attendee', update the free/busy details with the given
1.97 + 'periods', 'transp' setting and 'uid' in the 'store'.
1.98 + """
1.99 +
1.100 + _update_freebusy(freebusy, periods, transp, uid)
1.101 + store.set_freebusy(attendee, freebusy)
1.102 +
1.103 +def update_freebusy_for_other(freebusy, user, other, periods, transp, uid, store):
1.104 +
1.105 + """
1.106 + For the given 'user', update the free/busy details of 'other' with the given
1.107 + 'periods', 'transp' setting and 'uid' in the 'store'.
1.108 + """
1.109 +
1.110 + _update_freebusy(freebusy, periods, transp, uid)
1.111 + store.set_freebusy_for_other(user, freebusy, other)
1.112 +
1.113 +
1.114 # vim: tabstop=4 expandtab shiftwidth=4