# HG changeset patch # User Paul Boddie # Date 1439678413 -7200 # Node ID f6c8cc83f18a6d3d5cd596b95ce1a1ed3f376029 # Parent 0802aded40fe706bb8df62993e9f5bbbb1d0ea40 Added tentative support for countering requests upon correcting them. diff -r 0802aded40fe -r f6c8cc83f18a imiptools/handlers/resource.py --- a/imiptools/handlers/resource.py Sun Aug 16 00:39:41 2015 +0200 +++ b/imiptools/handlers/resource.py Sun Aug 16 00:40:13 2015 +0200 @@ -20,6 +20,7 @@ """ from imiptools.data import get_address, to_part +from imiptools.dates import ValidityError from imiptools.handlers import Handler from imiptools.handlers.common import CommonFreebusy, Outgoing @@ -41,10 +42,6 @@ organiser_item, attendees = oa - # Process each attendee separately. - - calendar = [] - # Process for the current user, a resource as attendee. if not self.have_new_object() or not self.is_attendee(self.user): @@ -52,11 +49,7 @@ # Collect response objects produced when handling the request. - response = handle_for_attendee() - if response: - calendar.append(response) - - return calendar + handle_for_attendee() def _schedule_for_attendee(self): @@ -64,20 +57,16 @@ Attempt to schedule the current object for the current user. """ + method = "REPLY" + # Check any constraints on the request. try: - check = self.check_object() - - # NOTE: Support countering by correcting any invalid values and - # NOTE: attempting to schedule using the corrected values. - - if check: - raise self.ValidityError + corrected = self.correct_object() # Refuse to schedule obviously invalid requests. - except self.ValidityError: + except ValidityError: scheduled = False # With a valid request, determine whether the event can be scheduled. @@ -94,34 +83,57 @@ freebusy = self.store.get_freebusy(self.user) scheduled = self.can_schedule(freebusy, periods) - # NOTE: Support countering by finding the next available slot if - # NOTE: the event cannot be scheduled. + # Where the corrected object can be scheduled, issue a counter + # request. + + if scheduled and corrected: + method = "COUNTER" + + # Find the next available slot if the event cannot be scheduled. + + #elif not scheduled and len(periods) == 1: + + # # Find a free period, update the object with the details. + + # duration = periods[0].get_duration() + # free = invert_freebusy(freebusy) + + # for found in periods_from(free, periods[0]): + # # NOTE: Correct the found period first. + # if found.get_duration() >= duration + # scheduled = True + # method = "COUNTER" + # # NOTE: Set the period using the original duration. + # break # Update the participation of the resource in the object. - attendee_attr = self.update_participation(self.obj, - scheduled and "ACCEPTED" or "DECLINED") + if method == "REPLY": + attendee_attr = self.update_participation(self.obj, + scheduled and "ACCEPTED" or "DECLINED") - # Set the complete event or an additional occurrence. + # Set the complete event or an additional occurrence. - event = self.obj.to_node() - self.store.set_event(self.user, self.uid, self.recurrenceid, event) + event = self.obj.to_node() + self.store.set_event(self.user, self.uid, self.recurrenceid, event) - # Remove additional recurrences if handling a complete event. + # Remove additional recurrences if handling a complete event. + + if not self.recurrenceid: + self.store.remove_recurrences(self.user, self.uid) - if not self.recurrenceid: - self.store.remove_recurrences(self.user, self.uid) + # Update free/busy information. - # Update free/busy information. - - self.update_event_in_freebusy(from_organiser=False) + self.update_event_in_freebusy(from_organiser=False) + else: + attendee_attr = self.obj.get_value_map("ATTENDEE")[self.user] # Make a version of the object with just this attendee, update the # DTSTAMP in the response, and return the object for sending. self.obj["ATTENDEE"] = [(self.user, attendee_attr)] self.update_dtstamp() - return self.obj.to_node() + self.add_result(method, map(get_address, self.obj.get_values("ORGANIZER")), to_part(method, [self.obj.to_node()])) def _cancel_for_attendee(self): @@ -137,10 +149,6 @@ self.remove_event_from_freebusy() - # No response is required. - - return None - class Event(ResourceHandler): "An event handler." @@ -194,9 +202,7 @@ No support for countering requests is implemented. """ - response = self._record_and_respond(self._schedule_for_attendee) - if response: - self.add_result("REPLY", map(get_address, self.obj.get_values("ORGANIZER")), to_part("REPLY", response)) + self._record_and_respond(self._schedule_for_attendee) class Freebusy(Handler, CommonFreebusy):