1.1 --- a/imip_resource.py Wed Oct 22 14:20:59 2014 +0200
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,272 +0,0 @@
1.4 -#!/usr/bin/env python
1.5 -
1.6 -"""
1.7 -Handlers for a resource.
1.8 -"""
1.9 -
1.10 -from datetime import date, datetime, timedelta
1.11 -from imiptools.content import Handler, format_datetime
1.12 -from imiptools.period import insert_period, period_overlaps, remove_period
1.13 -from vCalendar import to_node
1.14 -from vRecurrence import get_parameters, get_rule
1.15 -
1.16 -class Event(Handler):
1.17 -
1.18 - "An event handler."
1.19 -
1.20 - def add(self):
1.21 - pass
1.22 -
1.23 - def cancel(self):
1.24 - pass
1.25 -
1.26 - def counter(self):
1.27 -
1.28 - "Since this handler does not send requests, it will not handle replies."
1.29 -
1.30 - pass
1.31 -
1.32 - def declinecounter(self):
1.33 -
1.34 - """
1.35 - Since this handler does not send counter proposals, it will not handle
1.36 - replies to such proposals.
1.37 - """
1.38 -
1.39 - pass
1.40 -
1.41 - def publish(self):
1.42 - pass
1.43 -
1.44 - def refresh(self):
1.45 - pass
1.46 -
1.47 - def reply(self):
1.48 -
1.49 - "Since this handler does not send requests, it will not handle replies."
1.50 -
1.51 - pass
1.52 -
1.53 - def request(self):
1.54 -
1.55 - """
1.56 - Respond to a request by preparing a reply containing accept/decline
1.57 - information for each indicated attendee.
1.58 -
1.59 - No support for countering requests is implemented.
1.60 - """
1.61 -
1.62 - oa = self.require_organiser_and_attendees()
1.63 - if not oa:
1.64 - return None
1.65 -
1.66 - (organiser, organiser_attr), attendees = oa
1.67 -
1.68 - # Process each attendee separately.
1.69 -
1.70 - calendar = []
1.71 -
1.72 - for attendee, attendee_attr in attendees.items():
1.73 -
1.74 - # Check for event using UID.
1.75 -
1.76 - if not self.have_new_object(attendee, "VEVENT"):
1.77 - continue
1.78 -
1.79 - # If newer than any old version, discard old details from the
1.80 - # free/busy record and check for suitability.
1.81 -
1.82 - dtstart = self.get_utc_datetime("DTSTART")
1.83 - dtend = self.get_utc_datetime("DTEND")
1.84 -
1.85 - # NOTE: Need also DURATION support.
1.86 -
1.87 - duration = dtend - dtstart
1.88 -
1.89 - # Recurrence rules create multiple instances to be checked.
1.90 - # Conflicts may only be assessed within a period defined by policy
1.91 - # for the agent, with instances outside that period being considered
1.92 - # unchecked.
1.93 -
1.94 - # NOTE: Need to expose the 100 day window in the configuration.
1.95 -
1.96 - window_end = datetime.now() + timedelta(100)
1.97 -
1.98 - # NOTE: Need also RDATE and EXDATE support.
1.99 -
1.100 - rrule = self.get_value("RRULE")
1.101 -
1.102 - if rrule:
1.103 - selector = get_rule(dtstart, rrule)
1.104 - parameters = get_parameters(rrule)
1.105 - periods = []
1.106 - for start in selector.materialise(dtstart, window_end, parameters.get("COUNT"), parameters.get("BYSETPOS")):
1.107 - start = datetime(*start, tzinfo=timezone("UTC"))
1.108 - end = start + duration
1.109 - periods.append((format_datetime(start), format_datetime(end)))
1.110 - else:
1.111 - periods = [(format_datetime(dtstart), format_datetime(dtend))]
1.112 -
1.113 - conflict = False
1.114 - freebusy = self.store.get_freebusy(attendee)
1.115 -
1.116 - if freebusy:
1.117 - remove_period(freebusy, self.uid)
1.118 - conflict = True
1.119 - for start, end in periods:
1.120 - if period_overlaps(freebusy, (start, end)):
1.121 - break
1.122 - else:
1.123 - conflict = False
1.124 - else:
1.125 - freebusy = []
1.126 -
1.127 - # If the event can be scheduled, it is registered and a reply sent
1.128 - # accepting the event. (The attendee has PARTSTAT=ACCEPTED as an
1.129 - # attribute.)
1.130 -
1.131 - if not conflict:
1.132 - for start, end in periods:
1.133 - insert_period(freebusy, (start, end, self.uid))
1.134 -
1.135 - if self.get_value("TRANSP") in (None, "OPAQUE"):
1.136 - self.store.set_freebusy(attendee, freebusy)
1.137 -
1.138 - if self.publisher:
1.139 - self.publisher.set_freebusy(attendee, freebusy)
1.140 -
1.141 - self.store.set_event(attendee, self.uid, to_node(
1.142 - {"VEVENT" : [(self.details, {})]}
1.143 - ))
1.144 - attendee_attr["PARTSTAT"] = "ACCEPTED"
1.145 -
1.146 - # If the event cannot be scheduled, it is not registered and a reply
1.147 - # sent declining the event. (The attendee has PARTSTAT=DECLINED as an
1.148 - # attribute.)
1.149 -
1.150 - else:
1.151 - attendee_attr["PARTSTAT"] = "DECLINED"
1.152 -
1.153 - self.details["ATTENDEE"] = [(attendee, attendee_attr)]
1.154 - calendar.append(to_node(
1.155 - {"VEVENT" : [(self.details, {})]}
1.156 - ))
1.157 -
1.158 - return calendar
1.159 -
1.160 -class Freebusy(Handler):
1.161 -
1.162 - "A free/busy handler."
1.163 -
1.164 - def publish(self):
1.165 - pass
1.166 -
1.167 - def reply(self):
1.168 -
1.169 - "Since this handler does not send requests, it will not handle replies."
1.170 -
1.171 - pass
1.172 -
1.173 - def request(self):
1.174 -
1.175 - """
1.176 - Respond to a request by preparing a reply containing free/busy
1.177 - information for each indicated attendee.
1.178 - """
1.179 -
1.180 - oa = self.require_organiser_and_attendees()
1.181 - if not oa:
1.182 - return None
1.183 -
1.184 - (organiser, organiser_attr), attendees = oa
1.185 -
1.186 - # Construct an appropriate fragment.
1.187 -
1.188 - calendar = []
1.189 - cwrite = calendar.append
1.190 -
1.191 - # Get the details for each attendee.
1.192 -
1.193 - for attendee, attendee_attr in attendees.items():
1.194 - freebusy = self.store.get_freebusy(attendee)
1.195 -
1.196 - if freebusy:
1.197 - record = []
1.198 - rwrite = record.append
1.199 -
1.200 - rwrite(("ORGANIZER", organiser_attr, organiser))
1.201 - rwrite(("ATTENDEE", attendee_attr, attendee))
1.202 - rwrite(("UID", {}, self.uid))
1.203 -
1.204 - for start, end, uid in freebusy:
1.205 - rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, [start, end]))
1.206 -
1.207 - cwrite(("VFREEBUSY", {}, record))
1.208 -
1.209 - # Return the reply.
1.210 -
1.211 - return calendar
1.212 -
1.213 -class Journal(Handler):
1.214 -
1.215 - "A journal entry handler."
1.216 -
1.217 - def add(self):
1.218 - pass
1.219 -
1.220 - def cancel(self):
1.221 - pass
1.222 -
1.223 - def publish(self):
1.224 - pass
1.225 -
1.226 -class Todo(Handler):
1.227 -
1.228 - "A to-do item handler."
1.229 -
1.230 - def add(self):
1.231 - pass
1.232 -
1.233 - def cancel(self):
1.234 - pass
1.235 -
1.236 - def counter(self):
1.237 -
1.238 - "Since this handler does not send requests, it will not handle replies."
1.239 -
1.240 - pass
1.241 -
1.242 - def declinecounter(self):
1.243 -
1.244 - """
1.245 - Since this handler does not send counter proposals, it will not handle
1.246 - replies to such proposals.
1.247 - """
1.248 -
1.249 - pass
1.250 -
1.251 - def publish(self):
1.252 - pass
1.253 -
1.254 - def refresh(self):
1.255 - pass
1.256 -
1.257 - def reply(self):
1.258 -
1.259 - "Since this handler does not send requests, it will not handle replies."
1.260 -
1.261 - pass
1.262 -
1.263 - def request(self):
1.264 - pass
1.265 -
1.266 -# Handler registry.
1.267 -
1.268 -handlers = [
1.269 - ("VFREEBUSY", Freebusy),
1.270 - ("VEVENT", Event),
1.271 - ("VTODO", Todo),
1.272 - ("VJOURNAL", Journal),
1.273 - ]
1.274 -
1.275 -# vim: tabstop=4 expandtab shiftwidth=4