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