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@945 | 24 | from imiptools.period import get_common_periods, invert_freebusy, periods_from |
paul@938 | 25 | |
paul@938 | 26 | def schedule_in_freebusy(handler): |
paul@938 | 27 | |
paul@938 | 28 | """ |
paul@938 | 29 | Attempt to schedule the current object of the given 'handler' in the |
paul@938 | 30 | free/busy schedule of a resource, returning an indication of the kind of |
paul@938 | 31 | response to be returned. |
paul@938 | 32 | """ |
paul@938 | 33 | |
paul@938 | 34 | # If newer than any old version, discard old details from the |
paul@938 | 35 | # free/busy record and check for suitability. |
paul@938 | 36 | |
paul@938 | 37 | periods = handler.get_periods(handler.obj) |
paul@938 | 38 | |
paul@938 | 39 | freebusy = handler.store.get_freebusy(handler.user) |
paul@938 | 40 | offers = handler.store.get_freebusy_offers(handler.user) |
paul@938 | 41 | |
paul@938 | 42 | # Check the periods against any scheduled events and against |
paul@938 | 43 | # any outstanding offers. |
paul@938 | 44 | |
paul@938 | 45 | scheduled = handler.can_schedule(freebusy, periods) |
paul@938 | 46 | scheduled = scheduled and handler.can_schedule(offers, periods) |
paul@938 | 47 | |
paul@938 | 48 | return scheduled and "ACCEPTED" or "DECLINED" |
paul@938 | 49 | |
paul@938 | 50 | def schedule_corrected_in_freebusy(handler): |
paul@938 | 51 | |
paul@938 | 52 | """ |
paul@938 | 53 | Attempt to schedule the current object of the given 'handler', correcting |
paul@938 | 54 | specified datetimes according to the configuration of a resource, |
paul@938 | 55 | returning an indication of the kind of response to be returned. |
paul@938 | 56 | """ |
paul@938 | 57 | |
paul@940 | 58 | obj = handler.obj.copy() |
paul@940 | 59 | |
paul@938 | 60 | # Check any constraints on the request. |
paul@938 | 61 | |
paul@938 | 62 | try: |
paul@938 | 63 | corrected = handler.correct_object() |
paul@938 | 64 | |
paul@938 | 65 | # Refuse to schedule obviously invalid requests. |
paul@938 | 66 | |
paul@938 | 67 | except ValidityError: |
paul@938 | 68 | return None |
paul@938 | 69 | |
paul@938 | 70 | # With a valid request, determine whether the event can be scheduled. |
paul@938 | 71 | |
paul@938 | 72 | scheduled = schedule_in_freebusy(handler) |
paul@940 | 73 | |
paul@940 | 74 | # Restore the original object if it was corrected but could not be |
paul@940 | 75 | # scheduled. |
paul@940 | 76 | |
paul@940 | 77 | if scheduled == "DECLINED" and corrected: |
paul@940 | 78 | handler.set_object(obj) |
paul@938 | 79 | |
paul@938 | 80 | # Where the corrected object can be scheduled, issue a counter |
paul@938 | 81 | # request. |
paul@938 | 82 | |
paul@938 | 83 | return scheduled == "ACCEPTED" and (corrected and "COUNTER" or "ACCEPTED") or "DECLINED" |
paul@938 | 84 | |
paul@942 | 85 | def schedule_next_available_in_freebusy(handler): |
paul@942 | 86 | |
paul@942 | 87 | """ |
paul@942 | 88 | Attempt to schedule the current object of the given 'handler', correcting |
paul@942 | 89 | specified datetimes according to the configuration of a resource, then |
paul@942 | 90 | suggesting the next available period in the free/busy records if scheduling |
paul@942 | 91 | cannot occur for the requested period, returning an indication of the kind |
paul@942 | 92 | of response to be returned. |
paul@942 | 93 | """ |
paul@942 | 94 | |
paul@942 | 95 | scheduled = schedule_corrected_in_freebusy(handler) |
paul@942 | 96 | |
paul@942 | 97 | if scheduled in ("ACCEPTED", "COUNTER"): |
paul@942 | 98 | return scheduled |
paul@942 | 99 | |
paul@942 | 100 | # Find free periods, update the object with the details. |
paul@945 | 101 | # There should already be free/busy information for the user. |
paul@942 | 102 | |
paul@945 | 103 | all_free = [invert_freebusy(handler.store.get_freebusy(handler.user))] |
paul@945 | 104 | |
paul@945 | 105 | for attendee in uri_values(handler.obj.get_values("ATTENDEE")): |
paul@945 | 106 | if attendee != handler.user: |
paul@945 | 107 | freebusy = handler.store.get_freebusy_for_other(handler.user, attendee) |
paul@945 | 108 | if freebusy: |
paul@945 | 109 | all_free.append(invert_freebusy(freebusy)) |
paul@945 | 110 | |
paul@945 | 111 | free = get_common_periods(all_free) |
paul@942 | 112 | permitted_values = handler.get_permitted_values() |
paul@942 | 113 | periods = [] |
paul@942 | 114 | |
paul@942 | 115 | # Do not attempt to redefine rule-based periods. |
paul@942 | 116 | |
paul@942 | 117 | last = None |
paul@942 | 118 | |
paul@942 | 119 | for period in handler.get_periods(handler.obj, explicit_only=True): |
paul@942 | 120 | duration = period.get_duration() |
paul@942 | 121 | |
paul@942 | 122 | # Try and schedule periods normally since some of them may be |
paul@942 | 123 | # compatible with the schedule. |
paul@942 | 124 | |
paul@942 | 125 | if permitted_values: |
paul@942 | 126 | period = period.get_corrected(permitted_values) |
paul@942 | 127 | |
paul@942 | 128 | scheduled = handler.can_schedule(freebusy, [period]) |
paul@942 | 129 | |
paul@942 | 130 | if scheduled == "ACCEPTED": |
paul@942 | 131 | periods.append(period) |
paul@942 | 132 | last = period.get_end() |
paul@942 | 133 | continue |
paul@942 | 134 | |
paul@942 | 135 | # Get free periods from the time of each period. |
paul@942 | 136 | |
paul@942 | 137 | for found in periods_from(free, period): |
paul@942 | 138 | |
paul@942 | 139 | # Skip any periods before the last period. |
paul@942 | 140 | |
paul@942 | 141 | if last: |
paul@942 | 142 | if last > found.get_end(): |
paul@942 | 143 | continue |
paul@942 | 144 | |
paul@942 | 145 | # Adjust the start of the free period to exclude the last period. |
paul@942 | 146 | |
paul@942 | 147 | found = found.make_corrected(max(found.get_start(), last), found.get_end()) |
paul@942 | 148 | |
paul@942 | 149 | # Only test free periods long enough to hold the requested period. |
paul@942 | 150 | |
paul@942 | 151 | if found.get_duration() >= duration: |
paul@942 | 152 | |
paul@942 | 153 | # Obtain a possible period, starting at the found point and |
paul@942 | 154 | # with the requested duration. Then, correct the period if |
paul@942 | 155 | # necessary. |
paul@942 | 156 | |
paul@942 | 157 | start = to_timezone(found.get_start(), period.get_tzid()) |
paul@942 | 158 | possible = period.make_corrected(start, start + period.get_duration()) |
paul@942 | 159 | if permitted_values: |
paul@942 | 160 | possible = possible.get_corrected(permitted_values) |
paul@942 | 161 | |
paul@942 | 162 | # Only if the possible period is still within the free period |
paul@942 | 163 | # can it be used. |
paul@942 | 164 | |
paul@942 | 165 | if possible.within(found): |
paul@942 | 166 | periods.append(possible) |
paul@942 | 167 | break |
paul@942 | 168 | |
paul@942 | 169 | # Where no period can be found, decline the invitation. |
paul@942 | 170 | |
paul@942 | 171 | else: |
paul@942 | 172 | return "DECLINED" |
paul@942 | 173 | |
paul@942 | 174 | # Use the found period to set the start of the next window to search. |
paul@942 | 175 | |
paul@942 | 176 | last = periods[-1].get_end() |
paul@942 | 177 | |
paul@942 | 178 | # Replace the periods in the object. |
paul@942 | 179 | |
paul@942 | 180 | obj = handler.obj.copy() |
paul@942 | 181 | changed = handler.obj.set_periods(periods) |
paul@942 | 182 | |
paul@942 | 183 | # Check one last time, reverting the change if not scheduled. |
paul@942 | 184 | |
paul@942 | 185 | scheduled = schedule_in_freebusy(handler) |
paul@942 | 186 | |
paul@942 | 187 | if scheduled == "DECLINED": |
paul@942 | 188 | handler.set_object(obj) |
paul@942 | 189 | |
paul@942 | 190 | return scheduled == "ACCEPTED" and (changed and "COUNTER" or "ACCEPTED") or "DECLINED" |
paul@942 | 191 | |
paul@938 | 192 | # Registry of scheduling functions. |
paul@938 | 193 | |
paul@938 | 194 | scheduling_functions = { |
paul@938 | 195 | "schedule_in_freebusy" : schedule_in_freebusy, |
paul@938 | 196 | "schedule_corrected_in_freebusy" : schedule_corrected_in_freebusy, |
paul@942 | 197 | "schedule_next_available_in_freebusy" : schedule_next_available_in_freebusy, |
paul@938 | 198 | } |
paul@938 | 199 | |
paul@938 | 200 | # vim: tabstop=4 expandtab shiftwidth=4 |