imip-agent

imiptools/handlers/resource.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 Handlers for a resource.     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, to_part, uri_dict    23 from imiptools.dates import ValidityError    24 from imiptools.handlers import Handler    25 from imiptools.handlers.common import CommonFreebusy, CommonEvent    26     27 class ResourceHandler(CommonEvent, Handler):    28     29     "Handling mechanisms specific to resources."    30     31     def _record_and_respond(self, handle_for_attendee):    32     33         """    34         Record details from the incoming message, using the given    35         'handle_for_attendee' callable to process any valid message    36         appropriately.    37         """    38     39         oa = self.require_organiser_and_attendees()    40         if not oa:    41             return None    42     43         organiser_item, attendees = oa    44     45         # Process for the current user, a resource as attendee.    46     47         if not self.have_new_object():    48             return None    49     50         # Collect response objects produced when handling the request.    51     52         handle_for_attendee()    53     54     def _add_for_attendee(self):    55     56         """    57         Attempt to add a recurrence to an existing object for the current user.    58         This does not request a response concerning participation, apparently.    59         """    60     61         # Request details where configured, doing so for unknown objects anyway.    62     63         if self.will_refresh():    64             self.make_refresh()    65             return    66     67         # Record the event as a recurrence of the parent object.    68     69         self.update_recurrenceid()    70         self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node())    71     72         # Remove any previous cancellations involving this event.    73     74         self.store.remove_cancellation(self.user, self.uid, self.recurrenceid)    75     76         # Update free/busy information.    77     78         self.update_event_in_freebusy(for_organiser=False)    79     80     def _schedule_for_attendee(self):    81     82         "Attempt to schedule the current object for the current user."    83     84         method = "REPLY"    85         attendee_attr = uri_dict(self.obj.get_value_map("ATTENDEE"))[self.user]    86     87         # Check any constraints on the request.    88     89         try:    90             corrected = self.correct_object()    91     92         # Refuse to schedule obviously invalid requests.    93     94         except ValidityError:    95             attendee_attr = self.update_participation("DECLINED")    96     97         # With a valid request, determine whether the event can be scheduled.    98     99         else:   100             # Interpretation of periods can depend on the time zone.   101    102             tzid = self.get_tzid()   103    104             # If newer than any old version, discard old details from the   105             # free/busy record and check for suitability.   106    107             periods = self.obj.get_periods(tzid, self.get_window_end())   108    109             freebusy = self.store.get_freebusy(self.user)   110             offers = self.store.get_freebusy_offers(self.user)   111    112             # Check the periods against any scheduled events and against   113             # any outstanding offers.   114    115             scheduled = self.can_schedule(freebusy, periods)   116             scheduled = scheduled and self.can_schedule(offers, periods)   117    118             # Where the corrected object can be scheduled, issue a counter   119             # request.   120    121             if scheduled and corrected:   122                 method = "COUNTER"   123    124             # Find the next available slot if the event cannot be scheduled.   125    126             #elif not scheduled and len(periods) == 1:   127    128             #    # Find a free period, update the object with the details.   129    130             #    duration = periods[0].get_duration()   131             #    free = invert_freebusy(freebusy)   132    133             #    for found in periods_from(free, periods[0]):   134             #        # NOTE: Correct the found period first.   135             #        if found.get_duration() >= duration   136             #            scheduled = True   137             #            method = "COUNTER"   138             #            # NOTE: Set the period using the original duration.   139             #            break   140    141             # Update the participation of the resource in the object.   142             # Update free/busy information.   143    144             if method == "REPLY":   145                 attendee_attr = self.update_participation(scheduled and "ACCEPTED" or "DECLINED")   146    147                 self.update_event_in_freebusy(for_organiser=False)   148                 self.remove_event_from_freebusy_offers()   149    150             # For countered proposals, record the offer in the resource's   151             # free/busy collection.   152    153             elif method == "COUNTER":   154                 self.update_event_in_freebusy_offers()   155    156         # Set the complete event or an additional occurrence.   157    158         if method == "REPLY":   159             event = self.obj.to_node()   160             self.store.set_event(self.user, self.uid, self.recurrenceid, event)   161    162             # Remove additional recurrences if handling a complete event.   163             # Also remove any previous cancellations involving this event.   164    165             if not self.recurrenceid:   166                 self.store.remove_recurrences(self.user, self.uid)   167                 self.store.remove_cancellations(self.user, self.uid)   168             else:   169                 self.store.remove_cancellation(self.user, self.uid, self.recurrenceid)   170    171         # Make a version of the object with just this attendee, update the   172         # DTSTAMP in the response, and return the object for sending.   173    174         self.update_sender(attendee_attr)   175         self.obj["ATTENDEE"] = [(self.user, attendee_attr)]   176         self.update_dtstamp()   177         self.add_result(method, map(get_address, self.obj.get_values("ORGANIZER")), to_part(method, [self.obj.to_node()]))   178    179     def _cancel_for_attendee(self):   180    181         """   182         Cancel for the current user their attendance of the event described by   183         the current object.   184         """   185    186         # Update free/busy information.   187    188         self.remove_event_from_freebusy()   189    190         # Update the stored event and cancel it.   191    192         self.store.set_event(self.user, self.uid, self.recurrenceid, self.obj.to_node())   193         self.store.cancel_event(self.user, self.uid, self.recurrenceid)   194    195     def _revoke_for_attendee(self):   196    197         "Revoke any counter-proposal recorded as a free/busy offer."   198    199         self.remove_event_from_freebusy_offers()   200    201 class Event(ResourceHandler):   202    203     "An event handler."   204    205     def add(self):   206    207         "Add a new occurrence to an existing event."   208    209         self._record_and_respond(self._add_for_attendee)   210    211     def cancel(self):   212    213         "Cancel attendance for attendees."   214    215         self._record_and_respond(self._cancel_for_attendee)   216    217     def counter(self):   218    219         "Since this handler does not send requests, it will not handle replies."   220    221         pass   222    223     def declinecounter(self):   224    225         "Revoke any counter-proposal."   226    227         self._record_and_respond(self._revoke_for_attendee)   228    229     def publish(self):   230    231         """   232         Resources only consider events sent as requests, not generally published   233         events.   234         """   235    236         pass   237    238     def refresh(self):   239    240         """   241         Refresh messages are typically sent to event organisers, but resources   242         do not act as organisers themselves.   243         """   244    245         pass   246    247     def reply(self):   248    249         "Since this handler does not send requests, it will not handle replies."   250    251         pass   252    253     def request(self):   254    255         """   256         Respond to a request by preparing a reply containing accept/decline   257         information for the recipient.   258    259         No support for countering requests is implemented.   260         """   261    262         self._record_and_respond(self._schedule_for_attendee)   263    264 class Freebusy(CommonFreebusy, Handler):   265    266     "A free/busy handler."   267    268     def publish(self):   269    270         "Resources ignore generally published free/busy information."   271    272         pass   273    274     def reply(self):   275    276         "Since this handler does not send requests, it will not handle replies."   277    278         pass   279    280     # request provided by CommonFreeBusy.request   281    282 # Handler registry.   283    284 handlers = [   285     ("VFREEBUSY",   Freebusy),   286     ("VEVENT",      Event),   287     ]   288    289 # vim: tabstop=4 expandtab shiftwidth=4