paul@1028 | 1 | #!/usr/bin/env python |
paul@1028 | 2 | |
paul@1028 | 3 | """ |
paul@1028 | 4 | Free/busy-related scheduling functionality. |
paul@1028 | 5 | |
paul@1028 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@1028 | 7 | |
paul@1028 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1028 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1028 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1028 | 11 | version. |
paul@1028 | 12 | |
paul@1028 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1028 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1028 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1028 | 16 | details. |
paul@1028 | 17 | |
paul@1028 | 18 | You should have received a copy of the GNU General Public License along with |
paul@1028 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1028 | 20 | """ |
paul@1028 | 21 | |
paul@1028 | 22 | from imiptools.data import uri_values |
paul@1028 | 23 | from imiptools.dates import ValidityError, to_timezone |
paul@1182 | 24 | from imiptools.handlers.scheduling.common import standard_responses |
paul@1028 | 25 | |
paul@1031 | 26 | def schedule_in_freebusy(handler, args, freebusy=None): |
paul@1028 | 27 | |
paul@1028 | 28 | """ |
paul@1028 | 29 | Attempt to schedule the current object of the given 'handler' in the |
paul@1028 | 30 | free/busy schedule of a resource, returning an indication of the kind of |
paul@1028 | 31 | response to be returned. |
paul@1028 | 32 | |
paul@1183 | 33 | The 'args' are used to configure the behaviour of the function. If a number |
paul@1183 | 34 | is indicated, it is taken to mean the number of concurrent events that can |
paul@1183 | 35 | be scheduled at the same point in time, with the default being 1. |
paul@1183 | 36 | |
paul@1028 | 37 | If 'freebusy' is specified, the given collection of busy periods will be |
paul@1028 | 38 | used to determine whether any conflicts occur. Otherwise, the current user's |
paul@1028 | 39 | free/busy records will be used. |
paul@1028 | 40 | """ |
paul@1028 | 41 | |
paul@1183 | 42 | # Permit the indication that concurrent events may be scheduled. |
paul@1183 | 43 | |
paul@1183 | 44 | try: |
paul@1183 | 45 | max_concurrent = int((args[1:] or ["1"])[0]) |
paul@1183 | 46 | except ValueError: |
paul@1183 | 47 | max_concurrent = 1 |
paul@1183 | 48 | |
paul@1028 | 49 | # If newer than any old version, discard old details from the |
paul@1028 | 50 | # free/busy record and check for suitability. |
paul@1028 | 51 | |
paul@1028 | 52 | periods = handler.get_periods(handler.obj) |
paul@1028 | 53 | |
paul@1039 | 54 | freebusy = freebusy or handler.get_store().get_freebusy(handler.user) |
paul@1039 | 55 | offers = handler.get_store().get_freebusy_offers(handler.user) |
paul@1028 | 56 | |
paul@1028 | 57 | # Check the periods against any scheduled events and against |
paul@1028 | 58 | # any outstanding offers. |
paul@1028 | 59 | |
paul@1183 | 60 | if max_concurrent == 1: |
paul@1183 | 61 | scheduled = handler.can_schedule(freebusy, periods) |
paul@1183 | 62 | scheduled = scheduled and handler.can_schedule(offers, periods) |
paul@1183 | 63 | else: |
paul@1183 | 64 | conflicts = get_scheduling_conflicts(handler, freebusy, [handler.user]) |
paul@1183 | 65 | scheduled = conflicts[handler.user] < max_concurrent |
paul@1183 | 66 | conflicts = get_scheduling_conflicts(handler, offers, [handler.user]) |
paul@1183 | 67 | scheduled = scheduled and conflicts[handler.user] < max_concurrent |
paul@1028 | 68 | |
paul@1153 | 69 | return standard_responses(handler, scheduled and "ACCEPTED" or "DECLINED") |
paul@1028 | 70 | |
paul@1031 | 71 | def schedule_corrected_in_freebusy(handler, args): |
paul@1028 | 72 | |
paul@1028 | 73 | """ |
paul@1028 | 74 | Attempt to schedule the current object of the given 'handler', correcting |
paul@1028 | 75 | specified datetimes according to the configuration of a resource, |
paul@1028 | 76 | returning an indication of the kind of response to be returned. |
paul@1183 | 77 | |
paul@1183 | 78 | The 'args' are used to configure the behaviour of the function. If a number |
paul@1183 | 79 | is indicated, it is taken to mean the number of concurrent events that can |
paul@1183 | 80 | be scheduled at the same point in time, with the default being 1. |
paul@1028 | 81 | """ |
paul@1028 | 82 | |
paul@1028 | 83 | obj = handler.obj.copy() |
paul@1028 | 84 | |
paul@1028 | 85 | # Check any constraints on the request. |
paul@1028 | 86 | |
paul@1028 | 87 | try: |
paul@1028 | 88 | corrected = handler.correct_object() |
paul@1028 | 89 | |
paul@1028 | 90 | # Refuse to schedule obviously invalid requests. |
paul@1028 | 91 | |
paul@1028 | 92 | except ValidityError: |
paul@1028 | 93 | return None |
paul@1028 | 94 | |
paul@1028 | 95 | # With a valid request, determine whether the event can be scheduled. |
paul@1028 | 96 | |
paul@1034 | 97 | scheduled = schedule_in_freebusy(handler, args) |
paul@1153 | 98 | response, description = scheduled or ("DECLINED", None) |
paul@1028 | 99 | |
paul@1028 | 100 | # Restore the original object if it was corrected but could not be |
paul@1028 | 101 | # scheduled. |
paul@1028 | 102 | |
paul@1153 | 103 | if response == "DECLINED" and corrected: |
paul@1028 | 104 | handler.set_object(obj) |
paul@1028 | 105 | |
paul@1028 | 106 | # Where the corrected object can be scheduled, issue a counter |
paul@1028 | 107 | # request. |
paul@1028 | 108 | |
paul@1153 | 109 | response = response == "ACCEPTED" and (corrected and "COUNTER" or "ACCEPTED") or "DECLINED" |
paul@1153 | 110 | return standard_responses(handler, response) |
paul@1028 | 111 | |
paul@1031 | 112 | def schedule_next_available_in_freebusy(handler, args): |
paul@1028 | 113 | |
paul@1028 | 114 | """ |
paul@1028 | 115 | Attempt to schedule the current object of the given 'handler', correcting |
paul@1028 | 116 | specified datetimes according to the configuration of a resource, then |
paul@1028 | 117 | suggesting the next available period in the free/busy records if scheduling |
paul@1028 | 118 | cannot occur for the requested period, returning an indication of the kind |
paul@1028 | 119 | of response to be returned. |
paul@1183 | 120 | |
paul@1183 | 121 | The 'args' are used to configure the behaviour of the function. If a number |
paul@1183 | 122 | is indicated, it is taken to mean the number of concurrent events that can |
paul@1183 | 123 | be scheduled at the same point in time, with the default being 1. |
paul@1028 | 124 | """ |
paul@1028 | 125 | |
paul@1153 | 126 | _ = handler.get_translator() |
paul@1028 | 127 | |
paul@1153 | 128 | scheduled = schedule_corrected_in_freebusy(handler, args) |
paul@1153 | 129 | response, description = scheduled or ("DECLINED", None) |
paul@1153 | 130 | |
paul@1153 | 131 | if response in ("ACCEPTED", "COUNTER"): |
paul@1028 | 132 | return scheduled |
paul@1028 | 133 | |
paul@1028 | 134 | # There should already be free/busy information for the user. |
paul@1028 | 135 | |
paul@1039 | 136 | user_freebusy = handler.get_store().get_freebusy(handler.user) |
paul@1071 | 137 | |
paul@1071 | 138 | # Maintain a separate copy of the data. |
paul@1071 | 139 | |
paul@1071 | 140 | busy = user_freebusy.copy() |
paul@1028 | 141 | |
paul@1028 | 142 | # Subtract any periods from this event from the free/busy collections. |
paul@1028 | 143 | |
paul@1071 | 144 | event_periods = handler.remove_from_freebusy(busy) |
paul@1028 | 145 | |
paul@1028 | 146 | # Find busy periods for the other attendees. |
paul@1028 | 147 | |
paul@1028 | 148 | for attendee in uri_values(handler.obj.get_values("ATTENDEE")): |
paul@1028 | 149 | if attendee != handler.user: |
paul@1071 | 150 | |
paul@1071 | 151 | # Get a copy of the attendee's free/busy data. |
paul@1071 | 152 | |
paul@1071 | 153 | freebusy = handler.get_store().get_freebusy_for_other(handler.user, attendee).copy() |
paul@1028 | 154 | if freebusy: |
paul@1062 | 155 | freebusy.remove_periods(event_periods) |
paul@1028 | 156 | busy += freebusy |
paul@1028 | 157 | |
paul@1028 | 158 | # Obtain the combined busy periods. |
paul@1028 | 159 | |
paul@1062 | 160 | busy = busy.coalesce_freebusy() |
paul@1028 | 161 | |
paul@1028 | 162 | # Obtain free periods. |
paul@1028 | 163 | |
paul@1062 | 164 | free = busy.invert_freebusy() |
paul@1028 | 165 | permitted_values = handler.get_permitted_values() |
paul@1028 | 166 | periods = [] |
paul@1028 | 167 | |
paul@1028 | 168 | # Do not attempt to redefine rule-based periods. |
paul@1028 | 169 | |
paul@1028 | 170 | last = None |
paul@1028 | 171 | |
paul@1028 | 172 | for period in handler.get_periods(handler.obj, explicit_only=True): |
paul@1028 | 173 | duration = period.get_duration() |
paul@1028 | 174 | |
paul@1028 | 175 | # Try and schedule periods normally since some of them may be |
paul@1028 | 176 | # compatible with the schedule. |
paul@1028 | 177 | |
paul@1028 | 178 | if permitted_values: |
paul@1028 | 179 | period = period.get_corrected(permitted_values) |
paul@1028 | 180 | |
paul@1152 | 181 | scheduled = handler.can_schedule(busy, [period]) |
paul@1028 | 182 | |
paul@1152 | 183 | if scheduled: |
paul@1028 | 184 | periods.append(period) |
paul@1028 | 185 | last = period.get_end() |
paul@1028 | 186 | continue |
paul@1028 | 187 | |
paul@1028 | 188 | # Get free periods from the time of each period. |
paul@1028 | 189 | |
paul@1062 | 190 | for found in free.periods_from(period): |
paul@1028 | 191 | |
paul@1028 | 192 | # Skip any periods before the last period. |
paul@1028 | 193 | |
paul@1028 | 194 | if last: |
paul@1028 | 195 | if last > found.get_end(): |
paul@1028 | 196 | continue |
paul@1028 | 197 | |
paul@1028 | 198 | # Adjust the start of the free period to exclude the last period. |
paul@1028 | 199 | |
paul@1028 | 200 | found = found.make_corrected(max(found.get_start(), last), found.get_end()) |
paul@1028 | 201 | |
paul@1028 | 202 | # Only test free periods long enough to hold the requested period. |
paul@1028 | 203 | |
paul@1028 | 204 | if found.get_duration() >= duration: |
paul@1028 | 205 | |
paul@1028 | 206 | # Obtain a possible period, starting at the found point and |
paul@1028 | 207 | # with the requested duration. Then, correct the period if |
paul@1028 | 208 | # necessary. |
paul@1028 | 209 | |
paul@1028 | 210 | start = to_timezone(found.get_start(), period.get_tzid()) |
paul@1028 | 211 | possible = period.make_corrected(start, start + period.get_duration()) |
paul@1028 | 212 | if permitted_values: |
paul@1028 | 213 | possible = possible.get_corrected(permitted_values) |
paul@1028 | 214 | |
paul@1028 | 215 | # Only if the possible period is still within the free period |
paul@1028 | 216 | # can it be used. |
paul@1028 | 217 | |
paul@1028 | 218 | if possible.within(found): |
paul@1028 | 219 | periods.append(possible) |
paul@1028 | 220 | break |
paul@1028 | 221 | |
paul@1028 | 222 | # Where no period can be found, decline the invitation. |
paul@1028 | 223 | |
paul@1028 | 224 | else: |
paul@1153 | 225 | return "DECLINED", _("The recipient is unavailable in the requested period.") |
paul@1028 | 226 | |
paul@1028 | 227 | # Use the found period to set the start of the next window to search. |
paul@1028 | 228 | |
paul@1028 | 229 | last = periods[-1].get_end() |
paul@1028 | 230 | |
paul@1028 | 231 | # Replace the periods in the object. |
paul@1028 | 232 | |
paul@1028 | 233 | obj = handler.obj.copy() |
paul@1028 | 234 | changed = handler.obj.set_periods(periods) |
paul@1028 | 235 | |
paul@1028 | 236 | # Check one last time, reverting the change if not scheduled. |
paul@1028 | 237 | |
paul@1031 | 238 | scheduled = schedule_in_freebusy(handler, args, busy) |
paul@1153 | 239 | response, description = scheduled or ("DECLINED", None) |
paul@1028 | 240 | |
paul@1153 | 241 | if response == "DECLINED": |
paul@1028 | 242 | handler.set_object(obj) |
paul@1028 | 243 | |
paul@1153 | 244 | response = response == "ACCEPTED" and (changed and "COUNTER" or "ACCEPTED") or "DECLINED" |
paul@1153 | 245 | return standard_responses(handler, response) |
paul@1153 | 246 | |
paul@1028 | 247 | # Registry of scheduling functions. |
paul@1028 | 248 | |
paul@1028 | 249 | scheduling_functions = { |
paul@1199 | 250 | "schedule_in_freebusy" : [schedule_in_freebusy], |
paul@1199 | 251 | "schedule_corrected_in_freebusy" : [schedule_corrected_in_freebusy], |
paul@1199 | 252 | "schedule_next_available_in_freebusy" : [schedule_next_available_in_freebusy], |
paul@1028 | 253 | } |
paul@1028 | 254 | |
paul@1040 | 255 | # Registries of locking and unlocking functions. |
paul@1040 | 256 | |
paul@1040 | 257 | locking_functions = {} |
paul@1040 | 258 | unlocking_functions = {} |
paul@1040 | 259 | |
paul@1039 | 260 | # Registries of listener functions. |
paul@1039 | 261 | |
paul@1039 | 262 | confirmation_functions = {} |
paul@1039 | 263 | retraction_functions = {} |
paul@1039 | 264 | |
paul@1028 | 265 | # vim: tabstop=4 expandtab shiftwidth=4 |