1.1 --- a/imiptools/client.py Sat Jul 25 19:29:44 2015 +0200
1.2 +++ b/imiptools/client.py Sat Jul 25 23:18:58 2015 +0200
1.3 @@ -19,9 +19,12 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 +from datetime import datetime
1.8 from imiptools.data import get_address, get_uri, get_window_end, uri_dict, uri_items, uri_values
1.9 -from imiptools.dates import get_default_timezone
1.10 +from imiptools.period import update_freebusy
1.11 from imiptools.profile import Preferences
1.12 +from imiptools.dates import format_datetime, get_default_timezone, \
1.13 + to_timezone
1.14
1.15 def update_attendees(obj, attendees, removed):
1.16
1.17 @@ -72,8 +75,9 @@
1.18
1.19 default_window_size = 100
1.20
1.21 - def __init__(self, user):
1.22 + def __init__(self, user, messenger=None):
1.23 self.user = user
1.24 + self.messenger = messenger
1.25 self.preferences = None
1.26
1.27 def get_preferences(self):
1.28 @@ -132,4 +136,79 @@
1.29 if self.messenger and self.messenger.sender != get_address(self.user):
1.30 attr["SENT-BY"] = get_uri(self.messenger.sender)
1.31
1.32 +class ClientForObject(Client):
1.33 +
1.34 + "A client maintaining a specific object."
1.35 +
1.36 + def __init__(self, obj, user, messenger=None):
1.37 + Client.__init__(self, user, messenger)
1.38 + self.set_object(obj)
1.39 +
1.40 + def set_object(self, obj):
1.41 + self.obj = obj
1.42 + self.uid = obj and self.obj.get_uid()
1.43 + self.recurrenceid = obj and self.obj.get_recurrenceid()
1.44 + self.sequence = obj and self.obj.get_value("SEQUENCE")
1.45 + self.dtstamp = obj and self.obj.get_value("DTSTAMP")
1.46 +
1.47 + def _update_freebusy(self, freebusy, periods, recurrenceid, transp=None):
1.48 +
1.49 + """
1.50 + Update the 'freebusy' collection with the given 'periods', indicating an
1.51 + explicit 'recurrenceid' to affect either a recurrence or the parent
1.52 + event.
1.53 + """
1.54 +
1.55 + update_freebusy(freebusy, periods,
1.56 + transp or self.obj.get_value("TRANSP") or "OPAQUE",
1.57 + self.uid, recurrenceid,
1.58 + self.obj.get_value("SUMMARY"),
1.59 + self.obj.get_value("ORGANIZER"))
1.60 +
1.61 + def update_freebusy(self, freebusy, periods, transp=None):
1.62 +
1.63 + """
1.64 + Update the 'freebusy' collection for this event with the given
1.65 + 'periods'.
1.66 + """
1.67 +
1.68 + self._update_freebusy(freebusy, periods, self.recurrenceid, transp)
1.69 +
1.70 + def update_freebusy_for_participant(self, freebusy, periods, attr, for_organiser=False):
1.71 +
1.72 + """
1.73 + Update the 'freebusy' collection using the given 'periods', subject to
1.74 + the 'attr' provided for the participant, indicating whether this is
1.75 + being generated 'for_organiser' or not.
1.76 + """
1.77 +
1.78 + # Organisers employ a special transparency if not attending.
1.79 +
1.80 + if self.is_participating(attr, for_organiser):
1.81 + self.update_freebusy(freebusy, periods,
1.82 + transp=self.get_overriding_transparency(attr, for_organiser))
1.83 + else:
1.84 + self.remove_from_freebusy(freebusy)
1.85 +
1.86 + def is_participating(self, attr, as_organiser=False):
1.87 + return as_organiser or not attr or attr.get("PARTSTAT") != "DECLINED"
1.88 +
1.89 + def get_overriding_transparency(self, attr, as_organiser=False):
1.90 + return as_organiser and not (attr and attr.get("PARTSTAT")) and "ORG" or None
1.91 +
1.92 + def update_dtstamp(self):
1.93 +
1.94 + "Update the DTSTAMP in the current object."
1.95 +
1.96 + dtstamp = self.obj.get_utc_datetime("DTSTAMP")
1.97 + utcnow = to_timezone(datetime.utcnow(), "UTC")
1.98 + self.obj["DTSTAMP"] = [(format_datetime(dtstamp > utcnow and dtstamp or utcnow), {})]
1.99 +
1.100 + def set_sequence(self, increment=False):
1.101 +
1.102 + "Update the SEQUENCE in the current object."
1.103 +
1.104 + sequence = self.obj.get_value("SEQUENCE") or "0"
1.105 + self.obj["SEQUENCE"] = [(str(int(sequence) + (increment and 1 or 0)), {})]
1.106 +
1.107 # vim: tabstop=4 expandtab shiftwidth=4