paul@938 | 1 | #!/usr/bin/env python |
paul@938 | 2 | |
paul@938 | 3 | """ |
paul@938 | 4 | Common scheduling functionality. |
paul@938 | 5 | |
paul@938 | 6 | Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk> |
paul@938 | 7 | |
paul@938 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@938 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@938 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@938 | 11 | version. |
paul@938 | 12 | |
paul@938 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@938 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@938 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@938 | 16 | details. |
paul@938 | 17 | |
paul@938 | 18 | You should have received a copy of the GNU General Public License along with |
paul@938 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@938 | 20 | """ |
paul@938 | 21 | |
paul@945 | 22 | from imiptools.data import uri_values |
paul@942 | 23 | from imiptools.dates import ValidityError, to_timezone |
paul@957 | 24 | from imiptools.period import get_common_periods, invert_freebusy, \ |
paul@957 | 25 | periods_from, remove_period, remove_periods |
paul@938 | 26 | |
paul@938 | 27 | def schedule_in_freebusy(handler): |
paul@938 | 28 | |
paul@938 | 29 | """ |
paul@938 | 30 | Attempt to schedule the current object of the given 'handler' in the |
paul@938 | 31 | free/busy schedule of a resource, returning an indication of the kind of |
paul@938 | 32 | response to be returned. |
paul@938 | 33 | """ |
paul@938 | 34 | |
paul@938 | 35 | # If newer than any old version, discard old details from the |
paul@938 | 36 | # free/busy record and check for suitability. |
paul@938 | 37 | |
paul@938 | 38 | periods = handler.get_periods(handler.obj) |
paul@938 | 39 | |
paul@938 | 40 | freebusy = handler.store.get_freebusy(handler.user) |
paul@938 | 41 | offers = handler.store.get_freebusy_offers(handler.user) |
paul@938 | 42 | |
paul@938 | 43 | # Check the periods against any scheduled events and against |
paul@938 | 44 | # any outstanding offers. |
paul@938 | 45 | |
paul@938 | 46 | scheduled = handler.can_schedule(freebusy, periods) |
paul@938 | 47 | scheduled = scheduled and handler.can_schedule(offers, periods) |
paul@938 | 48 | |
paul@938 | 49 | return scheduled and "ACCEPTED" or "DECLINED" |
paul@938 | 50 | |
paul@938 | 51 | def schedule_corrected_in_freebusy(handler): |
paul@938 | 52 | |
paul@938 | 53 | """ |
paul@938 | 54 | Attempt to schedule the current object of the given 'handler', correcting |
paul@938 | 55 | specified datetimes according to the configuration of a resource, |
paul@938 | 56 | returning an indication of the kind of response to be returned. |
paul@938 | 57 | """ |
paul@938 | 58 | |
paul@940 | 59 | obj = handler.obj.copy() |
paul@940 | 60 | |
paul@938 | 61 | # Check any constraints on the request. |
paul@938 | 62 | |
paul@938 | 63 | try: |
paul@938 | 64 | corrected = handler.correct_object() |
paul@938 | 65 | |
paul@938 | 66 | # Refuse to schedule obviously invalid requests. |
paul@938 | 67 | |
paul@938 | 68 | except ValidityError: |
paul@938 | 69 | return None |
paul@938 | 70 | |
paul@938 | 71 | # With a valid request, determine whether the event can be scheduled. |
paul@938 | 72 | |
paul@938 | 73 | scheduled = schedule_in_freebusy(handler) |
paul@940 | 74 | |
paul@940 | 75 | # Restore the original object if it was corrected but could not be |
paul@940 | 76 | # scheduled. |
paul@940 | 77 | |
paul@940 | 78 | if scheduled == "DECLINED" and corrected: |
paul@940 | 79 | handler.set_object(obj) |
paul@938 | 80 | |
paul@938 | 81 | # Where the corrected object can be scheduled, issue a counter |
paul@938 | 82 | # request. |
paul@938 | 83 | |
paul@938 | 84 | return scheduled == "ACCEPTED" and (corrected and "COUNTER" or "ACCEPTED") or "DECLINED" |
paul@938 | 85 | |
paul@942 | 86 | def schedule_next_available_in_freebusy(handler): |
paul@942 | 87 | |
paul@942 | 88 | """ |
paul@942 | 89 | Attempt to schedule the current object of the given 'handler', correcting |
paul@942 | 90 | specified datetimes according to the configuration of a resource, then |
paul@942 | 91 | suggesting the next available period in the free/busy records if scheduling |
paul@942 | 92 | cannot occur for the requested period, returning an indication of the kind |
paul@942 | 93 | of response to be returned. |
paul@942 | 94 | """ |
paul@942 | 95 | |
paul@942 | 96 | scheduled = schedule_corrected_in_freebusy(handler) |
paul@942 | 97 | |
paul@942 | 98 | if scheduled in ("ACCEPTED", "COUNTER"): |
paul@942 | 99 | return scheduled |
paul@942 | 100 | |
paul@945 | 101 | # There should already be free/busy information for the user. |
paul@942 | 102 | |
paul@957 | 103 | user_freebusy = handler.store.get_freebusy(handler.user) |
paul@957 | 104 | all_freebusy = [user_freebusy] |
paul@957 | 105 | |
paul@957 | 106 | # Subtract any periods from this event from the free/busy collections. |
paul@957 | 107 | |
paul@957 | 108 | event_periods = remove_period(user_freebusy, handler.uid, handler.recurrenceid) |
paul@957 | 109 | |
paul@957 | 110 | # Find busy periods for the other attendees. |
paul@945 | 111 | |
paul@945 | 112 | for attendee in uri_values(handler.obj.get_values("ATTENDEE")): |
paul@945 | 113 | if attendee != handler.user: |
paul@945 | 114 | freebusy = handler.store.get_freebusy_for_other(handler.user, attendee) |
paul@945 | 115 | if freebusy: |
paul@957 | 116 | remove_periods(freebusy, event_periods) |
paul@957 | 117 | all_freebusy.append(freebusy) |
paul@945 | 118 | |
paul@957 | 119 | # Obtain free periods. |
paul@957 | 120 | |
paul@957 | 121 | all_free = [invert_freebusy(fb) for fb in all_freebusy] |
paul@945 | 122 | free = get_common_periods(all_free) |
paul@942 | 123 | permitted_values = handler.get_permitted_values() |
paul@942 | 124 | periods = [] |
paul@942 | 125 | |
paul@942 | 126 | # Do not attempt to redefine rule-based periods. |
paul@942 | 127 | |
paul@942 | 128 | last = None |
paul@942 | 129 | |
paul@942 | 130 | for period in handler.get_periods(handler.obj, explicit_only=True): |
paul@942 | 131 | duration = period.get_duration() |
paul@942 | 132 | |
paul@942 | 133 | # Try and schedule periods normally since some of them may be |
paul@942 | 134 | # compatible with the schedule. |
paul@942 | 135 | |
paul@942 | 136 | if permitted_values: |
paul@942 | 137 | period = period.get_corrected(permitted_values) |
paul@942 | 138 | |
paul@942 | 139 | scheduled = handler.can_schedule(freebusy, [period]) |
paul@942 | 140 | |
paul@942 | 141 | if scheduled == "ACCEPTED": |
paul@942 | 142 | periods.append(period) |
paul@942 | 143 | last = period.get_end() |
paul@942 | 144 | continue |
paul@942 | 145 | |
paul@942 | 146 | # Get free periods from the time of each period. |
paul@942 | 147 | |
paul@942 | 148 | for found in periods_from(free, period): |
paul@942 | 149 | |
paul@942 | 150 | # Skip any periods before the last period. |
paul@942 | 151 | |
paul@942 | 152 | if last: |
paul@942 | 153 | if last > found.get_end(): |
paul@942 | 154 | continue |
paul@942 | 155 | |
paul@942 | 156 | # Adjust the start of the free period to exclude the last period. |
paul@942 | 157 | |
paul@942 | 158 | found = found.make_corrected(max(found.get_start(), last), found.get_end()) |
paul@942 | 159 | |
paul@942 | 160 | # Only test free periods long enough to hold the requested period. |
paul@942 | 161 | |
paul@942 | 162 | if found.get_duration() >= duration: |
paul@942 | 163 | |
paul@942 | 164 | # Obtain a possible period, starting at the found point and |
paul@942 | 165 | # with the requested duration. Then, correct the period if |
paul@942 | 166 | # necessary. |
paul@942 | 167 | |
paul@942 | 168 | start = to_timezone(found.get_start(), period.get_tzid()) |
paul@942 | 169 | possible = period.make_corrected(start, start + period.get_duration()) |
paul@942 | 170 | if permitted_values: |
paul@942 | 171 | possible = possible.get_corrected(permitted_values) |
paul@942 | 172 | |
paul@942 | 173 | # Only if the possible period is still within the free period |
paul@942 | 174 | # can it be used. |
paul@942 | 175 | |
paul@942 | 176 | if possible.within(found): |
paul@942 | 177 | periods.append(possible) |
paul@942 | 178 | break |
paul@942 | 179 | |
paul@942 | 180 | # Where no period can be found, decline the invitation. |
paul@942 | 181 | |
paul@942 | 182 | else: |
paul@942 | 183 | return "DECLINED" |
paul@942 | 184 | |
paul@942 | 185 | # Use the found period to set the start of the next window to search. |
paul@942 | 186 | |
paul@942 | 187 | last = periods[-1].get_end() |
paul@942 | 188 | |
paul@942 | 189 | # Replace the periods in the object. |
paul@942 | 190 | |
paul@942 | 191 | obj = handler.obj.copy() |
paul@942 | 192 | changed = handler.obj.set_periods(periods) |
paul@942 | 193 | |
paul@942 | 194 | # Check one last time, reverting the change if not scheduled. |
paul@942 | 195 | |
paul@942 | 196 | scheduled = schedule_in_freebusy(handler) |
paul@942 | 197 | |
paul@942 | 198 | if scheduled == "DECLINED": |
paul@942 | 199 | handler.set_object(obj) |
paul@942 | 200 | |
paul@942 | 201 | return scheduled == "ACCEPTED" and (changed and "COUNTER" or "ACCEPTED") or "DECLINED" |
paul@942 | 202 | |
paul@938 | 203 | # Registry of scheduling functions. |
paul@938 | 204 | |
paul@938 | 205 | scheduling_functions = { |
paul@938 | 206 | "schedule_in_freebusy" : schedule_in_freebusy, |
paul@938 | 207 | "schedule_corrected_in_freebusy" : schedule_corrected_in_freebusy, |
paul@942 | 208 | "schedule_next_available_in_freebusy" : schedule_next_available_in_freebusy, |
paul@938 | 209 | } |
paul@938 | 210 | |
paul@938 | 211 | # vim: tabstop=4 expandtab shiftwidth=4 |