paul@938 | 1 | #!/usr/bin/env python |
paul@938 | 2 | |
paul@938 | 3 | """ |
paul@938 | 4 | Common scheduling functionality. |
paul@938 | 5 | |
paul@1025 | 6 | Copyright (C) 2015, 2016 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@962 | 24 | from imiptools.period import coalesce_freebusy, invert_freebusy, \ |
paul@959 | 25 | periods_from, remove_event_periods, \ |
paul@959 | 26 | remove_periods |
paul@938 | 27 | |
paul@1025 | 28 | def apply_scheduling_functions(functions, handler): |
paul@1025 | 29 | |
paul@1025 | 30 | """ |
paul@1025 | 31 | Apply the given scheduling 'functions' in the current object of the given |
paul@1025 | 32 | 'handler'. |
paul@1025 | 33 | """ |
paul@1025 | 34 | |
paul@1025 | 35 | response = "ACCEPTED" |
paul@1025 | 36 | |
paul@1025 | 37 | for fn in functions: |
paul@1025 | 38 | |
paul@1025 | 39 | # NOTE: Should signal an error for incorrectly configured resources. |
paul@1025 | 40 | |
paul@1025 | 41 | if not fn: |
paul@1025 | 42 | return "DECLINED" |
paul@1025 | 43 | |
paul@1025 | 44 | # Keep evaluating scheduling functions, stopping only if one |
paul@1025 | 45 | # declines or gives a null response. |
paul@1025 | 46 | |
paul@1025 | 47 | else: |
paul@1025 | 48 | result = fn(handler) |
paul@1025 | 49 | |
paul@1025 | 50 | # Return a negative result immediately. |
paul@1025 | 51 | |
paul@1025 | 52 | if not result or result == "DECLINED": |
paul@1025 | 53 | return result |
paul@1025 | 54 | |
paul@1025 | 55 | # Modify the eventual response from acceptance if a countering |
paul@1025 | 56 | # result is obtained. |
paul@1025 | 57 | |
paul@1025 | 58 | elif response == "ACCEPTED": |
paul@1025 | 59 | response = result |
paul@1025 | 60 | |
paul@1025 | 61 | return response |
paul@1025 | 62 | |
paul@962 | 63 | def schedule_in_freebusy(handler, freebusy=None): |
paul@938 | 64 | |
paul@938 | 65 | """ |
paul@938 | 66 | Attempt to schedule the current object of the given 'handler' in the |
paul@938 | 67 | free/busy schedule of a resource, returning an indication of the kind of |
paul@938 | 68 | response to be returned. |
paul@962 | 69 | |
paul@962 | 70 | If 'freebusy' is specified, the given collection of busy periods will be |
paul@962 | 71 | used to determine whether any conflicts occur. Otherwise, the current user's |
paul@962 | 72 | free/busy records will be used. |
paul@938 | 73 | """ |
paul@938 | 74 | |
paul@938 | 75 | # If newer than any old version, discard old details from the |
paul@938 | 76 | # free/busy record and check for suitability. |
paul@938 | 77 | |
paul@938 | 78 | periods = handler.get_periods(handler.obj) |
paul@938 | 79 | |
paul@962 | 80 | freebusy = freebusy or handler.store.get_freebusy(handler.user) |
paul@938 | 81 | offers = handler.store.get_freebusy_offers(handler.user) |
paul@938 | 82 | |
paul@938 | 83 | # Check the periods against any scheduled events and against |
paul@938 | 84 | # any outstanding offers. |
paul@938 | 85 | |
paul@938 | 86 | scheduled = handler.can_schedule(freebusy, periods) |
paul@938 | 87 | scheduled = scheduled and handler.can_schedule(offers, periods) |
paul@938 | 88 | |
paul@938 | 89 | return scheduled and "ACCEPTED" or "DECLINED" |
paul@938 | 90 | |
paul@938 | 91 | def schedule_corrected_in_freebusy(handler): |
paul@938 | 92 | |
paul@938 | 93 | """ |
paul@938 | 94 | Attempt to schedule the current object of the given 'handler', correcting |
paul@938 | 95 | specified datetimes according to the configuration of a resource, |
paul@938 | 96 | returning an indication of the kind of response to be returned. |
paul@938 | 97 | """ |
paul@938 | 98 | |
paul@940 | 99 | obj = handler.obj.copy() |
paul@940 | 100 | |
paul@938 | 101 | # Check any constraints on the request. |
paul@938 | 102 | |
paul@938 | 103 | try: |
paul@938 | 104 | corrected = handler.correct_object() |
paul@938 | 105 | |
paul@938 | 106 | # Refuse to schedule obviously invalid requests. |
paul@938 | 107 | |
paul@938 | 108 | except ValidityError: |
paul@938 | 109 | return None |
paul@938 | 110 | |
paul@938 | 111 | # With a valid request, determine whether the event can be scheduled. |
paul@938 | 112 | |
paul@938 | 113 | scheduled = schedule_in_freebusy(handler) |
paul@940 | 114 | |
paul@940 | 115 | # Restore the original object if it was corrected but could not be |
paul@940 | 116 | # scheduled. |
paul@940 | 117 | |
paul@940 | 118 | if scheduled == "DECLINED" and corrected: |
paul@940 | 119 | handler.set_object(obj) |
paul@938 | 120 | |
paul@938 | 121 | # Where the corrected object can be scheduled, issue a counter |
paul@938 | 122 | # request. |
paul@938 | 123 | |
paul@938 | 124 | return scheduled == "ACCEPTED" and (corrected and "COUNTER" or "ACCEPTED") or "DECLINED" |
paul@938 | 125 | |
paul@942 | 126 | def schedule_next_available_in_freebusy(handler): |
paul@942 | 127 | |
paul@942 | 128 | """ |
paul@942 | 129 | Attempt to schedule the current object of the given 'handler', correcting |
paul@942 | 130 | specified datetimes according to the configuration of a resource, then |
paul@942 | 131 | suggesting the next available period in the free/busy records if scheduling |
paul@942 | 132 | cannot occur for the requested period, returning an indication of the kind |
paul@942 | 133 | of response to be returned. |
paul@942 | 134 | """ |
paul@942 | 135 | |
paul@942 | 136 | scheduled = schedule_corrected_in_freebusy(handler) |
paul@942 | 137 | |
paul@942 | 138 | if scheduled in ("ACCEPTED", "COUNTER"): |
paul@942 | 139 | return scheduled |
paul@942 | 140 | |
paul@945 | 141 | # There should already be free/busy information for the user. |
paul@942 | 142 | |
paul@957 | 143 | user_freebusy = handler.store.get_freebusy(handler.user) |
paul@962 | 144 | busy = user_freebusy |
paul@957 | 145 | |
paul@957 | 146 | # Subtract any periods from this event from the free/busy collections. |
paul@957 | 147 | |
paul@959 | 148 | event_periods = remove_event_periods(user_freebusy, handler.uid, handler.recurrenceid) |
paul@957 | 149 | |
paul@957 | 150 | # Find busy periods for the other attendees. |
paul@945 | 151 | |
paul@945 | 152 | for attendee in uri_values(handler.obj.get_values("ATTENDEE")): |
paul@945 | 153 | if attendee != handler.user: |
paul@945 | 154 | freebusy = handler.store.get_freebusy_for_other(handler.user, attendee) |
paul@945 | 155 | if freebusy: |
paul@957 | 156 | remove_periods(freebusy, event_periods) |
paul@962 | 157 | busy += freebusy |
paul@962 | 158 | |
paul@962 | 159 | # Obtain the combined busy periods. |
paul@962 | 160 | |
paul@962 | 161 | busy.sort() |
paul@962 | 162 | busy = coalesce_freebusy(busy) |
paul@945 | 163 | |
paul@957 | 164 | # Obtain free periods. |
paul@957 | 165 | |
paul@962 | 166 | free = invert_freebusy(busy) |
paul@942 | 167 | permitted_values = handler.get_permitted_values() |
paul@942 | 168 | periods = [] |
paul@942 | 169 | |
paul@942 | 170 | # Do not attempt to redefine rule-based periods. |
paul@942 | 171 | |
paul@942 | 172 | last = None |
paul@942 | 173 | |
paul@942 | 174 | for period in handler.get_periods(handler.obj, explicit_only=True): |
paul@942 | 175 | duration = period.get_duration() |
paul@942 | 176 | |
paul@942 | 177 | # Try and schedule periods normally since some of them may be |
paul@942 | 178 | # compatible with the schedule. |
paul@942 | 179 | |
paul@942 | 180 | if permitted_values: |
paul@942 | 181 | period = period.get_corrected(permitted_values) |
paul@942 | 182 | |
paul@942 | 183 | scheduled = handler.can_schedule(freebusy, [period]) |
paul@942 | 184 | |
paul@942 | 185 | if scheduled == "ACCEPTED": |
paul@942 | 186 | periods.append(period) |
paul@942 | 187 | last = period.get_end() |
paul@942 | 188 | continue |
paul@942 | 189 | |
paul@942 | 190 | # Get free periods from the time of each period. |
paul@942 | 191 | |
paul@942 | 192 | for found in periods_from(free, period): |
paul@942 | 193 | |
paul@942 | 194 | # Skip any periods before the last period. |
paul@942 | 195 | |
paul@942 | 196 | if last: |
paul@942 | 197 | if last > found.get_end(): |
paul@942 | 198 | continue |
paul@942 | 199 | |
paul@942 | 200 | # Adjust the start of the free period to exclude the last period. |
paul@942 | 201 | |
paul@942 | 202 | found = found.make_corrected(max(found.get_start(), last), found.get_end()) |
paul@942 | 203 | |
paul@942 | 204 | # Only test free periods long enough to hold the requested period. |
paul@942 | 205 | |
paul@942 | 206 | if found.get_duration() >= duration: |
paul@942 | 207 | |
paul@942 | 208 | # Obtain a possible period, starting at the found point and |
paul@942 | 209 | # with the requested duration. Then, correct the period if |
paul@942 | 210 | # necessary. |
paul@942 | 211 | |
paul@942 | 212 | start = to_timezone(found.get_start(), period.get_tzid()) |
paul@942 | 213 | possible = period.make_corrected(start, start + period.get_duration()) |
paul@942 | 214 | if permitted_values: |
paul@942 | 215 | possible = possible.get_corrected(permitted_values) |
paul@942 | 216 | |
paul@942 | 217 | # Only if the possible period is still within the free period |
paul@942 | 218 | # can it be used. |
paul@942 | 219 | |
paul@942 | 220 | if possible.within(found): |
paul@942 | 221 | periods.append(possible) |
paul@942 | 222 | break |
paul@942 | 223 | |
paul@942 | 224 | # Where no period can be found, decline the invitation. |
paul@942 | 225 | |
paul@942 | 226 | else: |
paul@942 | 227 | return "DECLINED" |
paul@942 | 228 | |
paul@942 | 229 | # Use the found period to set the start of the next window to search. |
paul@942 | 230 | |
paul@942 | 231 | last = periods[-1].get_end() |
paul@942 | 232 | |
paul@942 | 233 | # Replace the periods in the object. |
paul@942 | 234 | |
paul@942 | 235 | obj = handler.obj.copy() |
paul@942 | 236 | changed = handler.obj.set_periods(periods) |
paul@942 | 237 | |
paul@942 | 238 | # Check one last time, reverting the change if not scheduled. |
paul@942 | 239 | |
paul@962 | 240 | scheduled = schedule_in_freebusy(handler, busy) |
paul@942 | 241 | |
paul@942 | 242 | if scheduled == "DECLINED": |
paul@942 | 243 | handler.set_object(obj) |
paul@942 | 244 | |
paul@942 | 245 | return scheduled == "ACCEPTED" and (changed and "COUNTER" or "ACCEPTED") or "DECLINED" |
paul@942 | 246 | |
paul@938 | 247 | # Registry of scheduling functions. |
paul@938 | 248 | |
paul@938 | 249 | scheduling_functions = { |
paul@938 | 250 | "schedule_in_freebusy" : schedule_in_freebusy, |
paul@938 | 251 | "schedule_corrected_in_freebusy" : schedule_corrected_in_freebusy, |
paul@942 | 252 | "schedule_next_available_in_freebusy" : schedule_next_available_in_freebusy, |
paul@938 | 253 | } |
paul@938 | 254 | |
paul@938 | 255 | # vim: tabstop=4 expandtab shiftwidth=4 |