paul@2 | 1 | #!/usr/bin/env python |
paul@2 | 2 | |
paul@146 | 3 | """ |
paul@146 | 4 | A simple filesystem-based store of calendar data. |
paul@146 | 5 | |
paul@1210 | 6 | Copyright (C) 2014, 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@146 | 7 | |
paul@146 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@146 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@146 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@146 | 11 | version. |
paul@146 | 12 | |
paul@146 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@146 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@146 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@146 | 16 | details. |
paul@146 | 17 | |
paul@146 | 18 | You should have received a copy of the GNU General Public License along with |
paul@146 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@146 | 20 | """ |
paul@146 | 21 | |
paul@1088 | 22 | from imiptools.stores.common import StoreBase, PublisherBase, JournalBase |
paul@1069 | 23 | |
paul@30 | 24 | from datetime import datetime |
paul@1210 | 25 | from imiptools.config import settings |
paul@1232 | 26 | from imiptools.data import Object, make_calendar, parse_object, to_stream |
paul@740 | 27 | from imiptools.dates import format_datetime, get_datetime, to_timezone |
paul@147 | 28 | from imiptools.filesys import fix_permissions, FileBase |
paul@1236 | 29 | |
paul@1236 | 30 | from imiptools.freebusy import FreeBusyCollection, \ |
paul@1234 | 31 | FreeBusyGroupCollection, \ |
paul@1236 | 32 | FreeBusyOffersCollection, \ |
paul@1236 | 33 | period_from_tuple, \ |
paul@1236 | 34 | period_to_tuple |
paul@1236 | 35 | |
paul@1236 | 36 | from imiptools.text import FileTable, FileTableDict, FileTableSingle, \ |
paul@1236 | 37 | have_table |
paul@1236 | 38 | |
paul@808 | 39 | from os.path import isdir, isfile, join |
paul@343 | 40 | from os import listdir, remove, rmdir |
paul@15 | 41 | |
paul@1230 | 42 | # Obtain defaults from the settings. |
paul@1230 | 43 | |
paul@1210 | 44 | STORE_DIR = settings["STORE_DIR"] |
paul@1210 | 45 | PUBLISH_DIR = settings["PUBLISH_DIR"] |
paul@1210 | 46 | JOURNAL_DIR = settings["JOURNAL_DIR"] |
paul@1210 | 47 | |
paul@1232 | 48 | # Store classes. |
paul@1232 | 49 | |
paul@1039 | 50 | class FileStoreBase(FileBase): |
paul@50 | 51 | |
paul@1236 | 52 | "A file store supporting user-specific locking." |
paul@147 | 53 | |
paul@303 | 54 | def acquire_lock(self, user, timeout=None): |
paul@303 | 55 | FileBase.acquire_lock(self, timeout, user) |
paul@303 | 56 | |
paul@303 | 57 | def release_lock(self, user): |
paul@303 | 58 | FileBase.release_lock(self, user) |
paul@303 | 59 | |
paul@1088 | 60 | class Store(FileStoreBase, StoreBase): |
paul@1039 | 61 | |
paul@1039 | 62 | "A file store of tabular free/busy data and objects." |
paul@1039 | 63 | |
paul@1039 | 64 | def __init__(self, store_dir=None): |
paul@1039 | 65 | FileBase.__init__(self, store_dir or STORE_DIR) |
paul@1039 | 66 | |
paul@648 | 67 | # Store object access. |
paul@648 | 68 | |
paul@329 | 69 | def _get_object(self, user, filename): |
paul@329 | 70 | |
paul@329 | 71 | """ |
paul@329 | 72 | Return the parsed object for the given 'user' having the given |
paul@329 | 73 | 'filename'. |
paul@329 | 74 | """ |
paul@329 | 75 | |
paul@329 | 76 | self.acquire_lock(user) |
paul@329 | 77 | try: |
paul@329 | 78 | f = open(filename, "rb") |
paul@329 | 79 | try: |
paul@1232 | 80 | return Object(parse_object(f, "utf-8")) |
paul@329 | 81 | finally: |
paul@329 | 82 | f.close() |
paul@329 | 83 | finally: |
paul@329 | 84 | self.release_lock(user) |
paul@329 | 85 | |
paul@329 | 86 | def _set_object(self, user, filename, node): |
paul@329 | 87 | |
paul@329 | 88 | """ |
paul@329 | 89 | Set an object for the given 'user' having the given 'filename', using |
paul@329 | 90 | 'node' to define the object. |
paul@329 | 91 | """ |
paul@329 | 92 | |
paul@329 | 93 | self.acquire_lock(user) |
paul@329 | 94 | try: |
paul@329 | 95 | f = open(filename, "wb") |
paul@329 | 96 | try: |
paul@329 | 97 | to_stream(f, node) |
paul@329 | 98 | finally: |
paul@329 | 99 | f.close() |
paul@329 | 100 | fix_permissions(filename) |
paul@329 | 101 | finally: |
paul@329 | 102 | self.release_lock(user) |
paul@329 | 103 | |
paul@329 | 104 | return True |
paul@329 | 105 | |
paul@329 | 106 | def _remove_object(self, filename): |
paul@329 | 107 | |
paul@329 | 108 | "Remove the object with the given 'filename'." |
paul@329 | 109 | |
paul@329 | 110 | try: |
paul@329 | 111 | remove(filename) |
paul@329 | 112 | except OSError: |
paul@329 | 113 | return False |
paul@329 | 114 | |
paul@329 | 115 | return True |
paul@329 | 116 | |
paul@343 | 117 | def _remove_collection(self, filename): |
paul@343 | 118 | |
paul@343 | 119 | "Remove the collection with the given 'filename'." |
paul@343 | 120 | |
paul@343 | 121 | try: |
paul@343 | 122 | rmdir(filename) |
paul@343 | 123 | except OSError: |
paul@343 | 124 | return False |
paul@343 | 125 | |
paul@343 | 126 | return True |
paul@343 | 127 | |
paul@670 | 128 | # User discovery. |
paul@670 | 129 | |
paul@670 | 130 | def get_users(self): |
paul@670 | 131 | |
paul@670 | 132 | "Return a list of users." |
paul@670 | 133 | |
paul@670 | 134 | return listdir(self.store_dir) |
paul@670 | 135 | |
paul@648 | 136 | # Event and event metadata access. |
paul@648 | 137 | |
paul@119 | 138 | def get_events(self, user): |
paul@119 | 139 | |
paul@119 | 140 | "Return a list of event identifiers." |
paul@119 | 141 | |
paul@138 | 142 | filename = self.get_object_in_store(user, "objects") |
paul@808 | 143 | if not filename or not isdir(filename): |
paul@1142 | 144 | return [] |
paul@119 | 145 | |
paul@119 | 146 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@119 | 147 | |
paul@1142 | 148 | def get_cancelled_events(self, user): |
paul@648 | 149 | |
paul@1142 | 150 | "Return a list of event identifiers for cancelled events." |
paul@648 | 151 | |
paul@1142 | 152 | filename = self.get_object_in_store(user, "cancellations", "objects") |
paul@1142 | 153 | if not filename or not isdir(filename): |
paul@1142 | 154 | return [] |
paul@648 | 155 | |
paul@1142 | 156 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@648 | 157 | |
paul@858 | 158 | def get_event(self, user, uid, recurrenceid=None, dirname=None): |
paul@343 | 159 | |
paul@343 | 160 | """ |
paul@343 | 161 | Get the event for the given 'user' with the given 'uid'. If |
paul@343 | 162 | the optional 'recurrenceid' is specified, a specific instance or |
paul@343 | 163 | occurrence of an event is returned. |
paul@343 | 164 | """ |
paul@343 | 165 | |
paul@858 | 166 | filename = self.get_event_filename(user, uid, recurrenceid, dirname) |
paul@808 | 167 | if not filename or not isfile(filename): |
paul@694 | 168 | return None |
paul@694 | 169 | |
paul@694 | 170 | return filename and self._get_object(user, filename) |
paul@694 | 171 | |
paul@1324 | 172 | def set_parent_event(self, user, uid, node): |
paul@50 | 173 | |
paul@50 | 174 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@50 | 175 | |
paul@138 | 176 | filename = self.get_object_in_store(user, "objects", uid) |
paul@50 | 177 | if not filename: |
paul@50 | 178 | return False |
paul@50 | 179 | |
paul@329 | 180 | return self._set_object(user, filename, node) |
paul@15 | 181 | |
paul@1068 | 182 | def remove_parent_event(self, user, uid): |
paul@1068 | 183 | |
paul@1068 | 184 | "Remove the parent event for 'user' having the given 'uid'." |
paul@369 | 185 | |
paul@234 | 186 | filename = self.get_object_in_store(user, "objects", uid) |
paul@234 | 187 | if not filename: |
paul@234 | 188 | return False |
paul@234 | 189 | |
paul@329 | 190 | return self._remove_object(filename) |
paul@234 | 191 | |
paul@334 | 192 | def get_recurrences(self, user, uid): |
paul@334 | 193 | |
paul@334 | 194 | """ |
paul@334 | 195 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 196 | indicated 'uid'. Both active and cancelled recurrences are returned. |
paul@694 | 197 | """ |
paul@694 | 198 | |
paul@694 | 199 | return self.get_active_recurrences(user, uid) + self.get_cancelled_recurrences(user, uid) |
paul@694 | 200 | |
paul@694 | 201 | def get_active_recurrences(self, user, uid): |
paul@694 | 202 | |
paul@694 | 203 | """ |
paul@694 | 204 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 205 | indicated 'uid'. Cancelled recurrences are not returned. |
paul@334 | 206 | """ |
paul@334 | 207 | |
paul@334 | 208 | filename = self.get_object_in_store(user, "recurrences", uid) |
paul@808 | 209 | if not filename or not isdir(filename): |
paul@347 | 210 | return [] |
paul@334 | 211 | |
paul@334 | 212 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@334 | 213 | |
paul@694 | 214 | def get_cancelled_recurrences(self, user, uid): |
paul@694 | 215 | |
paul@694 | 216 | """ |
paul@694 | 217 | Get additional event instances for an event of the given 'user' with the |
paul@694 | 218 | indicated 'uid'. Only cancelled recurrences are returned. |
paul@694 | 219 | """ |
paul@694 | 220 | |
paul@782 | 221 | filename = self.get_object_in_store(user, "cancellations", "recurrences", uid) |
paul@808 | 222 | if not filename or not isdir(filename): |
paul@694 | 223 | return [] |
paul@694 | 224 | |
paul@694 | 225 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@694 | 226 | |
paul@334 | 227 | def get_recurrence(self, user, uid, recurrenceid): |
paul@334 | 228 | |
paul@334 | 229 | """ |
paul@334 | 230 | For the event of the given 'user' with the given 'uid', return the |
paul@334 | 231 | specific recurrence indicated by the 'recurrenceid'. |
paul@334 | 232 | """ |
paul@334 | 233 | |
paul@694 | 234 | filename = self.get_recurrence_filename(user, uid, recurrenceid) |
paul@808 | 235 | if not filename or not isfile(filename): |
paul@334 | 236 | return None |
paul@334 | 237 | |
paul@694 | 238 | return filename and self._get_object(user, filename) |
paul@334 | 239 | |
paul@334 | 240 | def set_recurrence(self, user, uid, recurrenceid, node): |
paul@334 | 241 | |
paul@334 | 242 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@334 | 243 | |
paul@334 | 244 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 245 | if not filename: |
paul@334 | 246 | return False |
paul@334 | 247 | |
paul@334 | 248 | return self._set_object(user, filename, node) |
paul@334 | 249 | |
paul@334 | 250 | def remove_recurrence(self, user, uid, recurrenceid): |
paul@334 | 251 | |
paul@378 | 252 | """ |
paul@378 | 253 | Remove a special recurrence from an event stored by 'user' having the |
paul@378 | 254 | given 'uid' and 'recurrenceid'. |
paul@378 | 255 | """ |
paul@334 | 256 | |
paul@378 | 257 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 258 | if not filename: |
paul@334 | 259 | return False |
paul@334 | 260 | |
paul@334 | 261 | return self._remove_object(filename) |
paul@334 | 262 | |
paul@1068 | 263 | def remove_recurrence_collection(self, user, uid): |
paul@1068 | 264 | |
paul@1068 | 265 | """ |
paul@1068 | 266 | Remove the collection of recurrences stored by 'user' having the given |
paul@1068 | 267 | 'uid'. |
paul@1068 | 268 | """ |
paul@1068 | 269 | |
paul@378 | 270 | recurrences = self.get_object_in_store(user, "recurrences", uid) |
paul@378 | 271 | if recurrences: |
paul@378 | 272 | return self._remove_collection(recurrences) |
paul@378 | 273 | |
paul@378 | 274 | return True |
paul@378 | 275 | |
paul@1142 | 276 | # Event filename computation. |
paul@1142 | 277 | |
paul@1142 | 278 | def get_event_filename(self, user, uid, recurrenceid=None, dirname=None, username=None): |
paul@1142 | 279 | |
paul@1142 | 280 | """ |
paul@1142 | 281 | Get the filename providing the event for the given 'user' with the given |
paul@1142 | 282 | 'uid'. If the optional 'recurrenceid' is specified, a specific instance |
paul@1142 | 283 | or occurrence of an event is returned. |
paul@1142 | 284 | |
paul@1142 | 285 | Where 'dirname' is specified, the given directory name is used as the |
paul@1142 | 286 | base of the location within which any filename will reside. |
paul@1142 | 287 | """ |
paul@1142 | 288 | |
paul@1142 | 289 | if recurrenceid: |
paul@1142 | 290 | return self.get_recurrence_filename(user, uid, recurrenceid, dirname, username) |
paul@1142 | 291 | else: |
paul@1324 | 292 | return self.get_parent_event_filename(user, uid, dirname, username) |
paul@1142 | 293 | |
paul@1142 | 294 | def get_recurrence_filename(self, user, uid, recurrenceid, dirname=None, username=None): |
paul@1142 | 295 | |
paul@1142 | 296 | """ |
paul@1142 | 297 | For the event of the given 'user' with the given 'uid', return the |
paul@1142 | 298 | filename providing the recurrence with the given 'recurrenceid'. |
paul@1142 | 299 | |
paul@1142 | 300 | Where 'dirname' is specified, the given directory name is used as the |
paul@1142 | 301 | base of the location within which any filename will reside. |
paul@1142 | 302 | |
paul@1142 | 303 | Where 'username' is specified, the event details will reside in a file |
paul@1142 | 304 | bearing that name within a directory having 'uid' as its name. |
paul@1142 | 305 | """ |
paul@1142 | 306 | |
paul@1142 | 307 | return self.get_object_in_store(user, dirname, "recurrences", uid, recurrenceid, username) |
paul@1142 | 308 | |
paul@1324 | 309 | def get_parent_event_filename(self, user, uid, dirname=None, username=None): |
paul@1142 | 310 | |
paul@1142 | 311 | """ |
paul@1142 | 312 | Get the filename providing the event for the given 'user' with the given |
paul@1142 | 313 | 'uid'. |
paul@1142 | 314 | |
paul@1142 | 315 | Where 'dirname' is specified, the given directory name is used as the |
paul@1142 | 316 | base of the location within which any filename will reside. |
paul@1142 | 317 | |
paul@1142 | 318 | Where 'username' is specified, the event details will reside in a file |
paul@1142 | 319 | bearing that name within a directory having 'uid' as its name. |
paul@1142 | 320 | """ |
paul@1142 | 321 | |
paul@1142 | 322 | return self.get_object_in_store(user, dirname, "objects", uid, username) |
paul@1142 | 323 | |
paul@652 | 324 | # Free/busy period providers, upon extension of the free/busy records. |
paul@652 | 325 | |
paul@672 | 326 | def _get_freebusy_providers(self, user): |
paul@672 | 327 | |
paul@672 | 328 | """ |
paul@672 | 329 | Return the free/busy providers for the given 'user'. |
paul@672 | 330 | |
paul@672 | 331 | This function returns any stored datetime and a list of providers as a |
paul@672 | 332 | 2-tuple. Each provider is itself a (uid, recurrenceid) tuple. |
paul@672 | 333 | """ |
paul@672 | 334 | |
paul@672 | 335 | filename = self.get_object_in_store(user, "freebusy-providers") |
paul@1236 | 336 | if not filename: |
paul@672 | 337 | return None |
paul@672 | 338 | |
paul@672 | 339 | # Attempt to read providers, with a declaration of the datetime |
paul@672 | 340 | # from which such providers are considered as still being active. |
paul@672 | 341 | |
paul@1236 | 342 | t = self._get_freebusy_providers_table(filename) |
paul@1236 | 343 | header = t.get_header_values() |
paul@1236 | 344 | if not header: |
paul@672 | 345 | return None |
paul@672 | 346 | |
paul@1236 | 347 | return header[0], t |
paul@1236 | 348 | |
paul@1236 | 349 | def _get_freebusy_providers_table(self, filename): |
paul@1236 | 350 | |
paul@1236 | 351 | "Return a file-based table for storing providers in 'filename'." |
paul@672 | 352 | |
paul@1236 | 353 | return FileTable(filename, |
paul@1236 | 354 | in_defaults=[(1, None)], |
paul@1236 | 355 | out_defaults=[(1, "")], |
paul@1236 | 356 | headers=1) |
paul@672 | 357 | |
paul@1236 | 358 | def _set_freebusy_providers(self, user, dt_string, providers): |
paul@1236 | 359 | |
paul@1236 | 360 | "Set the given provider timestamp 'dt_string' and 'providers'." |
paul@672 | 361 | |
paul@652 | 362 | filename = self.get_object_in_store(user, "freebusy-providers") |
paul@672 | 363 | if not filename: |
paul@672 | 364 | return False |
paul@652 | 365 | |
paul@1236 | 366 | self.acquire_lock(user) |
paul@1236 | 367 | try: |
paul@1236 | 368 | if not have_table(providers, filename): |
paul@1236 | 369 | pr = self._get_freebusy_providers_table(filename) |
paul@1236 | 370 | pr.replaceall(providers) |
paul@1236 | 371 | providers = pr |
paul@1236 | 372 | providers.set_header_values([dt_string]) |
paul@1236 | 373 | providers.close() |
paul@1236 | 374 | finally: |
paul@1236 | 375 | self.release_lock(user) |
paul@672 | 376 | return True |
paul@652 | 377 | |
paul@648 | 378 | # Free/busy period access. |
paul@648 | 379 | |
paul@1236 | 380 | def get_freebusy(self, user, name=None, mutable=False): |
paul@15 | 381 | |
paul@15 | 382 | "Get free/busy details for the given 'user'." |
paul@15 | 383 | |
paul@702 | 384 | filename = self.get_object_in_store(user, name or "freebusy") |
paul@1062 | 385 | |
paul@1236 | 386 | if not filename: |
paul@1236 | 387 | return [] |
paul@702 | 388 | |
paul@1236 | 389 | return self._get_freebusy(filename, mutable, FreeBusyCollection) |
paul@1071 | 390 | |
paul@1236 | 391 | def get_freebusy_for_other(self, user, other, mutable=False, collection=None): |
paul@112 | 392 | |
paul@112 | 393 | "For the given 'user', get free/busy details for the 'other' user." |
paul@112 | 394 | |
paul@112 | 395 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@1062 | 396 | |
paul@1236 | 397 | if not filename: |
paul@1236 | 398 | return [] |
paul@1236 | 399 | |
paul@1236 | 400 | return self._get_freebusy(filename, mutable, collection or FreeBusyCollection) |
paul@1236 | 401 | |
paul@1236 | 402 | def _get_freebusy(self, filename, mutable=False, collection=None): |
paul@1236 | 403 | |
paul@1236 | 404 | """ |
paul@1236 | 405 | Return a free/busy collection for 'filename' with the given 'mutable' |
paul@1236 | 406 | condition, employing the specified 'collection' class. |
paul@1236 | 407 | """ |
paul@702 | 408 | |
paul@1192 | 409 | collection = collection or FreeBusyCollection |
paul@1236 | 410 | |
paul@1236 | 411 | periods = FileTable(filename, mutable=mutable, |
paul@1236 | 412 | in_converter=period_from_tuple(collection.period_class), |
paul@1236 | 413 | out_converter=period_to_tuple) |
paul@1236 | 414 | |
paul@1236 | 415 | return collection(periods, mutable=mutable) |
paul@1071 | 416 | |
paul@1064 | 417 | def set_freebusy(self, user, freebusy, name=None): |
paul@15 | 418 | |
paul@15 | 419 | "For the given 'user', set 'freebusy' details." |
paul@15 | 420 | |
paul@702 | 421 | filename = self.get_object_in_store(user, name or "freebusy") |
paul@15 | 422 | if not filename: |
paul@15 | 423 | return False |
paul@15 | 424 | |
paul@1236 | 425 | return self._set_freebusy(user, freebusy, filename) |
paul@15 | 426 | |
paul@1236 | 427 | def set_freebusy_for_other(self, user, freebusy, other, collection=None): |
paul@110 | 428 | |
paul@110 | 429 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@110 | 430 | |
paul@110 | 431 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@110 | 432 | if not filename: |
paul@110 | 433 | return False |
paul@110 | 434 | |
paul@1236 | 435 | return self._set_freebusy(user, freebusy, filename, collection) |
paul@1236 | 436 | |
paul@1236 | 437 | def _set_freebusy(self, user, freebusy, filename, collection=None): |
paul@1236 | 438 | |
paul@1236 | 439 | "For the given 'user', set 'freebusy' details for the given 'filename'." |
paul@1236 | 440 | |
paul@1236 | 441 | # Copy to the specified table if different from that given. |
paul@1236 | 442 | |
paul@1236 | 443 | self.acquire_lock(user) |
paul@1236 | 444 | try: |
paul@1236 | 445 | if not have_table(freebusy, filename): |
paul@1236 | 446 | fbc = self._get_freebusy(filename, True, collection) |
paul@1236 | 447 | fbc += freebusy |
paul@1236 | 448 | freebusy = fbc |
paul@1236 | 449 | freebusy.close() |
paul@1236 | 450 | finally: |
paul@1236 | 451 | self.release_lock(user) |
paul@1236 | 452 | |
paul@112 | 453 | return True |
paul@112 | 454 | |
paul@1142 | 455 | def get_freebusy_others(self, user): |
paul@1142 | 456 | |
paul@1142 | 457 | """ |
paul@1142 | 458 | For the given 'user', return a list of other users for whom free/busy |
paul@1142 | 459 | information is retained. |
paul@1142 | 460 | """ |
paul@1142 | 461 | |
paul@1142 | 462 | filename = self.get_object_in_store(user, "freebusy-other") |
paul@1142 | 463 | |
paul@1142 | 464 | if not filename or not isdir(filename): |
paul@1142 | 465 | return [] |
paul@1142 | 466 | |
paul@1142 | 467 | return listdir(filename) |
paul@1142 | 468 | |
paul@710 | 469 | # Tentative free/busy periods related to countering. |
paul@710 | 470 | |
paul@1071 | 471 | def get_freebusy_offers(self, user, mutable=False): |
paul@710 | 472 | |
paul@710 | 473 | "Get free/busy offers for the given 'user'." |
paul@710 | 474 | |
paul@1236 | 475 | filename = self.get_object_in_store(user, "freebusy-offers") |
paul@1236 | 476 | |
paul@1236 | 477 | if not filename: |
paul@1236 | 478 | return [] |
paul@1236 | 479 | |
paul@710 | 480 | expired = [] |
paul@741 | 481 | now = to_timezone(datetime.utcnow(), "UTC") |
paul@710 | 482 | |
paul@710 | 483 | # Expire old offers and save the collection if modified. |
paul@710 | 484 | |
paul@730 | 485 | self.acquire_lock(user) |
paul@710 | 486 | try: |
paul@1236 | 487 | offers = self._get_freebusy(filename, True, FreeBusyOffersCollection) |
paul@1236 | 488 | for fb in offers: |
paul@710 | 489 | if fb.expires and get_datetime(fb.expires) <= now: |
paul@1236 | 490 | offers.remove(fb) |
paul@710 | 491 | if expired: |
paul@1236 | 492 | offers.close() |
paul@710 | 493 | finally: |
paul@730 | 494 | self.release_lock(user) |
paul@710 | 495 | |
paul@1236 | 496 | offers.mutable = mutable |
paul@1236 | 497 | return offers |
paul@710 | 498 | |
paul@747 | 499 | # Requests and counter-proposals. |
paul@648 | 500 | |
paul@1236 | 501 | def get_requests(self, user, queue="requests"): |
paul@66 | 502 | |
paul@142 | 503 | "Get requests for the given 'user' from the given 'queue'." |
paul@66 | 504 | |
paul@142 | 505 | filename = self.get_object_in_store(user, queue) |
paul@1236 | 506 | if not filename: |
paul@1142 | 507 | return [] |
paul@66 | 508 | |
paul@1236 | 509 | return FileTable(filename, |
paul@1236 | 510 | in_defaults=[(1, None), (2, None)], |
paul@1236 | 511 | out_defaults=[(1, ""), (2, "")]) |
paul@66 | 512 | |
paul@1236 | 513 | def set_request(self, user, uid, recurrenceid=None, type=None): |
paul@142 | 514 | |
paul@1236 | 515 | """ |
paul@1236 | 516 | For the given 'user', set the queued 'uid' and 'recurrenceid', |
paul@1236 | 517 | indicating a request, along with any given 'type'. |
paul@1236 | 518 | """ |
paul@142 | 519 | |
paul@1236 | 520 | requests = self.get_requests(user) |
paul@1236 | 521 | return self.set_requests(user, [(uid, recurrenceid, type)]) |
paul@142 | 522 | |
paul@1236 | 523 | def set_requests(self, user, requests, queue="requests"): |
paul@66 | 524 | |
paul@142 | 525 | """ |
paul@142 | 526 | For the given 'user', set the list of queued 'requests' in the given |
paul@142 | 527 | 'queue'. |
paul@142 | 528 | """ |
paul@142 | 529 | |
paul@142 | 530 | filename = self.get_object_in_store(user, queue) |
paul@66 | 531 | if not filename: |
paul@66 | 532 | return False |
paul@66 | 533 | |
paul@1236 | 534 | # Copy to the specified table if different from that given. |
paul@55 | 535 | |
paul@303 | 536 | self.acquire_lock(user) |
paul@55 | 537 | try: |
paul@1236 | 538 | if not have_table(requests, filename): |
paul@1236 | 539 | req = self.get_requests(user, queue) |
paul@1236 | 540 | req.replaceall(requests) |
paul@1236 | 541 | requests = req |
paul@1236 | 542 | requests.close() |
paul@55 | 543 | finally: |
paul@303 | 544 | self.release_lock(user) |
paul@55 | 545 | |
paul@55 | 546 | return True |
paul@55 | 547 | |
paul@760 | 548 | def get_counters(self, user, uid, recurrenceid=None): |
paul@754 | 549 | |
paul@754 | 550 | """ |
paul@766 | 551 | For the given 'user', return a list of users from whom counter-proposals |
paul@766 | 552 | have been received for the given 'uid' and optional 'recurrenceid'. |
paul@754 | 553 | """ |
paul@754 | 554 | |
paul@754 | 555 | filename = self.get_event_filename(user, uid, recurrenceid, "counters") |
paul@808 | 556 | if not filename or not isdir(filename): |
paul@1142 | 557 | return [] |
paul@754 | 558 | |
paul@766 | 559 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@760 | 560 | |
paul@1305 | 561 | def get_counter_recurrences(self, user, uid): |
paul@1305 | 562 | |
paul@1305 | 563 | """ |
paul@1305 | 564 | For the given 'user', return a list of recurrence identifiers describing |
paul@1305 | 565 | counter-proposals for the parent event with the given 'uid'. |
paul@1305 | 566 | """ |
paul@1305 | 567 | |
paul@1305 | 568 | filename = self.get_object_in_store(user, "counters", "recurrences", uid) |
paul@1305 | 569 | if not filename or not isdir(filename): |
paul@1305 | 570 | return [] |
paul@1305 | 571 | |
paul@1305 | 572 | return listdir(filename) |
paul@1305 | 573 | |
paul@760 | 574 | def get_counter(self, user, other, uid, recurrenceid=None): |
paul@105 | 575 | |
paul@343 | 576 | """ |
paul@760 | 577 | For the given 'user', return the counter-proposal from 'other' for the |
paul@760 | 578 | given 'uid' and optional 'recurrenceid'. |
paul@760 | 579 | """ |
paul@760 | 580 | |
paul@760 | 581 | filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@1090 | 582 | if not filename or not isfile(filename): |
paul@1142 | 583 | return None |
paul@760 | 584 | |
paul@760 | 585 | return self._get_object(user, filename) |
paul@760 | 586 | |
paul@760 | 587 | def set_counter(self, user, other, node, uid, recurrenceid=None): |
paul@760 | 588 | |
paul@760 | 589 | """ |
paul@760 | 590 | For the given 'user', store a counter-proposal received from 'other' the |
paul@760 | 591 | given 'node' representing that proposal for the given 'uid' and |
paul@760 | 592 | 'recurrenceid'. |
paul@760 | 593 | """ |
paul@760 | 594 | |
paul@760 | 595 | filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@760 | 596 | if not filename: |
paul@760 | 597 | return False |
paul@760 | 598 | |
paul@760 | 599 | return self._set_object(user, filename, node) |
paul@760 | 600 | |
paul@1306 | 601 | def remove_counters(self, user, uid, recurrenceid=None, attendee=None): |
paul@760 | 602 | |
paul@760 | 603 | """ |
paul@760 | 604 | For the given 'user', remove all counter-proposals associated with the |
paul@1306 | 605 | given 'uid' and 'recurrenceid'. If a parent event is specified, all |
paul@1306 | 606 | recurrence counter-proposals will be removed. If 'attendee' is |
paul@1306 | 607 | specified, only objects provided by this attendee will be removed. |
paul@1306 | 608 | """ |
paul@1306 | 609 | |
paul@1306 | 610 | self._remove_counters(user, uid, recurrenceid, attendee) |
paul@1306 | 611 | |
paul@1306 | 612 | if not recurrenceid: |
paul@1306 | 613 | for recurrenceid in self.get_counter_recurrences(user, uid): |
paul@1306 | 614 | self._remove_counters(user, uid, recurrenceid, attendee) |
paul@1306 | 615 | |
paul@1306 | 616 | def _remove_counters(self, user, uid, recurrenceid=None, attendee=None): |
paul@1306 | 617 | |
paul@1306 | 618 | """ |
paul@1306 | 619 | For the given 'user', remove all counter-proposals associated with the |
paul@1306 | 620 | given 'uid' and 'recurrenceid'. If 'attendee' is specified, only objects |
paul@1306 | 621 | provided by this attendee will be removed. |
paul@343 | 622 | """ |
paul@105 | 623 | |
paul@747 | 624 | filename = self.get_event_filename(user, uid, recurrenceid, "counters") |
paul@808 | 625 | if not filename or not isdir(filename): |
paul@747 | 626 | return False |
paul@747 | 627 | |
paul@760 | 628 | removed = False |
paul@747 | 629 | |
paul@760 | 630 | for other in listdir(filename): |
paul@1306 | 631 | if not attendee or other == attendee: |
paul@1306 | 632 | counter_filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@1306 | 633 | removed = removed or self._remove_object(counter_filename) |
paul@1306 | 634 | |
paul@1306 | 635 | if not listdir(filename): |
paul@1306 | 636 | self._remove_collection(filename) |
paul@760 | 637 | |
paul@760 | 638 | return removed |
paul@760 | 639 | |
paul@760 | 640 | def remove_counter(self, user, other, uid, recurrenceid=None): |
paul@105 | 641 | |
paul@747 | 642 | """ |
paul@760 | 643 | For the given 'user', remove any counter-proposal from 'other' |
paul@760 | 644 | associated with the given 'uid' and 'recurrenceid'. |
paul@747 | 645 | """ |
paul@747 | 646 | |
paul@760 | 647 | filename = self.get_event_filename(user, uid, recurrenceid, "counters", other) |
paul@808 | 648 | if not filename or not isfile(filename): |
paul@105 | 649 | return False |
paul@747 | 650 | |
paul@747 | 651 | return self._remove_object(filename) |
paul@747 | 652 | |
paul@747 | 653 | # Event cancellation. |
paul@105 | 654 | |
paul@343 | 655 | def cancel_event(self, user, uid, recurrenceid=None): |
paul@142 | 656 | |
paul@343 | 657 | """ |
paul@694 | 658 | Cancel an event for 'user' having the given 'uid'. If the optional |
paul@694 | 659 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@694 | 660 | event is cancelled. |
paul@343 | 661 | """ |
paul@142 | 662 | |
paul@694 | 663 | filename = self.get_event_filename(user, uid, recurrenceid) |
paul@694 | 664 | cancelled_filename = self.get_event_filename(user, uid, recurrenceid, "cancellations") |
paul@142 | 665 | |
paul@808 | 666 | if filename and cancelled_filename and isfile(filename): |
paul@694 | 667 | return self.move_object(filename, cancelled_filename) |
paul@142 | 668 | |
paul@142 | 669 | return False |
paul@142 | 670 | |
paul@863 | 671 | def uncancel_event(self, user, uid, recurrenceid=None): |
paul@863 | 672 | |
paul@863 | 673 | """ |
paul@863 | 674 | Uncancel an event for 'user' having the given 'uid'. If the optional |
paul@863 | 675 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@863 | 676 | event is uncancelled. |
paul@863 | 677 | """ |
paul@863 | 678 | |
paul@863 | 679 | filename = self.get_event_filename(user, uid, recurrenceid) |
paul@863 | 680 | cancelled_filename = self.get_event_filename(user, uid, recurrenceid, "cancellations") |
paul@863 | 681 | |
paul@863 | 682 | if filename and cancelled_filename and isfile(cancelled_filename): |
paul@863 | 683 | return self.move_object(cancelled_filename, filename) |
paul@863 | 684 | |
paul@863 | 685 | return False |
paul@863 | 686 | |
paul@796 | 687 | def remove_cancellation(self, user, uid, recurrenceid=None): |
paul@796 | 688 | |
paul@796 | 689 | """ |
paul@796 | 690 | Remove a cancellation for 'user' for the event having the given 'uid'. |
paul@796 | 691 | If the optional 'recurrenceid' is specified, a specific instance or |
paul@796 | 692 | occurrence of an event is affected. |
paul@796 | 693 | """ |
paul@796 | 694 | |
paul@796 | 695 | # Remove any parent event cancellation or a specific recurrence |
paul@796 | 696 | # cancellation if indicated. |
paul@796 | 697 | |
paul@796 | 698 | filename = self.get_event_filename(user, uid, recurrenceid, "cancellations") |
paul@796 | 699 | |
paul@808 | 700 | if filename and isfile(filename): |
paul@796 | 701 | return self._remove_object(filename) |
paul@796 | 702 | |
paul@796 | 703 | return False |
paul@796 | 704 | |
paul@1088 | 705 | class Publisher(FileBase, PublisherBase): |
paul@30 | 706 | |
paul@30 | 707 | "A publisher of objects." |
paul@30 | 708 | |
paul@597 | 709 | def __init__(self, store_dir=None): |
paul@597 | 710 | FileBase.__init__(self, store_dir or PUBLISH_DIR) |
paul@30 | 711 | |
paul@30 | 712 | def set_freebusy(self, user, freebusy): |
paul@30 | 713 | |
paul@30 | 714 | "For the given 'user', set 'freebusy' details." |
paul@30 | 715 | |
paul@52 | 716 | filename = self.get_object_in_store(user, "freebusy") |
paul@30 | 717 | if not filename: |
paul@30 | 718 | return False |
paul@30 | 719 | |
paul@30 | 720 | record = [] |
paul@30 | 721 | rwrite = record.append |
paul@30 | 722 | |
paul@30 | 723 | rwrite(("ORGANIZER", {}, user)) |
paul@30 | 724 | rwrite(("UID", {}, user)) |
paul@30 | 725 | rwrite(("DTSTAMP", {}, datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"))) |
paul@30 | 726 | |
paul@458 | 727 | for fb in freebusy: |
paul@458 | 728 | if not fb.transp or fb.transp == "OPAQUE": |
paul@529 | 729 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@563 | 730 | map(format_datetime, [fb.get_start_point(), fb.get_end_point()])))) |
paul@30 | 731 | |
paul@395 | 732 | f = open(filename, "wb") |
paul@30 | 733 | try: |
paul@30 | 734 | to_stream(f, make_calendar([("VFREEBUSY", {}, record)], "PUBLISH")) |
paul@30 | 735 | finally: |
paul@30 | 736 | f.close() |
paul@103 | 737 | fix_permissions(filename) |
paul@30 | 738 | |
paul@30 | 739 | return True |
paul@30 | 740 | |
paul@1192 | 741 | class Journal(Store, JournalBase): |
paul@1039 | 742 | |
paul@1039 | 743 | "A journal system to support quotas." |
paul@1039 | 744 | |
paul@1049 | 745 | # Quota and user identity/group discovery. |
paul@1049 | 746 | |
paul@1194 | 747 | get_quotas = Store.get_users |
paul@1194 | 748 | get_quota_users = Store.get_freebusy_others |
paul@1176 | 749 | |
paul@1176 | 750 | # Delegate information for the quota. |
paul@1176 | 751 | |
paul@1176 | 752 | def get_delegates(self, quota): |
paul@1176 | 753 | |
paul@1176 | 754 | "Return a list of delegates for 'quota'." |
paul@1176 | 755 | |
paul@1176 | 756 | filename = self.get_object_in_store(quota, "delegates") |
paul@1236 | 757 | if not filename: |
paul@1176 | 758 | return [] |
paul@1176 | 759 | |
paul@1236 | 760 | return FileTableSingle(filename) |
paul@1176 | 761 | |
paul@1176 | 762 | def set_delegates(self, quota, delegates): |
paul@1176 | 763 | |
paul@1176 | 764 | "For the given 'quota', set the list of 'delegates'." |
paul@1176 | 765 | |
paul@1176 | 766 | filename = self.get_object_in_store(quota, "delegates") |
paul@1176 | 767 | if not filename: |
paul@1176 | 768 | return False |
paul@1176 | 769 | |
paul@1236 | 770 | self.acquire_lock(quota) |
paul@1236 | 771 | try: |
paul@1236 | 772 | if not have_table(delegates, filename): |
paul@1236 | 773 | de = self.get_delegates(quota) |
paul@1236 | 774 | de.replaceall(delegates) |
paul@1236 | 775 | delegates = de |
paul@1236 | 776 | delegates.close() |
paul@1236 | 777 | finally: |
paul@1236 | 778 | self.release_lock(quota) |
paul@1236 | 779 | |
paul@1176 | 780 | return True |
paul@1049 | 781 | |
paul@1039 | 782 | # Groups of users sharing quotas. |
paul@1039 | 783 | |
paul@1039 | 784 | def get_groups(self, quota): |
paul@1039 | 785 | |
paul@1039 | 786 | "Return the identity mappings for the given 'quota' as a dictionary." |
paul@1039 | 787 | |
paul@1039 | 788 | filename = self.get_object_in_store(quota, "groups") |
paul@1236 | 789 | if not filename: |
paul@1039 | 790 | return {} |
paul@1039 | 791 | |
paul@1236 | 792 | return FileTableDict(filename, tab_separated=False) |
paul@1039 | 793 | |
paul@1176 | 794 | def set_groups(self, quota, groups): |
paul@1142 | 795 | |
paul@1176 | 796 | "For the given 'quota', set 'groups' mapping users to groups." |
paul@1142 | 797 | |
paul@1142 | 798 | filename = self.get_object_in_store(quota, "groups") |
paul@1142 | 799 | if not filename: |
paul@1142 | 800 | return False |
paul@1142 | 801 | |
paul@1236 | 802 | self.acquire_lock(quota) |
paul@1236 | 803 | try: |
paul@1236 | 804 | if not have_table(groups, filename): |
paul@1236 | 805 | gr = self.get_groups(quota) |
paul@1236 | 806 | gr.updateall(groups) |
paul@1236 | 807 | groups = gr |
paul@1236 | 808 | groups.close() |
paul@1236 | 809 | finally: |
paul@1236 | 810 | self.release_lock(quota) |
paul@1236 | 811 | |
paul@1142 | 812 | return True |
paul@1142 | 813 | |
paul@1039 | 814 | def get_limits(self, quota): |
paul@1039 | 815 | |
paul@1039 | 816 | """ |
paul@1039 | 817 | Return the limits for the 'quota' as a dictionary mapping identities or |
paul@1039 | 818 | groups to durations. |
paul@1039 | 819 | """ |
paul@1039 | 820 | |
paul@1039 | 821 | filename = self.get_object_in_store(quota, "limits") |
paul@1236 | 822 | if not filename: |
paul@1142 | 823 | return {} |
paul@1039 | 824 | |
paul@1236 | 825 | return FileTableDict(filename, tab_separated=False) |
paul@1039 | 826 | |
paul@1176 | 827 | def set_limits(self, quota, limits): |
paul@1089 | 828 | |
paul@1089 | 829 | """ |
paul@1176 | 830 | For the given 'quota', set the given 'limits' on resource usage mapping |
paul@1176 | 831 | groups to limits. |
paul@1089 | 832 | """ |
paul@1089 | 833 | |
paul@1089 | 834 | filename = self.get_object_in_store(quota, "limits") |
paul@1089 | 835 | if not filename: |
paul@1142 | 836 | return False |
paul@1089 | 837 | |
paul@1236 | 838 | self.acquire_lock(quota) |
paul@1236 | 839 | try: |
paul@1236 | 840 | if not have_table(limits, filename): |
paul@1236 | 841 | li = self.get_limits(quota) |
paul@1236 | 842 | li.updateall(limits) |
paul@1236 | 843 | limits = li |
paul@1236 | 844 | limits.close() |
paul@1236 | 845 | finally: |
paul@1236 | 846 | self.release_lock(quota) |
paul@1236 | 847 | |
paul@1089 | 848 | return True |
paul@1089 | 849 | |
paul@1039 | 850 | # Journal entry methods. |
paul@1039 | 851 | |
paul@1071 | 852 | def get_entries(self, quota, group, mutable=False): |
paul@1039 | 853 | |
paul@1039 | 854 | """ |
paul@1039 | 855 | Return a list of journal entries for the given 'quota' for the indicated |
paul@1039 | 856 | 'group'. |
paul@1039 | 857 | """ |
paul@1039 | 858 | |
paul@1193 | 859 | return self.get_freebusy_for_other(quota, group, mutable) |
paul@1039 | 860 | |
paul@1039 | 861 | def set_entries(self, quota, group, entries): |
paul@1039 | 862 | |
paul@1039 | 863 | """ |
paul@1039 | 864 | For the given 'quota' and indicated 'group', set the list of journal |
paul@1039 | 865 | 'entries'. |
paul@1039 | 866 | """ |
paul@1039 | 867 | |
paul@1192 | 868 | return self.set_freebusy_for_other(quota, entries, group) |
paul@1039 | 869 | |
paul@1193 | 870 | # Compatibility methods. |
paul@1193 | 871 | |
paul@1193 | 872 | def get_freebusy_for_other(self, user, other, mutable=False): |
paul@1236 | 873 | return Store.get_freebusy_for_other(self, user, other, mutable, collection=FreeBusyGroupCollection) |
paul@1236 | 874 | |
paul@1236 | 875 | def set_freebusy_for_other(self, user, entries, other): |
paul@1236 | 876 | Store.set_freebusy_for_other(self, user, entries, other, collection=FreeBusyGroupCollection) |
paul@1193 | 877 | |
paul@2 | 878 | # vim: tabstop=4 expandtab shiftwidth=4 |