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@146 | 6 | Copyright (C) 2014, 2015 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@30 | 22 | from datetime import datetime |
paul@68 | 23 | from imiptools.config import STORE_DIR, PUBLISH_DIR |
paul@301 | 24 | from imiptools.data import make_calendar, parse_object, to_stream |
paul@529 | 25 | from imiptools.dates import format_datetime |
paul@147 | 26 | from imiptools.filesys import fix_permissions, FileBase |
paul@458 | 27 | from imiptools.period import FreeBusyPeriod |
paul@147 | 28 | from os.path import exists, isfile, join |
paul@343 | 29 | from os import listdir, remove, rmdir |
paul@303 | 30 | from time import sleep |
paul@395 | 31 | import codecs |
paul@15 | 32 | |
paul@50 | 33 | class FileStore(FileBase): |
paul@50 | 34 | |
paul@50 | 35 | "A file store of tabular free/busy data and objects." |
paul@50 | 36 | |
paul@597 | 37 | def __init__(self, store_dir=None): |
paul@597 | 38 | FileBase.__init__(self, store_dir or STORE_DIR) |
paul@147 | 39 | |
paul@303 | 40 | def acquire_lock(self, user, timeout=None): |
paul@303 | 41 | FileBase.acquire_lock(self, timeout, user) |
paul@303 | 42 | |
paul@303 | 43 | def release_lock(self, user): |
paul@303 | 44 | FileBase.release_lock(self, user) |
paul@303 | 45 | |
paul@648 | 46 | # Utility methods. |
paul@648 | 47 | |
paul@343 | 48 | def _set_defaults(self, t, empty_defaults): |
paul@343 | 49 | for i, default in empty_defaults: |
paul@343 | 50 | if i >= len(t): |
paul@343 | 51 | t += [None] * (i - len(t) + 1) |
paul@343 | 52 | if not t[i]: |
paul@343 | 53 | t[i] = default |
paul@343 | 54 | return t |
paul@343 | 55 | |
paul@343 | 56 | def _get_table(self, user, filename, empty_defaults=None): |
paul@343 | 57 | |
paul@343 | 58 | """ |
paul@343 | 59 | From the file for the given 'user' having the given 'filename', return |
paul@343 | 60 | a list of tuples representing the file's contents. |
paul@343 | 61 | |
paul@343 | 62 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@343 | 63 | default value where a column either does not exist or provides an empty |
paul@343 | 64 | value. |
paul@343 | 65 | """ |
paul@343 | 66 | |
paul@343 | 67 | self.acquire_lock(user) |
paul@343 | 68 | try: |
paul@395 | 69 | f = codecs.open(filename, "rb", encoding="utf-8") |
paul@343 | 70 | try: |
paul@343 | 71 | l = [] |
paul@343 | 72 | for line in f.readlines(): |
paul@343 | 73 | t = line.strip().split("\t") |
paul@343 | 74 | if empty_defaults: |
paul@343 | 75 | t = self._set_defaults(t, empty_defaults) |
paul@343 | 76 | l.append(tuple(t)) |
paul@343 | 77 | return l |
paul@343 | 78 | finally: |
paul@343 | 79 | f.close() |
paul@343 | 80 | finally: |
paul@343 | 81 | self.release_lock(user) |
paul@343 | 82 | |
paul@343 | 83 | def _set_table(self, user, filename, items, empty_defaults=None): |
paul@343 | 84 | |
paul@343 | 85 | """ |
paul@343 | 86 | For the given 'user', write to the file having the given 'filename' the |
paul@343 | 87 | 'items'. |
paul@343 | 88 | |
paul@343 | 89 | The 'empty_defaults' is a list of (index, value) tuples indicating the |
paul@343 | 90 | default value where a column either does not exist or provides an empty |
paul@343 | 91 | value. |
paul@343 | 92 | """ |
paul@343 | 93 | |
paul@343 | 94 | self.acquire_lock(user) |
paul@343 | 95 | try: |
paul@395 | 96 | f = codecs.open(filename, "wb", encoding="utf-8") |
paul@343 | 97 | try: |
paul@343 | 98 | for item in items: |
paul@343 | 99 | if empty_defaults: |
paul@343 | 100 | item = self._set_defaults(list(item), empty_defaults) |
paul@343 | 101 | f.write("\t".join(item) + "\n") |
paul@343 | 102 | finally: |
paul@343 | 103 | f.close() |
paul@343 | 104 | fix_permissions(filename) |
paul@343 | 105 | finally: |
paul@343 | 106 | self.release_lock(user) |
paul@343 | 107 | |
paul@648 | 108 | # Store object access. |
paul@648 | 109 | |
paul@329 | 110 | def _get_object(self, user, filename): |
paul@329 | 111 | |
paul@329 | 112 | """ |
paul@329 | 113 | Return the parsed object for the given 'user' having the given |
paul@329 | 114 | 'filename'. |
paul@329 | 115 | """ |
paul@329 | 116 | |
paul@329 | 117 | self.acquire_lock(user) |
paul@329 | 118 | try: |
paul@329 | 119 | f = open(filename, "rb") |
paul@329 | 120 | try: |
paul@329 | 121 | return parse_object(f, "utf-8") |
paul@329 | 122 | finally: |
paul@329 | 123 | f.close() |
paul@329 | 124 | finally: |
paul@329 | 125 | self.release_lock(user) |
paul@329 | 126 | |
paul@329 | 127 | def _set_object(self, user, filename, node): |
paul@329 | 128 | |
paul@329 | 129 | """ |
paul@329 | 130 | Set an object for the given 'user' having the given 'filename', using |
paul@329 | 131 | 'node' to define the object. |
paul@329 | 132 | """ |
paul@329 | 133 | |
paul@329 | 134 | self.acquire_lock(user) |
paul@329 | 135 | try: |
paul@329 | 136 | f = open(filename, "wb") |
paul@329 | 137 | try: |
paul@329 | 138 | to_stream(f, node) |
paul@329 | 139 | finally: |
paul@329 | 140 | f.close() |
paul@329 | 141 | fix_permissions(filename) |
paul@329 | 142 | finally: |
paul@329 | 143 | self.release_lock(user) |
paul@329 | 144 | |
paul@329 | 145 | return True |
paul@329 | 146 | |
paul@329 | 147 | def _remove_object(self, filename): |
paul@329 | 148 | |
paul@329 | 149 | "Remove the object with the given 'filename'." |
paul@329 | 150 | |
paul@329 | 151 | try: |
paul@329 | 152 | remove(filename) |
paul@329 | 153 | except OSError: |
paul@329 | 154 | return False |
paul@329 | 155 | |
paul@329 | 156 | return True |
paul@329 | 157 | |
paul@343 | 158 | def _remove_collection(self, filename): |
paul@343 | 159 | |
paul@343 | 160 | "Remove the collection with the given 'filename'." |
paul@343 | 161 | |
paul@343 | 162 | try: |
paul@343 | 163 | rmdir(filename) |
paul@343 | 164 | except OSError: |
paul@343 | 165 | return False |
paul@343 | 166 | |
paul@343 | 167 | return True |
paul@343 | 168 | |
paul@648 | 169 | # Event and event metadata access. |
paul@648 | 170 | |
paul@119 | 171 | def get_events(self, user): |
paul@119 | 172 | |
paul@119 | 173 | "Return a list of event identifiers." |
paul@119 | 174 | |
paul@138 | 175 | filename = self.get_object_in_store(user, "objects") |
paul@119 | 176 | if not filename or not exists(filename): |
paul@119 | 177 | return None |
paul@119 | 178 | |
paul@119 | 179 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@119 | 180 | |
paul@648 | 181 | def get_all_events(self, user): |
paul@648 | 182 | |
paul@648 | 183 | "Return a set of (uid, recurrenceid) tuples for all events." |
paul@648 | 184 | |
paul@648 | 185 | uids = self.get_events(user) |
paul@648 | 186 | |
paul@648 | 187 | all_events = set() |
paul@648 | 188 | for uid in uids: |
paul@648 | 189 | all_events.add((uid, None)) |
paul@648 | 190 | all_events.update([(uid, recurrenceid) for recurrenceid in self.get_recurrences(user, uid)]) |
paul@648 | 191 | |
paul@648 | 192 | return all_events |
paul@648 | 193 | |
paul@648 | 194 | def get_active_events(self, user): |
paul@648 | 195 | |
paul@648 | 196 | "Return a set of uncancelled events of the form (uid, recurrenceid)." |
paul@648 | 197 | |
paul@648 | 198 | all_events = self.get_all_events(user) |
paul@648 | 199 | |
paul@648 | 200 | # Filter out cancelled events. |
paul@648 | 201 | |
paul@648 | 202 | cancelled = self.get_cancellations(user) or [] |
paul@648 | 203 | all_events.difference_update(cancelled) |
paul@648 | 204 | return all_events |
paul@648 | 205 | |
paul@343 | 206 | def get_event(self, user, uid, recurrenceid=None): |
paul@343 | 207 | |
paul@343 | 208 | """ |
paul@343 | 209 | Get the event for the given 'user' with the given 'uid'. If |
paul@343 | 210 | the optional 'recurrenceid' is specified, a specific instance or |
paul@343 | 211 | occurrence of an event is returned. |
paul@343 | 212 | """ |
paul@343 | 213 | |
paul@343 | 214 | if recurrenceid: |
paul@343 | 215 | return self.get_recurrence(user, uid, recurrenceid) |
paul@343 | 216 | else: |
paul@343 | 217 | return self.get_complete_event(user, uid) |
paul@343 | 218 | |
paul@343 | 219 | def get_complete_event(self, user, uid): |
paul@50 | 220 | |
paul@50 | 221 | "Get the event for the given 'user' with the given 'uid'." |
paul@50 | 222 | |
paul@138 | 223 | filename = self.get_object_in_store(user, "objects", uid) |
paul@50 | 224 | if not filename or not exists(filename): |
paul@50 | 225 | return None |
paul@50 | 226 | |
paul@329 | 227 | return self._get_object(user, filename) |
paul@50 | 228 | |
paul@343 | 229 | def set_event(self, user, uid, recurrenceid, node): |
paul@343 | 230 | |
paul@343 | 231 | """ |
paul@343 | 232 | Set an event for 'user' having the given 'uid' and 'recurrenceid' (which |
paul@343 | 233 | if the latter is specified, a specific instance or occurrence of an |
paul@343 | 234 | event is referenced), using the given 'node' description. |
paul@343 | 235 | """ |
paul@343 | 236 | |
paul@343 | 237 | if recurrenceid: |
paul@343 | 238 | return self.set_recurrence(user, uid, recurrenceid, node) |
paul@343 | 239 | else: |
paul@343 | 240 | return self.set_complete_event(user, uid, node) |
paul@343 | 241 | |
paul@343 | 242 | def set_complete_event(self, user, uid, node): |
paul@50 | 243 | |
paul@50 | 244 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@50 | 245 | |
paul@138 | 246 | filename = self.get_object_in_store(user, "objects", uid) |
paul@50 | 247 | if not filename: |
paul@50 | 248 | return False |
paul@50 | 249 | |
paul@329 | 250 | return self._set_object(user, filename, node) |
paul@15 | 251 | |
paul@365 | 252 | def remove_event(self, user, uid, recurrenceid=None): |
paul@234 | 253 | |
paul@343 | 254 | """ |
paul@343 | 255 | Remove an event for 'user' having the given 'uid'. If the optional |
paul@343 | 256 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@343 | 257 | event is removed. |
paul@343 | 258 | """ |
paul@343 | 259 | |
paul@343 | 260 | if recurrenceid: |
paul@343 | 261 | return self.remove_recurrence(user, uid, recurrenceid) |
paul@343 | 262 | else: |
paul@343 | 263 | for recurrenceid in self.get_recurrences(user, uid) or []: |
paul@343 | 264 | self.remove_recurrence(user, uid, recurrenceid) |
paul@343 | 265 | return self.remove_complete_event(user, uid) |
paul@343 | 266 | |
paul@343 | 267 | def remove_complete_event(self, user, uid): |
paul@343 | 268 | |
paul@234 | 269 | "Remove an event for 'user' having the given 'uid'." |
paul@234 | 270 | |
paul@378 | 271 | self.remove_recurrences(user, uid) |
paul@369 | 272 | |
paul@234 | 273 | filename = self.get_object_in_store(user, "objects", uid) |
paul@234 | 274 | if not filename: |
paul@234 | 275 | return False |
paul@234 | 276 | |
paul@329 | 277 | return self._remove_object(filename) |
paul@234 | 278 | |
paul@334 | 279 | def get_recurrences(self, user, uid): |
paul@334 | 280 | |
paul@334 | 281 | """ |
paul@334 | 282 | Get additional event instances for an event of the given 'user' with the |
paul@334 | 283 | indicated 'uid'. |
paul@334 | 284 | """ |
paul@334 | 285 | |
paul@334 | 286 | filename = self.get_object_in_store(user, "recurrences", uid) |
paul@334 | 287 | if not filename or not exists(filename): |
paul@347 | 288 | return [] |
paul@334 | 289 | |
paul@334 | 290 | return [name for name in listdir(filename) if isfile(join(filename, name))] |
paul@334 | 291 | |
paul@334 | 292 | def get_recurrence(self, user, uid, recurrenceid): |
paul@334 | 293 | |
paul@334 | 294 | """ |
paul@334 | 295 | For the event of the given 'user' with the given 'uid', return the |
paul@334 | 296 | specific recurrence indicated by the 'recurrenceid'. |
paul@334 | 297 | """ |
paul@334 | 298 | |
paul@334 | 299 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 300 | if not filename or not exists(filename): |
paul@334 | 301 | return None |
paul@334 | 302 | |
paul@334 | 303 | return self._get_object(user, filename) |
paul@334 | 304 | |
paul@334 | 305 | def set_recurrence(self, user, uid, recurrenceid, node): |
paul@334 | 306 | |
paul@334 | 307 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@334 | 308 | |
paul@334 | 309 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 310 | if not filename: |
paul@334 | 311 | return False |
paul@334 | 312 | |
paul@334 | 313 | return self._set_object(user, filename, node) |
paul@334 | 314 | |
paul@334 | 315 | def remove_recurrence(self, user, uid, recurrenceid): |
paul@334 | 316 | |
paul@378 | 317 | """ |
paul@378 | 318 | Remove a special recurrence from an event stored by 'user' having the |
paul@378 | 319 | given 'uid' and 'recurrenceid'. |
paul@378 | 320 | """ |
paul@334 | 321 | |
paul@378 | 322 | filename = self.get_object_in_store(user, "recurrences", uid, recurrenceid) |
paul@334 | 323 | if not filename: |
paul@334 | 324 | return False |
paul@334 | 325 | |
paul@334 | 326 | return self._remove_object(filename) |
paul@334 | 327 | |
paul@378 | 328 | def remove_recurrences(self, user, uid): |
paul@378 | 329 | |
paul@378 | 330 | """ |
paul@378 | 331 | Remove all recurrences for an event stored by 'user' having the given |
paul@378 | 332 | 'uid'. |
paul@378 | 333 | """ |
paul@378 | 334 | |
paul@378 | 335 | for recurrenceid in self.get_recurrences(user, uid): |
paul@378 | 336 | self.remove_recurrence(user, uid, recurrenceid) |
paul@378 | 337 | |
paul@378 | 338 | recurrences = self.get_object_in_store(user, "recurrences", uid) |
paul@378 | 339 | if recurrences: |
paul@378 | 340 | return self._remove_collection(recurrences) |
paul@378 | 341 | |
paul@378 | 342 | return True |
paul@378 | 343 | |
paul@648 | 344 | # Free/busy period access. |
paul@648 | 345 | |
paul@15 | 346 | def get_freebusy(self, user): |
paul@15 | 347 | |
paul@15 | 348 | "Get free/busy details for the given 'user'." |
paul@15 | 349 | |
paul@52 | 350 | filename = self.get_object_in_store(user, "freebusy") |
paul@15 | 351 | if not filename or not exists(filename): |
paul@167 | 352 | return [] |
paul@112 | 353 | else: |
paul@458 | 354 | return map(lambda t: FreeBusyPeriod(*t), self._get_table(user, filename, [(4, None)])) |
paul@2 | 355 | |
paul@112 | 356 | def get_freebusy_for_other(self, user, other): |
paul@112 | 357 | |
paul@112 | 358 | "For the given 'user', get free/busy details for the 'other' user." |
paul@112 | 359 | |
paul@112 | 360 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@167 | 361 | if not filename or not exists(filename): |
paul@167 | 362 | return [] |
paul@112 | 363 | else: |
paul@458 | 364 | return map(lambda t: FreeBusyPeriod(*t), self._get_table(user, filename, [(4, None)])) |
paul@2 | 365 | |
paul@15 | 366 | def set_freebusy(self, user, freebusy): |
paul@15 | 367 | |
paul@15 | 368 | "For the given 'user', set 'freebusy' details." |
paul@15 | 369 | |
paul@52 | 370 | filename = self.get_object_in_store(user, "freebusy") |
paul@15 | 371 | if not filename: |
paul@15 | 372 | return False |
paul@15 | 373 | |
paul@563 | 374 | self._set_table(user, filename, map(lambda fb: fb.as_tuple(strings_only=True), freebusy)) |
paul@15 | 375 | return True |
paul@15 | 376 | |
paul@110 | 377 | def set_freebusy_for_other(self, user, freebusy, other): |
paul@110 | 378 | |
paul@110 | 379 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@110 | 380 | |
paul@110 | 381 | filename = self.get_object_in_store(user, "freebusy-other", other) |
paul@110 | 382 | if not filename: |
paul@110 | 383 | return False |
paul@110 | 384 | |
paul@563 | 385 | self._set_table(user, filename, map(lambda fb: fb.as_tuple(strings_only=True), freebusy)) |
paul@112 | 386 | return True |
paul@112 | 387 | |
paul@648 | 388 | # Object status details access. |
paul@648 | 389 | |
paul@142 | 390 | def _get_requests(self, user, queue): |
paul@66 | 391 | |
paul@142 | 392 | "Get requests for the given 'user' from the given 'queue'." |
paul@66 | 393 | |
paul@142 | 394 | filename = self.get_object_in_store(user, queue) |
paul@81 | 395 | if not filename or not exists(filename): |
paul@66 | 396 | return None |
paul@66 | 397 | |
paul@343 | 398 | return self._get_table(user, filename, [(1, None)]) |
paul@66 | 399 | |
paul@142 | 400 | def get_requests(self, user): |
paul@142 | 401 | |
paul@142 | 402 | "Get requests for the given 'user'." |
paul@142 | 403 | |
paul@142 | 404 | return self._get_requests(user, "requests") |
paul@142 | 405 | |
paul@142 | 406 | def get_cancellations(self, user): |
paul@142 | 407 | |
paul@142 | 408 | "Get cancellations for the given 'user'." |
paul@55 | 409 | |
paul@142 | 410 | return self._get_requests(user, "cancellations") |
paul@142 | 411 | |
paul@142 | 412 | def _set_requests(self, user, requests, queue): |
paul@66 | 413 | |
paul@142 | 414 | """ |
paul@142 | 415 | For the given 'user', set the list of queued 'requests' in the given |
paul@142 | 416 | 'queue'. |
paul@142 | 417 | """ |
paul@142 | 418 | |
paul@142 | 419 | filename = self.get_object_in_store(user, queue) |
paul@66 | 420 | if not filename: |
paul@66 | 421 | return False |
paul@66 | 422 | |
paul@303 | 423 | self.acquire_lock(user) |
paul@66 | 424 | try: |
paul@303 | 425 | f = open(filename, "w") |
paul@303 | 426 | try: |
paul@303 | 427 | for request in requests: |
paul@343 | 428 | print >>f, "\t".join([value or "" for value in request]) |
paul@303 | 429 | finally: |
paul@303 | 430 | f.close() |
paul@303 | 431 | fix_permissions(filename) |
paul@66 | 432 | finally: |
paul@303 | 433 | self.release_lock(user) |
paul@66 | 434 | |
paul@66 | 435 | return True |
paul@66 | 436 | |
paul@142 | 437 | def set_requests(self, user, requests): |
paul@142 | 438 | |
paul@142 | 439 | "For the given 'user', set the list of queued 'requests'." |
paul@142 | 440 | |
paul@142 | 441 | return self._set_requests(user, requests, "requests") |
paul@142 | 442 | |
paul@142 | 443 | def set_cancellations(self, user, cancellations): |
paul@66 | 444 | |
paul@142 | 445 | "For the given 'user', set the list of queued 'cancellations'." |
paul@142 | 446 | |
paul@142 | 447 | return self._set_requests(user, cancellations, "cancellations") |
paul@55 | 448 | |
paul@343 | 449 | def _set_request(self, user, uid, recurrenceid, queue): |
paul@142 | 450 | |
paul@343 | 451 | """ |
paul@343 | 452 | For the given 'user', set the queued 'uid' and 'recurrenceid' in the |
paul@343 | 453 | given 'queue'. |
paul@343 | 454 | """ |
paul@142 | 455 | |
paul@142 | 456 | filename = self.get_object_in_store(user, queue) |
paul@55 | 457 | if not filename: |
paul@55 | 458 | return False |
paul@55 | 459 | |
paul@303 | 460 | self.acquire_lock(user) |
paul@55 | 461 | try: |
paul@303 | 462 | f = open(filename, "a") |
paul@303 | 463 | try: |
paul@343 | 464 | print >>f, "\t".join([uid, recurrenceid or ""]) |
paul@303 | 465 | finally: |
paul@303 | 466 | f.close() |
paul@303 | 467 | fix_permissions(filename) |
paul@55 | 468 | finally: |
paul@303 | 469 | self.release_lock(user) |
paul@55 | 470 | |
paul@55 | 471 | return True |
paul@55 | 472 | |
paul@343 | 473 | def set_request(self, user, uid, recurrenceid=None): |
paul@142 | 474 | |
paul@343 | 475 | "For the given 'user', set the queued 'uid' and 'recurrenceid'." |
paul@142 | 476 | |
paul@343 | 477 | return self._set_request(user, uid, recurrenceid, "requests") |
paul@142 | 478 | |
paul@343 | 479 | def set_cancellation(self, user, uid, recurrenceid=None): |
paul@343 | 480 | |
paul@343 | 481 | "For the given 'user', set the queued 'uid' and 'recurrenceid'." |
paul@142 | 482 | |
paul@343 | 483 | return self._set_request(user, uid, recurrenceid, "cancellations") |
paul@142 | 484 | |
paul@343 | 485 | def queue_request(self, user, uid, recurrenceid=None): |
paul@142 | 486 | |
paul@343 | 487 | """ |
paul@343 | 488 | Queue a request for 'user' having the given 'uid'. If the optional |
paul@343 | 489 | 'recurrenceid' is specified, the request refers to a specific instance |
paul@343 | 490 | or occurrence of an event. |
paul@343 | 491 | """ |
paul@66 | 492 | |
paul@81 | 493 | requests = self.get_requests(user) or [] |
paul@66 | 494 | |
paul@343 | 495 | if (uid, recurrenceid) not in requests: |
paul@343 | 496 | return self.set_request(user, uid, recurrenceid) |
paul@66 | 497 | |
paul@66 | 498 | return False |
paul@66 | 499 | |
paul@343 | 500 | def dequeue_request(self, user, uid, recurrenceid=None): |
paul@105 | 501 | |
paul@343 | 502 | """ |
paul@343 | 503 | Dequeue a request for 'user' having the given 'uid'. If the optional |
paul@343 | 504 | 'recurrenceid' is specified, the request refers to a specific instance |
paul@343 | 505 | or occurrence of an event. |
paul@343 | 506 | """ |
paul@105 | 507 | |
paul@105 | 508 | requests = self.get_requests(user) or [] |
paul@105 | 509 | |
paul@105 | 510 | try: |
paul@343 | 511 | requests.remove((uid, recurrenceid)) |
paul@105 | 512 | self.set_requests(user, requests) |
paul@105 | 513 | except ValueError: |
paul@105 | 514 | return False |
paul@105 | 515 | else: |
paul@105 | 516 | return True |
paul@105 | 517 | |
paul@343 | 518 | def cancel_event(self, user, uid, recurrenceid=None): |
paul@142 | 519 | |
paul@343 | 520 | """ |
paul@343 | 521 | Queue an event for cancellation for 'user' having the given 'uid'. If |
paul@343 | 522 | the optional 'recurrenceid' is specified, a specific instance or |
paul@343 | 523 | occurrence of an event is cancelled. |
paul@343 | 524 | """ |
paul@142 | 525 | |
paul@142 | 526 | cancellations = self.get_cancellations(user) or [] |
paul@142 | 527 | |
paul@343 | 528 | if (uid, recurrenceid) not in cancellations: |
paul@343 | 529 | return self.set_cancellation(user, uid, recurrenceid) |
paul@142 | 530 | |
paul@142 | 531 | return False |
paul@142 | 532 | |
paul@30 | 533 | class FilePublisher(FileBase): |
paul@30 | 534 | |
paul@30 | 535 | "A publisher of objects." |
paul@30 | 536 | |
paul@597 | 537 | def __init__(self, store_dir=None): |
paul@597 | 538 | FileBase.__init__(self, store_dir or PUBLISH_DIR) |
paul@30 | 539 | |
paul@30 | 540 | def set_freebusy(self, user, freebusy): |
paul@30 | 541 | |
paul@30 | 542 | "For the given 'user', set 'freebusy' details." |
paul@30 | 543 | |
paul@52 | 544 | filename = self.get_object_in_store(user, "freebusy") |
paul@30 | 545 | if not filename: |
paul@30 | 546 | return False |
paul@30 | 547 | |
paul@30 | 548 | record = [] |
paul@30 | 549 | rwrite = record.append |
paul@30 | 550 | |
paul@30 | 551 | rwrite(("ORGANIZER", {}, user)) |
paul@30 | 552 | rwrite(("UID", {}, user)) |
paul@30 | 553 | rwrite(("DTSTAMP", {}, datetime.utcnow().strftime("%Y%m%dT%H%M%SZ"))) |
paul@30 | 554 | |
paul@458 | 555 | for fb in freebusy: |
paul@458 | 556 | if not fb.transp or fb.transp == "OPAQUE": |
paul@529 | 557 | rwrite(("FREEBUSY", {"FBTYPE" : "BUSY"}, "/".join( |
paul@563 | 558 | map(format_datetime, [fb.get_start_point(), fb.get_end_point()])))) |
paul@30 | 559 | |
paul@395 | 560 | f = open(filename, "wb") |
paul@30 | 561 | try: |
paul@30 | 562 | to_stream(f, make_calendar([("VFREEBUSY", {}, record)], "PUBLISH")) |
paul@30 | 563 | finally: |
paul@30 | 564 | f.close() |
paul@103 | 565 | fix_permissions(filename) |
paul@30 | 566 | |
paul@30 | 567 | return True |
paul@30 | 568 | |
paul@2 | 569 | # vim: tabstop=4 expandtab shiftwidth=4 |