imip-agent

imiptools/handlers/common.py

886:8a3994e54ea4
2015-10-20 Paul Boddie Permit the selection of a same-day ending while still allowing time adjustments.
     1 #!/usr/bin/env python     2      3 """     4 Common handler functionality for different entities.     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_address, get_uri, make_freebusy, to_part, \    23                            uri_dict    24 from imiptools.dates import format_datetime    25 from imiptools.period import Period    26     27 class CommonFreebusy:    28     29     "Common free/busy mix-in."    30     31     def request(self):    32     33         """    34         Respond to a request by preparing a reply containing free/busy    35         information for each indicated attendee.    36         """    37     38         oa = self.require_organiser_and_attendees()    39         if not oa:    40             return    41     42         (organiser, organiser_attr), attendees = oa    43     44         # Get the details for each attendee.    45     46         responses = []    47         rwrite = responses.append    48     49         # For replies, the organiser and attendee are preserved.    50     51         for attendee, attendee_attr in attendees.items():    52             freebusy = self.store.get_freebusy(attendee)    53     54             # Indicate the actual sender of the reply.    55     56             self.update_sender(attendee_attr)    57     58             dtstart = self.obj.get_datetime("DTSTART")    59             dtend = self.obj.get_datetime("DTEND")    60             period = dtstart and dtend and Period(dtstart, dtend, self.get_tzid()) or None    61     62             rwrite(make_freebusy(freebusy, self.uid, organiser, organiser_attr, attendee, attendee_attr, period))    63     64         # Return the reply.    65     66         self.add_result("REPLY", [get_address(organiser)], to_part("REPLY", responses))    67     68 class CommonEvent:    69     70     "Common outgoing message handling functionality mix-in."    71     72     def is_usable(self, method=None):    73     74         "Return whether the current object is usable with the given 'method'."    75     76         return self.obj and (    77             method in ("CANCEL", "REFRESH") or    78             self.obj.get_datetime("DTSTART") and    79                 (self.obj.get_datetime("DTEND") or self.obj.get_duration("DURATION")))    80     81     def will_refresh(self):    82     83         """    84         Indicate whether a REFRESH message should be used to respond to an ADD    85         message.    86         """    87     88         return not self.get_stored_object_version() or self.get_add_method_response() == "refresh"    89     90     def make_refresh(self):    91     92         "Make a REFRESH message."    93     94         organiser = get_uri(self.obj.get_value("ORGANIZER"))    95         attendees = uri_dict(self.obj.get_value_map("ATTENDEE"))    96     97         # Indicate the actual sender of the message.    98     99         attendee_attr = attendees[self.user]   100         self.update_sender(attendee_attr)   101    102         # Make a new object with a minimal property selection.   103    104         obj = self.obj.copy()   105         obj.preserve(("ORGANIZER", "DTSTAMP", "UID", "RECURRENCE-ID"))   106         obj["ATTENDEE"] = [(self.user, attendee_attr)]   107    108         # Send a REFRESH message in response.   109    110         self.add_result("REFRESH", [get_address(organiser)], obj.to_part("REFRESH"))   111    112 # vim: tabstop=4 expandtab shiftwidth=4