imip-agent

imiptools/handlers/person.py

502:ee8848449822
2015-04-07 Paul Boddie Only remove an event from the free/busy record if cancelling the whole thing, not if only removing attendees.
     1 #!/usr/bin/env python     2      3 """     4 Handlers for a person for whom scheduling is performed.     5      6 Copyright (C) 2014, 2015 Paul Boddie <paul@boddie.org.uk>     7      8 This program is free software; you can redistribute it and/or modify it under     9 the terms of the GNU General Public License as published by the Free Software    10 Foundation; either version 3 of the License, or (at your option) any later    11 version.    12     13 This program is distributed in the hope that it will be useful, but WITHOUT    14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    15 FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more    16 details.    17     18 You should have received a copy of the GNU General Public License along with    19 this program.  If not, see <http://www.gnu.org/licenses/>.    20 """    21     22 from imiptools.data import get_uri, uri_values, values_from_items    23 from imiptools.dates import format_datetime    24 from imiptools.handlers import Handler    25 from imiptools.handlers.common import CommonFreebusy    26 from imiptools.period import Period, replace_overlapping    27 from imiptools.profile import Preferences    28     29 class PersonHandler(Handler):    30     31     "Handling mechanisms specific to people."    32     33     def _record(self, from_organiser=True, queue=False, cancel=False):    34     35         """    36         Record details from the current object given a message originating    37         from an organiser if 'from_organiser' is set to a true value, queuing a    38         request if 'queue' is set to a true value, or cancelling an event if    39         'cancel' is set to a true value.    40         """    41     42         # Obtain valid organiser and attendee details.    43     44         oa = self.require_organiser_and_attendees(from_organiser)    45         if not oa:    46             return False    47     48         (organiser, organiser_attr), attendees = organiser_item, attendees = oa    49     50         # Handle notifications and invitations.    51     52         if from_organiser:    53     54             # Process for the current user, an attendee.    55     56             if not self.have_new_object() or not self.is_attendee(self.user):    57                 return False    58     59             # Set the complete event or an additional occurrence.    60     61             self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node())    62     63             # Remove additional recurrences if handling a complete event.    64     65             if not self.recurrenceid:    66                 self.store.remove_recurrences(self.user, self.uid)    67     68             # Queue any request, if appropriate.    69     70             if queue:    71                 self.store.queue_request(self.user, self.uid, self.recurrenceid)    72     73             # Cancel complete events or particular occurrences in recurring    74             # events.    75     76             elif cancel:    77                 self.store.cancel_event(self.user, self.uid, self.recurrenceid)    78     79                 # No return message will occur to update the free/busy    80                 # information, so this is done here.    81     82                 freebusy = self.store.get_freebusy(self.user)    83                 self.remove_from_freebusy(freebusy)    84     85                 self.store.set_freebusy(self.user, freebusy)    86     87                 if self.publisher and self.is_sharing():    88                     self.publisher.set_freebusy(self.user, freebusy)    89     90             self.update_freebusy_from_organiser(organiser_item)    91     92         # As organiser, update attendance from valid attendees.    93     94         else:    95             if self.merge_attendance(attendees):    96                 self.update_freebusy_from_attendees(attendees)    97     98         return True    99    100     def _record_freebusy(self, from_organiser=True):   101    102         """   103         Record free/busy information for a message originating from an organiser   104         if 'from_organiser' is set to a true value.   105         """   106    107         if from_organiser:   108             organiser_item = self.require_organiser(from_organiser)   109             if not organiser_item:   110                 return   111    112             senders = [organiser_item]   113         else:   114             oa = self.require_organiser_and_attendees(from_organiser)   115             if not oa:   116                 return   117    118             organiser_item, attendees = oa   119             senders = attendees.items()   120    121             if not senders:   122                 return   123    124         freebusy = []   125    126         for value in self.obj.get_values("FREEBUSY") or []:   127             if not isinstance(value, list):   128                 value = [value]   129             for v in value:   130                 try:   131                     start, end = v.split("/", 1)   132                     freebusy.append(Period(start, end))   133                 except ValueError:   134                     pass   135    136         dtstart = format_datetime(self.obj.get_utc_datetime("DTSTART"))   137         dtend = format_datetime(self.obj.get_utc_datetime("DTEND"))   138    139         for sender, sender_attr in senders:   140             stored_freebusy = self.store.get_freebusy_for_other(self.user, sender)   141             replace_overlapping(stored_freebusy, Period(dtstart, dtend), freebusy)   142             self.store.set_freebusy_for_other(self.user, stored_freebusy, sender)   143    144 class Event(PersonHandler):   145    146     "An event handler."   147    148     def add(self):   149    150         # NOTE: Queue a suggested modification to any active event.   151    152         return self.wrap("An addition to an event has been received.", link=False)   153    154     def cancel(self):   155    156         "Queue a cancellation of any active event."   157    158         self._record(from_organiser=True, queue=False, cancel=True)   159         return self.wrap("A cancellation has been received.", link=False)   160    161     def counter(self):   162    163         # NOTE: Queue a suggested modification to any active event.   164    165         return self.wrap("A counter proposal has been received.", link=False)   166    167     def declinecounter(self):   168    169         # NOTE: Queue a suggested modification to any active event.   170    171         return self.wrap("A declining counter proposal has been received.", link=False)   172    173     def publish(self):   174    175         "Register details of any relevant event."   176    177         self._record(from_organiser=True, queue=False)   178         return self.wrap("Details of an event have been received.")   179    180     def refresh(self):   181    182         "Generate details of any active event."   183    184         # NOTE: Return event details if configured to do so.   185    186         return self.wrap("A request for updated event details has been received.")   187    188     def reply(self):   189    190         "Record replies and notify the recipient."   191    192         self._record(from_organiser=False, queue=False)   193         return self.wrap("A reply has been received.")   194    195     def request(self):   196    197         "Hold requests and notify the recipient."   198    199         self._record(from_organiser=True, queue=True)   200         return self.wrap("A request has been received.")   201    202 class Freebusy(PersonHandler, CommonFreebusy):   203    204     "A free/busy handler."   205    206     def publish(self):   207    208         "Register free/busy information."   209    210         self._record_freebusy(from_organiser=True)   211    212         # Produce a message if configured to do so.   213    214         if self.is_notifying():   215             return self.wrap("A free/busy update has been received.", link=False)   216    217     def reply(self):   218    219         "Record replies and notify the recipient."   220    221         self._record_freebusy(from_organiser=False)   222    223         # Produce a message if configured to do so.   224    225         if self.is_notifying():   226             return self.wrap("A reply to a free/busy request has been received.", link=False)   227    228     def request(self):   229    230         """   231         Respond to a request by preparing a reply containing free/busy   232         information for the recipient.   233         """   234    235         # Produce a reply if configured to do so.   236    237         if self.is_sharing():   238             return CommonFreebusy.request(self)   239    240 # Handler registry.   241    242 handlers = [   243     ("VFREEBUSY",   Freebusy),   244     ("VEVENT",      Event),   245     ]   246    247 # vim: tabstop=4 expandtab shiftwidth=4