1.1 --- a/imiptools/handlers/__init__.py Tue Mar 31 19:09:11 2015 +0200
1.2 +++ b/imiptools/handlers/__init__.py Tue Mar 31 19:12:21 2015 +0200
1.3 @@ -21,11 +21,12 @@
1.4
1.5 from datetime import datetime
1.6 from email.mime.text import MIMEText
1.7 +from imiptools.client import Client
1.8 from imiptools.config import MANAGER_PATH, MANAGER_URL
1.9 from imiptools.data import Object, \
1.10 - get_address, get_uri, get_value, get_window_end, \
1.11 + get_address, get_uri, get_value, \
1.12 is_new_object, uri_dict, uri_item, uri_values
1.13 -from imiptools.dates import format_datetime, get_default_timezone, to_timezone
1.14 +from imiptools.dates import format_datetime, to_timezone
1.15 from imiptools.period import can_schedule, remove_period, \
1.16 remove_additional_periods, remove_affected_period, \
1.17 update_freebusy
1.18 @@ -45,7 +46,7 @@
1.19 recurrenceid and "/%s" % recurrenceid or ""
1.20 )
1.21
1.22 -class Handler:
1.23 +class Handler(Client):
1.24
1.25 "General handler support."
1.26
1.27 @@ -56,6 +57,8 @@
1.28 'recipient' of the object (if specifically indicated).
1.29 """
1.30
1.31 + Client.__init__(self, recipient and get_uri(recipient))
1.32 +
1.33 self.senders = senders and set(map(get_address, senders))
1.34 self.recipient = recipient and get_address(recipient)
1.35 self.messenger = messenger
1.36 @@ -186,65 +189,66 @@
1.37
1.38 # Convenience methods for updating stored free/busy information.
1.39
1.40 - def update_freebusy_from_participant(self, user, participant_item, for_organiser):
1.41 + def update_freebusy_from_participant(self, participant_item, for_organiser):
1.42
1.43 """
1.44 - For the given 'user', record the free/busy information for the
1.45 + For the calendar user, record the free/busy information for the
1.46 'participant_item' (a value plus attributes) representing a different
1.47 identity, thus maintaining a separate record of their free/busy details.
1.48 """
1.49
1.50 participant, participant_attr = participant_item
1.51
1.52 - if participant == user:
1.53 + if participant == self.user:
1.54 return
1.55
1.56 - freebusy = self.store.get_freebusy_for_other(user, participant)
1.57 - tzid = self.get_tzid(user)
1.58 - window_end = get_window_end(tzid)
1.59 + freebusy = self.store.get_freebusy_for_other(self.user, participant)
1.60
1.61 # Obtain the stored object if the current object is not issued by the
1.62 # organiser.
1.63
1.64 - obj = for_organiser and self.obj or self.get_object(user)
1.65 + obj = for_organiser and self.obj or self.get_object()
1.66 if not obj:
1.67 return
1.68
1.69 # Obtain the affected periods.
1.70
1.71 - periods = obj.get_periods_for_freebusy(tzid, window_end)
1.72 + periods = obj.get_periods_for_freebusy(self.get_tzid(), self.get_window_end())
1.73
1.74 # Record in the free/busy details unless a non-participating attendee.
1.75
1.76 self.update_freebusy_for_participant(freebusy, periods, participant_attr,
1.77 - for_organiser and self.is_not_attendee(participant, self.obj))
1.78 + for_organiser and not self.is_attendee(participant))
1.79
1.80 - self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(user, self.uid))
1.81 - self.store.set_freebusy_for_other(user, freebusy, participant)
1.82 + self.remove_freebusy_for_recurrences(freebusy, self.store.get_recurrences(self.user, self.uid))
1.83 + self.store.set_freebusy_for_other(self.user, freebusy, participant)
1.84
1.85 - def update_freebusy_from_organiser(self, attendee, organiser_item):
1.86 + def update_freebusy_from_organiser(self, organiser_item):
1.87
1.88 """
1.89 - For the 'attendee', record free/busy information from the
1.90 + For the current user, record free/busy information from the
1.91 'organiser_item' (a value plus attributes).
1.92 """
1.93
1.94 - self.update_freebusy_from_participant(attendee, organiser_item, True)
1.95 + self.update_freebusy_from_participant(organiser_item, True)
1.96
1.97 - def update_freebusy_from_attendees(self, organiser, attendees):
1.98 + def update_freebusy_from_attendees(self, attendees):
1.99
1.100 - "For the 'organiser', record free/busy information from 'attendees'."
1.101 + "For the current user, record free/busy information from 'attendees'."
1.102
1.103 for attendee_item in attendees.items():
1.104 - self.update_freebusy_from_participant(organiser, attendee_item, False)
1.105 + self.update_freebusy_from_participant(attendee_item, False)
1.106
1.107 # Logic, filtering and access to calendar structures and other data.
1.108
1.109 - def is_not_attendee(self, identity, obj):
1.110 + def is_attendee(self, identity, obj=None):
1.111
1.112 - "Return whether 'identity' is not an attendee in 'obj'."
1.113 + """
1.114 + Return whether 'identity' is an attendee in the current object, or in
1.115 + 'obj' if specified.
1.116 + """
1.117
1.118 - return identity not in uri_values(obj.get_values("ATTENDEE"))
1.119 + return identity in uri_values((obj or self.obj).get_values("ATTENDEE"))
1.120
1.121 def can_schedule(self, freebusy, periods):
1.122 return can_schedule(freebusy, periods, self.uid, self.recurrenceid)
1.123 @@ -362,41 +366,42 @@
1.124
1.125 return senders
1.126
1.127 - def _get_object(self, user, uid, recurrenceid):
1.128 + def _get_object(self, uid, recurrenceid):
1.129
1.130 """
1.131 - Return the stored object for the given 'user', 'uid' and 'recurrenceid'.
1.132 + Return the stored object for the current user, with the given 'uid' and
1.133 + 'recurrenceid'.
1.134 """
1.135
1.136 - fragment = self.store.get_event(user, uid, recurrenceid)
1.137 + fragment = self.store.get_event(self.user, uid, recurrenceid)
1.138 return fragment and Object(fragment)
1.139
1.140 - def get_object(self, user):
1.141 + def get_object(self):
1.142
1.143 """
1.144 Return the stored object to which the current object refers for the
1.145 - given 'user'.
1.146 + current user.
1.147 """
1.148
1.149 - return self._get_object(user, self.uid, self.recurrenceid)
1.150 + return self._get_object(self.uid, self.recurrenceid)
1.151
1.152 - def get_parent_object(self, user):
1.153 + def get_parent_object(self):
1.154
1.155 """
1.156 Return the parent object to which the current object refers for the
1.157 - given 'user'.
1.158 + current user.
1.159 """
1.160
1.161 - return self.recurrenceid and self._get_object(user, self.uid, None) or None
1.162 + return self.recurrenceid and self._get_object(self.uid, None) or None
1.163
1.164 - def have_new_object(self, attendee, obj=None):
1.165 + def have_new_object(self, obj=None):
1.166
1.167 """
1.168 - Return whether the current object is new to the 'attendee' (or if the
1.169 + Return whether the current object is new to the current user (or if the
1.170 given 'obj' is new).
1.171 """
1.172
1.173 - obj = obj or self.get_object(attendee)
1.174 + obj = obj or self.get_object()
1.175
1.176 # If found, compare SEQUENCE and potentially DTSTAMP.
1.177
1.178 @@ -438,16 +443,16 @@
1.179
1.180 return False
1.181
1.182 - def merge_attendance(self, attendees, identity):
1.183 + def merge_attendance(self, attendees):
1.184
1.185 """
1.186 Merge attendance from the current object's 'attendees' into the version
1.187 - stored for the given 'identity'.
1.188 + stored for the current user.
1.189 """
1.190
1.191 - obj = self.get_object(identity)
1.192 + obj = self.get_object()
1.193
1.194 - if not obj or not self.have_new_object(identity, obj=obj):
1.195 + if not obj or not self.have_new_object(obj):
1.196 return False
1.197
1.198 # Get attendee details in a usable form.
1.199 @@ -469,7 +474,7 @@
1.200 event = obj.to_node()
1.201 recurrenceid = format_datetime(obj.get_utc_datetime("RECURRENCE-ID"))
1.202
1.203 - self.store.set_event(identity, self.uid, self.recurrenceid, event)
1.204 + self.store.set_event(self.user, self.uid, self.recurrenceid, event)
1.205
1.206 return True
1.207
1.208 @@ -488,11 +493,4 @@
1.209 sequence = self.obj.get_value("SEQUENCE") or "0"
1.210 self.obj["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})]
1.211
1.212 - def get_tzid(self, identity):
1.213 -
1.214 - "Return the time regime applicable for the given 'identity'."
1.215 -
1.216 - preferences = Preferences(identity)
1.217 - return preferences.get("TZID") or get_default_timezone()
1.218 -
1.219 # vim: tabstop=4 expandtab shiftwidth=4