paul@1083 | 1 | #!/usr/bin/env python |
paul@1083 | 2 | |
paul@1083 | 3 | """ |
paul@1083 | 4 | A database store of calendar data. |
paul@1083 | 5 | |
paul@1083 | 6 | Copyright (C) 2014, 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@1083 | 7 | |
paul@1083 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@1083 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@1083 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@1083 | 11 | version. |
paul@1083 | 12 | |
paul@1083 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@1083 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@1083 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@1083 | 16 | details. |
paul@1083 | 17 | |
paul@1083 | 18 | You should have received a copy of the GNU General Public License along with |
paul@1083 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@1083 | 20 | """ |
paul@1083 | 21 | |
paul@1088 | 22 | from imiptools.stores.common import StoreBase, JournalBase |
paul@1083 | 23 | |
paul@1083 | 24 | from datetime import datetime |
paul@1083 | 25 | from imiptools.data import parse_string, to_string |
paul@1083 | 26 | from imiptools.dates import format_datetime, get_datetime, to_timezone |
paul@1088 | 27 | from imiptools.period import FreeBusyDatabaseCollection |
paul@1083 | 28 | from imiptools.sql import DatabaseOperations |
paul@1083 | 29 | |
paul@1088 | 30 | class DatabaseStoreBase(DatabaseOperations): |
paul@1083 | 31 | |
paul@1083 | 32 | "A database store supporting user-specific locking." |
paul@1083 | 33 | |
paul@1087 | 34 | def __init__(self, connection, paramstyle=None): |
paul@1087 | 35 | DatabaseOperations.__init__(self, paramstyle=paramstyle) |
paul@1087 | 36 | self.connection = connection |
paul@1087 | 37 | self.cursor = connection.cursor() |
paul@1087 | 38 | |
paul@1083 | 39 | def acquire_lock(self, user, timeout=None): |
paul@1087 | 40 | pass |
paul@1083 | 41 | |
paul@1083 | 42 | def release_lock(self, user): |
paul@1087 | 43 | pass |
paul@1083 | 44 | |
paul@1088 | 45 | class DatabaseStore(DatabaseStoreBase, StoreBase): |
paul@1083 | 46 | |
paul@1083 | 47 | "A database store of tabular free/busy data and objects." |
paul@1083 | 48 | |
paul@1083 | 49 | # User discovery. |
paul@1083 | 50 | |
paul@1083 | 51 | def get_users(self): |
paul@1083 | 52 | |
paul@1083 | 53 | "Return a list of users." |
paul@1083 | 54 | |
paul@1083 | 55 | query = "select distinct store_user from freebusy" |
paul@1083 | 56 | self.cursor.execute(query) |
paul@1083 | 57 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 58 | |
paul@1083 | 59 | # Event and event metadata access. |
paul@1083 | 60 | |
paul@1142 | 61 | def get_all_events(self, user, dirname=None): |
paul@1142 | 62 | |
paul@1142 | 63 | """ |
paul@1142 | 64 | Return a set of (uid, recurrenceid) tuples for all events. Unless |
paul@1142 | 65 | 'dirname' is specified, only active events are returned; otherwise, |
paul@1142 | 66 | events from the given 'dirname' are returned. |
paul@1142 | 67 | """ |
paul@1142 | 68 | |
paul@1142 | 69 | columns, values = self.get_event_table_filters(dirname) |
paul@1142 | 70 | |
paul@1142 | 71 | columns += ["store_user"] |
paul@1142 | 72 | values += [user] |
paul@1142 | 73 | |
paul@1142 | 74 | query, values = self.get_query( |
paul@1142 | 75 | "select object_uid, null as object_recurrenceid from objects :condition " |
paul@1142 | 76 | "union all " |
paul@1142 | 77 | "select object_uid, object_recurrenceid from recurrences :condition", |
paul@1142 | 78 | columns, values) |
paul@1142 | 79 | |
paul@1142 | 80 | self.cursor.execute(query, values) |
paul@1142 | 81 | return self.cursor.fetchall() |
paul@1142 | 82 | |
paul@1142 | 83 | def get_events(self, user, dirname=None): |
paul@1083 | 84 | |
paul@1083 | 85 | "Return a list of event identifiers." |
paul@1083 | 86 | |
paul@1142 | 87 | columns, values = self.get_event_table_filters(dirname) |
paul@1142 | 88 | |
paul@1142 | 89 | columns += ["store_user"] |
paul@1142 | 90 | values += [user] |
paul@1083 | 91 | |
paul@1083 | 92 | query, values = self.get_query( |
paul@1083 | 93 | "select object_uid from objects :condition", |
paul@1083 | 94 | columns, values) |
paul@1083 | 95 | |
paul@1083 | 96 | self.cursor.execute(query, values) |
paul@1083 | 97 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 98 | |
paul@1142 | 99 | def get_cancelled_events(self, user): |
paul@1083 | 100 | |
paul@1142 | 101 | "Return a list of event identifiers for cancelled events." |
paul@1083 | 102 | |
paul@1142 | 103 | return self.get_events(user, "cancellations") |
paul@1083 | 104 | |
paul@1083 | 105 | def get_event(self, user, uid, recurrenceid=None, dirname=None): |
paul@1083 | 106 | |
paul@1083 | 107 | """ |
paul@1083 | 108 | Get the event for the given 'user' with the given 'uid'. If |
paul@1083 | 109 | the optional 'recurrenceid' is specified, a specific instance or |
paul@1083 | 110 | occurrence of an event is returned. |
paul@1083 | 111 | """ |
paul@1083 | 112 | |
paul@1083 | 113 | table = self.get_event_table(recurrenceid, dirname) |
paul@1083 | 114 | columns, values = self.get_event_table_filters(dirname) |
paul@1083 | 115 | |
paul@1083 | 116 | if recurrenceid: |
paul@1083 | 117 | columns += ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 118 | values += [user, uid, recurrenceid] |
paul@1083 | 119 | else: |
paul@1083 | 120 | columns += ["store_user", "object_uid"] |
paul@1083 | 121 | values += [user, uid] |
paul@1083 | 122 | |
paul@1083 | 123 | query, values = self.get_query( |
paul@1083 | 124 | "select object_text from %(table)s :condition" % { |
paul@1083 | 125 | "table" : table |
paul@1083 | 126 | }, |
paul@1083 | 127 | columns, values) |
paul@1083 | 128 | |
paul@1083 | 129 | self.cursor.execute(query, values) |
paul@1083 | 130 | result = self.cursor.fetchone() |
paul@1083 | 131 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 132 | |
paul@1083 | 133 | def get_complete_event(self, user, uid): |
paul@1083 | 134 | |
paul@1083 | 135 | "Get the event for the given 'user' with the given 'uid'." |
paul@1083 | 136 | |
paul@1083 | 137 | columns = ["store_user", "object_uid"] |
paul@1083 | 138 | values = [user, uid] |
paul@1083 | 139 | |
paul@1083 | 140 | query, values = self.get_query( |
paul@1083 | 141 | "select object_text from objects :condition", |
paul@1083 | 142 | columns, values) |
paul@1083 | 143 | |
paul@1083 | 144 | self.cursor.execute(query, values) |
paul@1083 | 145 | result = self.cursor.fetchone() |
paul@1083 | 146 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 147 | |
paul@1083 | 148 | def set_complete_event(self, user, uid, node): |
paul@1083 | 149 | |
paul@1083 | 150 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@1083 | 151 | |
paul@1083 | 152 | columns = ["store_user", "object_uid"] |
paul@1083 | 153 | values = [user, uid] |
paul@1083 | 154 | setcolumns = ["object_text", "status"] |
paul@1083 | 155 | setvalues = [to_string(node, "utf-8"), "active"] |
paul@1083 | 156 | |
paul@1083 | 157 | query, values = self.get_query( |
paul@1083 | 158 | "update objects :set :condition", |
paul@1083 | 159 | columns, values, setcolumns, setvalues) |
paul@1083 | 160 | |
paul@1083 | 161 | self.cursor.execute(query, values) |
paul@1083 | 162 | |
paul@1083 | 163 | if self.cursor.rowcount > 0 or self.get_complete_event(user, uid): |
paul@1083 | 164 | return True |
paul@1083 | 165 | |
paul@1083 | 166 | columns = ["store_user", "object_uid", "object_text", "status"] |
paul@1083 | 167 | values = [user, uid, to_string(node, "utf-8"), "active"] |
paul@1083 | 168 | |
paul@1083 | 169 | query, values = self.get_query( |
paul@1083 | 170 | "insert into objects (:columns) values (:values)", |
paul@1083 | 171 | columns, values) |
paul@1083 | 172 | |
paul@1083 | 173 | self.cursor.execute(query, values) |
paul@1083 | 174 | return True |
paul@1083 | 175 | |
paul@1083 | 176 | def remove_parent_event(self, user, uid): |
paul@1083 | 177 | |
paul@1083 | 178 | "Remove the parent event for 'user' having the given 'uid'." |
paul@1083 | 179 | |
paul@1083 | 180 | columns = ["store_user", "object_uid"] |
paul@1083 | 181 | values = [user, uid] |
paul@1083 | 182 | |
paul@1083 | 183 | query, values = self.get_query( |
paul@1083 | 184 | "delete from objects :condition", |
paul@1083 | 185 | columns, values) |
paul@1083 | 186 | |
paul@1083 | 187 | self.cursor.execute(query, values) |
paul@1083 | 188 | return self.cursor.rowcount > 0 |
paul@1083 | 189 | |
paul@1083 | 190 | def get_active_recurrences(self, user, uid): |
paul@1083 | 191 | |
paul@1083 | 192 | """ |
paul@1083 | 193 | Get additional event instances for an event of the given 'user' with the |
paul@1083 | 194 | indicated 'uid'. Cancelled recurrences are not returned. |
paul@1083 | 195 | """ |
paul@1083 | 196 | |
paul@1083 | 197 | columns = ["store_user", "object_uid", "status"] |
paul@1083 | 198 | values = [user, uid, "active"] |
paul@1083 | 199 | |
paul@1083 | 200 | query, values = self.get_query( |
paul@1083 | 201 | "select object_recurrenceid from recurrences :condition", |
paul@1083 | 202 | columns, values) |
paul@1083 | 203 | |
paul@1083 | 204 | self.cursor.execute(query, values) |
paul@1083 | 205 | return [t[0] for t in self.cursor.fetchall() or []] |
paul@1083 | 206 | |
paul@1083 | 207 | def get_cancelled_recurrences(self, user, uid): |
paul@1083 | 208 | |
paul@1083 | 209 | """ |
paul@1083 | 210 | Get additional event instances for an event of the given 'user' with the |
paul@1083 | 211 | indicated 'uid'. Only cancelled recurrences are returned. |
paul@1083 | 212 | """ |
paul@1083 | 213 | |
paul@1083 | 214 | columns = ["store_user", "object_uid", "status"] |
paul@1083 | 215 | values = [user, uid, "cancelled"] |
paul@1083 | 216 | |
paul@1083 | 217 | query, values = self.get_query( |
paul@1083 | 218 | "select object_recurrenceid from recurrences :condition", |
paul@1083 | 219 | columns, values) |
paul@1083 | 220 | |
paul@1083 | 221 | self.cursor.execute(query, values) |
paul@1083 | 222 | return [t[0] for t in self.cursor.fetchall() or []] |
paul@1083 | 223 | |
paul@1083 | 224 | def get_recurrence(self, user, uid, recurrenceid): |
paul@1083 | 225 | |
paul@1083 | 226 | """ |
paul@1083 | 227 | For the event of the given 'user' with the given 'uid', return the |
paul@1083 | 228 | specific recurrence indicated by the 'recurrenceid'. |
paul@1083 | 229 | """ |
paul@1083 | 230 | |
paul@1083 | 231 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 232 | values = [user, uid, recurrenceid] |
paul@1083 | 233 | |
paul@1083 | 234 | query, values = self.get_query( |
paul@1083 | 235 | "select object_text from recurrences :condition", |
paul@1083 | 236 | columns, values) |
paul@1083 | 237 | |
paul@1083 | 238 | self.cursor.execute(query, values) |
paul@1083 | 239 | result = self.cursor.fetchone() |
paul@1083 | 240 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 241 | |
paul@1083 | 242 | def set_recurrence(self, user, uid, recurrenceid, node): |
paul@1083 | 243 | |
paul@1083 | 244 | "Set an event for 'user' having the given 'uid' and 'node'." |
paul@1083 | 245 | |
paul@1083 | 246 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 247 | values = [user, uid, recurrenceid] |
paul@1083 | 248 | setcolumns = ["object_text", "status"] |
paul@1083 | 249 | setvalues = [to_string(node, "utf-8"), "active"] |
paul@1083 | 250 | |
paul@1083 | 251 | query, values = self.get_query( |
paul@1083 | 252 | "update recurrences :set :condition", |
paul@1083 | 253 | columns, values, setcolumns, setvalues) |
paul@1083 | 254 | |
paul@1083 | 255 | self.cursor.execute(query, values) |
paul@1083 | 256 | |
paul@1083 | 257 | if self.cursor.rowcount > 0 or self.get_recurrence(user, uid, recurrenceid): |
paul@1083 | 258 | return True |
paul@1083 | 259 | |
paul@1083 | 260 | columns = ["store_user", "object_uid", "object_recurrenceid", "object_text", "status"] |
paul@1083 | 261 | values = [user, uid, recurrenceid, to_string(node, "utf-8"), "active"] |
paul@1083 | 262 | |
paul@1083 | 263 | query, values = self.get_query( |
paul@1083 | 264 | "insert into recurrences (:columns) values (:values)", |
paul@1083 | 265 | columns, values) |
paul@1083 | 266 | |
paul@1083 | 267 | self.cursor.execute(query, values) |
paul@1083 | 268 | return True |
paul@1083 | 269 | |
paul@1083 | 270 | def remove_recurrence(self, user, uid, recurrenceid): |
paul@1083 | 271 | |
paul@1083 | 272 | """ |
paul@1083 | 273 | Remove a special recurrence from an event stored by 'user' having the |
paul@1083 | 274 | given 'uid' and 'recurrenceid'. |
paul@1083 | 275 | """ |
paul@1083 | 276 | |
paul@1083 | 277 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 278 | values = [user, uid, recurrenceid] |
paul@1083 | 279 | |
paul@1083 | 280 | query, values = self.get_query( |
paul@1083 | 281 | "delete from recurrences :condition", |
paul@1083 | 282 | columns, values) |
paul@1083 | 283 | |
paul@1083 | 284 | self.cursor.execute(query, values) |
paul@1083 | 285 | return True |
paul@1083 | 286 | |
paul@1083 | 287 | def remove_recurrences(self, user, uid): |
paul@1083 | 288 | |
paul@1083 | 289 | """ |
paul@1083 | 290 | Remove all recurrences for an event stored by 'user' having the given |
paul@1083 | 291 | 'uid'. |
paul@1083 | 292 | """ |
paul@1083 | 293 | |
paul@1083 | 294 | columns = ["store_user", "object_uid"] |
paul@1083 | 295 | values = [user, uid] |
paul@1083 | 296 | |
paul@1083 | 297 | query, values = self.get_query( |
paul@1083 | 298 | "delete from recurrences :condition", |
paul@1083 | 299 | columns, values) |
paul@1083 | 300 | |
paul@1083 | 301 | self.cursor.execute(query, values) |
paul@1083 | 302 | return True |
paul@1083 | 303 | |
paul@1142 | 304 | # Event table computation. |
paul@1142 | 305 | |
paul@1142 | 306 | def get_event_table(self, recurrenceid=None, dirname=None): |
paul@1142 | 307 | |
paul@1142 | 308 | "Get the table providing events for any specified 'dirname'." |
paul@1142 | 309 | |
paul@1142 | 310 | if recurrenceid: |
paul@1142 | 311 | return self.get_recurrence_table(dirname) |
paul@1142 | 312 | else: |
paul@1142 | 313 | return self.get_complete_event_table(dirname) |
paul@1142 | 314 | |
paul@1142 | 315 | def get_event_table_filters(self, dirname=None): |
paul@1142 | 316 | |
paul@1142 | 317 | "Get filter details for any specified 'dirname'." |
paul@1142 | 318 | |
paul@1142 | 319 | if dirname == "cancellations": |
paul@1142 | 320 | return ["status"], ["cancelled"] |
paul@1142 | 321 | else: |
paul@1142 | 322 | return ["status"], ["active"] |
paul@1142 | 323 | |
paul@1142 | 324 | def get_complete_event_table(self, dirname=None): |
paul@1142 | 325 | |
paul@1142 | 326 | "Get the table providing events for any specified 'dirname'." |
paul@1142 | 327 | |
paul@1142 | 328 | if dirname == "counters": |
paul@1142 | 329 | return "countered_objects" |
paul@1142 | 330 | else: |
paul@1142 | 331 | return "objects" |
paul@1142 | 332 | |
paul@1142 | 333 | def get_recurrence_table(self, dirname=None): |
paul@1142 | 334 | |
paul@1142 | 335 | "Get the table providing recurrences for any specified 'dirname'." |
paul@1142 | 336 | |
paul@1142 | 337 | if dirname == "counters": |
paul@1142 | 338 | return "countered_recurrences" |
paul@1142 | 339 | else: |
paul@1142 | 340 | return "recurrences" |
paul@1142 | 341 | |
paul@1083 | 342 | # Free/busy period providers, upon extension of the free/busy records. |
paul@1083 | 343 | |
paul@1083 | 344 | def _get_freebusy_providers(self, user): |
paul@1083 | 345 | |
paul@1083 | 346 | """ |
paul@1083 | 347 | Return the free/busy providers for the given 'user'. |
paul@1083 | 348 | |
paul@1083 | 349 | This function returns any stored datetime and a list of providers as a |
paul@1083 | 350 | 2-tuple. Each provider is itself a (uid, recurrenceid) tuple. |
paul@1083 | 351 | """ |
paul@1083 | 352 | |
paul@1083 | 353 | columns = ["store_user"] |
paul@1083 | 354 | values = [user] |
paul@1083 | 355 | |
paul@1083 | 356 | query, values = self.get_query( |
paul@1083 | 357 | "select object_uid, object_recurrenceid from freebusy_providers :condition", |
paul@1083 | 358 | columns, values) |
paul@1083 | 359 | |
paul@1083 | 360 | self.cursor.execute(query, values) |
paul@1083 | 361 | providers = self.cursor.fetchall() |
paul@1083 | 362 | |
paul@1083 | 363 | columns = ["store_user"] |
paul@1083 | 364 | values = [user] |
paul@1083 | 365 | |
paul@1083 | 366 | query, values = self.get_query( |
paul@1083 | 367 | "select start from freebusy_provider_datetimes :condition", |
paul@1083 | 368 | columns, values) |
paul@1083 | 369 | |
paul@1083 | 370 | self.cursor.execute(query, values) |
paul@1083 | 371 | result = self.cursor.fetchone() |
paul@1083 | 372 | dt_string = result and result[0] |
paul@1083 | 373 | |
paul@1083 | 374 | return dt_string, providers |
paul@1083 | 375 | |
paul@1083 | 376 | def _set_freebusy_providers(self, user, dt_string, t): |
paul@1083 | 377 | |
paul@1083 | 378 | "Set the given provider timestamp 'dt_string' and table 't'." |
paul@1083 | 379 | |
paul@1083 | 380 | # NOTE: Locking? |
paul@1083 | 381 | |
paul@1083 | 382 | columns = ["store_user"] |
paul@1083 | 383 | values = [user] |
paul@1083 | 384 | |
paul@1083 | 385 | query, values = self.get_query( |
paul@1083 | 386 | "delete from freebusy_providers :condition", |
paul@1083 | 387 | columns, values) |
paul@1083 | 388 | |
paul@1083 | 389 | self.cursor.execute(query, values) |
paul@1083 | 390 | |
paul@1083 | 391 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 392 | |
paul@1083 | 393 | for uid, recurrenceid in t: |
paul@1083 | 394 | values = [user, uid, recurrenceid] |
paul@1083 | 395 | |
paul@1083 | 396 | query, values = self.get_query( |
paul@1083 | 397 | "insert into freebusy_providers (:columns) values (:values)", |
paul@1083 | 398 | columns, values) |
paul@1083 | 399 | |
paul@1083 | 400 | self.cursor.execute(query, values) |
paul@1083 | 401 | |
paul@1083 | 402 | columns = ["store_user"] |
paul@1083 | 403 | values = [user] |
paul@1083 | 404 | setcolumns = ["start"] |
paul@1083 | 405 | setvalues = [dt_string] |
paul@1083 | 406 | |
paul@1083 | 407 | query, values = self.get_query( |
paul@1083 | 408 | "update freebusy_provider_datetimes :set :condition", |
paul@1083 | 409 | columns, values, setcolumns, setvalues) |
paul@1083 | 410 | |
paul@1083 | 411 | self.cursor.execute(query, values) |
paul@1083 | 412 | |
paul@1083 | 413 | if self.cursor.rowcount > 0: |
paul@1083 | 414 | return True |
paul@1083 | 415 | |
paul@1083 | 416 | columns = ["store_user", "start"] |
paul@1083 | 417 | values = [user, dt_string] |
paul@1083 | 418 | |
paul@1083 | 419 | query, values = self.get_query( |
paul@1083 | 420 | "insert into freebusy_provider_datetimes (:columns) values (:values)", |
paul@1083 | 421 | columns, values) |
paul@1083 | 422 | |
paul@1083 | 423 | self.cursor.execute(query, values) |
paul@1083 | 424 | return True |
paul@1083 | 425 | |
paul@1083 | 426 | # Free/busy period access. |
paul@1083 | 427 | |
paul@1083 | 428 | def get_freebusy(self, user, name=None, mutable=False): |
paul@1083 | 429 | |
paul@1083 | 430 | "Get free/busy details for the given 'user'." |
paul@1083 | 431 | |
paul@1083 | 432 | table = name or "freebusy" |
paul@1083 | 433 | return FreeBusyDatabaseCollection(self.cursor, table, ["store_user"], [user], mutable, self.paramstyle) |
paul@1083 | 434 | |
paul@1083 | 435 | def get_freebusy_for_other(self, user, other, mutable=False): |
paul@1083 | 436 | |
paul@1083 | 437 | "For the given 'user', get free/busy details for the 'other' user." |
paul@1083 | 438 | |
paul@1088 | 439 | table = "freebusy_other" |
paul@1083 | 440 | return FreeBusyDatabaseCollection(self.cursor, table, ["store_user", "other"], [user, other], mutable, self.paramstyle) |
paul@1083 | 441 | |
paul@1083 | 442 | def set_freebusy(self, user, freebusy, name=None): |
paul@1083 | 443 | |
paul@1083 | 444 | "For the given 'user', set 'freebusy' details." |
paul@1083 | 445 | |
paul@1083 | 446 | table = name or "freebusy" |
paul@1083 | 447 | |
paul@1083 | 448 | if not isinstance(freebusy, FreeBusyDatabaseCollection) or freebusy.table_name != table: |
paul@1083 | 449 | fbc = FreeBusyDatabaseCollection(self.cursor, table, ["store_user"], [user], True, self.paramstyle) |
paul@1083 | 450 | fbc += freebusy |
paul@1083 | 451 | |
paul@1083 | 452 | return True |
paul@1083 | 453 | |
paul@1083 | 454 | def set_freebusy_for_other(self, user, freebusy, other): |
paul@1083 | 455 | |
paul@1083 | 456 | "For the given 'user', set 'freebusy' details for the 'other' user." |
paul@1083 | 457 | |
paul@1088 | 458 | table = "freebusy_other" |
paul@1083 | 459 | |
paul@1083 | 460 | if not isinstance(freebusy, FreeBusyDatabaseCollection) or freebusy.table_name != table: |
paul@1083 | 461 | fbc = FreeBusyDatabaseCollection(self.cursor, table, ["store_user", "other"], [user, other], True, self.paramstyle) |
paul@1083 | 462 | fbc += freebusy |
paul@1083 | 463 | |
paul@1083 | 464 | return True |
paul@1083 | 465 | |
paul@1142 | 466 | def get_freebusy_others(self, user): |
paul@1142 | 467 | |
paul@1142 | 468 | """ |
paul@1142 | 469 | For the given 'user', return a list of other users for whom free/busy |
paul@1142 | 470 | information is retained. |
paul@1142 | 471 | """ |
paul@1142 | 472 | |
paul@1142 | 473 | columns = ["store_user"] |
paul@1142 | 474 | values = [user] |
paul@1142 | 475 | |
paul@1142 | 476 | query, values = self.get_query( |
paul@1146 | 477 | "select distinct other from freebusy_other :condition", |
paul@1142 | 478 | columns, values) |
paul@1142 | 479 | |
paul@1142 | 480 | self.cursor.execute(query, values) |
paul@1142 | 481 | return [r[0] for r in self.cursor.fetchall()] |
paul@1142 | 482 | |
paul@1083 | 483 | # Tentative free/busy periods related to countering. |
paul@1083 | 484 | |
paul@1083 | 485 | def get_freebusy_offers(self, user, mutable=False): |
paul@1083 | 486 | |
paul@1083 | 487 | "Get free/busy offers for the given 'user'." |
paul@1083 | 488 | |
paul@1083 | 489 | # Expire old offers and save the collection if modified. |
paul@1083 | 490 | |
paul@1083 | 491 | now = format_datetime(to_timezone(datetime.utcnow(), "UTC")) |
paul@1083 | 492 | columns = ["store_user", "expires"] |
paul@1083 | 493 | values = [user, now] |
paul@1083 | 494 | |
paul@1083 | 495 | query, values = self.get_query( |
paul@1083 | 496 | "delete from freebusy_offers :condition", |
paul@1083 | 497 | columns, values) |
paul@1083 | 498 | |
paul@1083 | 499 | self.cursor.execute(query, values) |
paul@1083 | 500 | |
paul@1083 | 501 | return self.get_freebusy(user, "freebusy_offers", mutable) |
paul@1083 | 502 | |
paul@1088 | 503 | def set_freebusy_offers(self, user, freebusy): |
paul@1088 | 504 | |
paul@1088 | 505 | "For the given 'user', set 'freebusy' offers." |
paul@1088 | 506 | |
paul@1088 | 507 | return self.set_freebusy(user, freebusy, "freebusy_offers") |
paul@1088 | 508 | |
paul@1083 | 509 | # Requests and counter-proposals. |
paul@1083 | 510 | |
paul@1083 | 511 | def get_requests(self, user): |
paul@1083 | 512 | |
paul@1083 | 513 | "Get requests for the given 'user'." |
paul@1083 | 514 | |
paul@1083 | 515 | columns = ["store_user"] |
paul@1083 | 516 | values = [user] |
paul@1083 | 517 | |
paul@1083 | 518 | query, values = self.get_query( |
paul@1088 | 519 | "select object_uid, object_recurrenceid, request_type from requests :condition", |
paul@1083 | 520 | columns, values) |
paul@1083 | 521 | |
paul@1083 | 522 | self.cursor.execute(query, values) |
paul@1083 | 523 | return self.cursor.fetchall() |
paul@1083 | 524 | |
paul@1083 | 525 | def set_request(self, user, uid, recurrenceid=None, type=None): |
paul@1083 | 526 | |
paul@1083 | 527 | """ |
paul@1083 | 528 | For the given 'user', set the queued 'uid' and 'recurrenceid', |
paul@1083 | 529 | indicating a request, along with any given 'type'. |
paul@1083 | 530 | """ |
paul@1083 | 531 | |
paul@1083 | 532 | columns = ["store_user", "object_uid", "object_recurrenceid", "request_type"] |
paul@1083 | 533 | values = [user, uid, recurrenceid, type] |
paul@1083 | 534 | |
paul@1083 | 535 | query, values = self.get_query( |
paul@1083 | 536 | "insert into requests (:columns) values (:values)", |
paul@1083 | 537 | columns, values) |
paul@1083 | 538 | |
paul@1083 | 539 | self.cursor.execute(query, values) |
paul@1083 | 540 | return True |
paul@1083 | 541 | |
paul@1088 | 542 | def queue_request(self, user, uid, recurrenceid=None, type=None): |
paul@1088 | 543 | |
paul@1088 | 544 | """ |
paul@1088 | 545 | Queue a request for 'user' having the given 'uid'. If the optional |
paul@1088 | 546 | 'recurrenceid' is specified, the entry refers to a specific instance |
paul@1088 | 547 | or occurrence of an event. The 'type' parameter can be used to indicate |
paul@1088 | 548 | a specific type of request. |
paul@1088 | 549 | """ |
paul@1088 | 550 | |
paul@1088 | 551 | if recurrenceid: |
paul@1088 | 552 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1088 | 553 | values = [user, uid, recurrenceid] |
paul@1088 | 554 | else: |
paul@1088 | 555 | columns = ["store_user", "object_uid"] |
paul@1088 | 556 | values = [user, uid] |
paul@1088 | 557 | |
paul@1088 | 558 | setcolumns = ["request_type"] |
paul@1088 | 559 | setvalues = [type] |
paul@1088 | 560 | |
paul@1088 | 561 | query, values = self.get_query( |
paul@1088 | 562 | "update requests :set :condition", |
paul@1088 | 563 | columns, values, setcolumns, setvalues) |
paul@1088 | 564 | |
paul@1088 | 565 | self.cursor.execute(query, values) |
paul@1088 | 566 | |
paul@1088 | 567 | if self.cursor.rowcount > 0: |
paul@1088 | 568 | return |
paul@1088 | 569 | |
paul@1088 | 570 | self.set_request(user, uid, recurrenceid, type) |
paul@1088 | 571 | |
paul@1088 | 572 | def dequeue_request(self, user, uid, recurrenceid=None): |
paul@1088 | 573 | |
paul@1088 | 574 | """ |
paul@1088 | 575 | Dequeue all requests for 'user' having the given 'uid'. If the optional |
paul@1088 | 576 | 'recurrenceid' is specified, all requests for that specific instance or |
paul@1088 | 577 | occurrence of an event are dequeued. |
paul@1088 | 578 | """ |
paul@1088 | 579 | |
paul@1088 | 580 | if recurrenceid: |
paul@1088 | 581 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1088 | 582 | values = [user, uid, recurrenceid] |
paul@1088 | 583 | else: |
paul@1088 | 584 | columns = ["store_user", "object_uid"] |
paul@1088 | 585 | values = [user, uid] |
paul@1088 | 586 | |
paul@1088 | 587 | query, values = self.get_query( |
paul@1088 | 588 | "delete from requests :condition", |
paul@1088 | 589 | columns, values) |
paul@1088 | 590 | |
paul@1088 | 591 | self.cursor.execute(query, values) |
paul@1088 | 592 | return True |
paul@1088 | 593 | |
paul@1083 | 594 | def get_counters(self, user, uid, recurrenceid=None): |
paul@1083 | 595 | |
paul@1083 | 596 | """ |
paul@1083 | 597 | For the given 'user', return a list of users from whom counter-proposals |
paul@1083 | 598 | have been received for the given 'uid' and optional 'recurrenceid'. |
paul@1083 | 599 | """ |
paul@1083 | 600 | |
paul@1083 | 601 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 602 | |
paul@1083 | 603 | if recurrenceid: |
paul@1083 | 604 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 605 | values = [user, uid, recurrenceid] |
paul@1083 | 606 | else: |
paul@1083 | 607 | columns = ["store_user", "object_uid"] |
paul@1083 | 608 | values = [user, uid] |
paul@1083 | 609 | |
paul@1083 | 610 | query, values = self.get_query( |
paul@1083 | 611 | "select other from %(table)s :condition" % { |
paul@1083 | 612 | "table" : table |
paul@1083 | 613 | }, |
paul@1083 | 614 | columns, values) |
paul@1083 | 615 | |
paul@1083 | 616 | self.cursor.execute(query, values) |
paul@1146 | 617 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 618 | |
paul@1083 | 619 | def get_counter(self, user, other, uid, recurrenceid=None): |
paul@1083 | 620 | |
paul@1083 | 621 | """ |
paul@1083 | 622 | For the given 'user', return the counter-proposal from 'other' for the |
paul@1083 | 623 | given 'uid' and optional 'recurrenceid'. |
paul@1083 | 624 | """ |
paul@1083 | 625 | |
paul@1083 | 626 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 627 | |
paul@1083 | 628 | if recurrenceid: |
paul@1083 | 629 | columns = ["store_user", "other", "object_uid", "object_recurrenceid"] |
paul@1083 | 630 | values = [user, other, uid, recurrenceid] |
paul@1083 | 631 | else: |
paul@1083 | 632 | columns = ["store_user", "other", "object_uid"] |
paul@1083 | 633 | values = [user, other, uid] |
paul@1083 | 634 | |
paul@1083 | 635 | query, values = self.get_query( |
paul@1083 | 636 | "select object_text from %(table)s :condition" % { |
paul@1083 | 637 | "table" : table |
paul@1083 | 638 | }, |
paul@1083 | 639 | columns, values) |
paul@1083 | 640 | |
paul@1083 | 641 | self.cursor.execute(query, values) |
paul@1088 | 642 | result = self.cursor.fetchone() |
paul@1083 | 643 | return result and parse_string(result[0], "utf-8") |
paul@1083 | 644 | |
paul@1083 | 645 | def set_counter(self, user, other, node, uid, recurrenceid=None): |
paul@1083 | 646 | |
paul@1083 | 647 | """ |
paul@1083 | 648 | For the given 'user', store a counter-proposal received from 'other' the |
paul@1083 | 649 | given 'node' representing that proposal for the given 'uid' and |
paul@1083 | 650 | 'recurrenceid'. |
paul@1083 | 651 | """ |
paul@1083 | 652 | |
paul@1083 | 653 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 654 | |
paul@1088 | 655 | if recurrenceid: |
paul@1088 | 656 | columns = ["store_user", "other", "object_uid", "object_recurrenceid", "object_text"] |
paul@1088 | 657 | values = [user, other, uid, recurrenceid, to_string(node, "utf-8")] |
paul@1088 | 658 | else: |
paul@1088 | 659 | columns = ["store_user", "other", "object_uid", "object_text"] |
paul@1088 | 660 | values = [user, other, uid, to_string(node, "utf-8")] |
paul@1083 | 661 | |
paul@1083 | 662 | query, values = self.get_query( |
paul@1083 | 663 | "insert into %(table)s (:columns) values (:values)" % { |
paul@1083 | 664 | "table" : table |
paul@1083 | 665 | }, |
paul@1083 | 666 | columns, values) |
paul@1083 | 667 | |
paul@1083 | 668 | self.cursor.execute(query, values) |
paul@1083 | 669 | return True |
paul@1083 | 670 | |
paul@1083 | 671 | def remove_counters(self, user, uid, recurrenceid=None): |
paul@1083 | 672 | |
paul@1083 | 673 | """ |
paul@1083 | 674 | For the given 'user', remove all counter-proposals associated with the |
paul@1083 | 675 | given 'uid' and 'recurrenceid'. |
paul@1083 | 676 | """ |
paul@1083 | 677 | |
paul@1083 | 678 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 679 | |
paul@1083 | 680 | if recurrenceid: |
paul@1083 | 681 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 682 | values = [user, uid, recurrenceid] |
paul@1083 | 683 | else: |
paul@1083 | 684 | columns = ["store_user", "object_uid"] |
paul@1083 | 685 | values = [user, uid] |
paul@1083 | 686 | |
paul@1083 | 687 | query, values = self.get_query( |
paul@1083 | 688 | "delete from %(table)s :condition" % { |
paul@1083 | 689 | "table" : table |
paul@1083 | 690 | }, |
paul@1083 | 691 | columns, values) |
paul@1083 | 692 | |
paul@1083 | 693 | self.cursor.execute(query, values) |
paul@1083 | 694 | return True |
paul@1083 | 695 | |
paul@1083 | 696 | def remove_counter(self, user, other, uid, recurrenceid=None): |
paul@1083 | 697 | |
paul@1083 | 698 | """ |
paul@1083 | 699 | For the given 'user', remove any counter-proposal from 'other' |
paul@1083 | 700 | associated with the given 'uid' and 'recurrenceid'. |
paul@1083 | 701 | """ |
paul@1083 | 702 | |
paul@1083 | 703 | table = self.get_event_table(recurrenceid, "counters") |
paul@1083 | 704 | |
paul@1083 | 705 | if recurrenceid: |
paul@1083 | 706 | columns = ["store_user", "other", "object_uid", "object_recurrenceid"] |
paul@1083 | 707 | values = [user, other, uid, recurrenceid] |
paul@1083 | 708 | else: |
paul@1083 | 709 | columns = ["store_user", "other", "object_uid"] |
paul@1083 | 710 | values = [user, other, uid] |
paul@1083 | 711 | |
paul@1083 | 712 | query, values = self.get_query( |
paul@1083 | 713 | "delete from %(table)s :condition" % { |
paul@1083 | 714 | "table" : table |
paul@1083 | 715 | }, |
paul@1083 | 716 | columns, values) |
paul@1083 | 717 | |
paul@1083 | 718 | self.cursor.execute(query, values) |
paul@1083 | 719 | return True |
paul@1083 | 720 | |
paul@1083 | 721 | # Event cancellation. |
paul@1083 | 722 | |
paul@1083 | 723 | def cancel_event(self, user, uid, recurrenceid=None): |
paul@1083 | 724 | |
paul@1083 | 725 | """ |
paul@1083 | 726 | Cancel an event for 'user' having the given 'uid'. If the optional |
paul@1083 | 727 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@1083 | 728 | event is cancelled. |
paul@1083 | 729 | """ |
paul@1083 | 730 | |
paul@1083 | 731 | table = self.get_event_table(recurrenceid) |
paul@1083 | 732 | |
paul@1083 | 733 | if recurrenceid: |
paul@1083 | 734 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 735 | values = [user, uid, recurrenceid] |
paul@1083 | 736 | else: |
paul@1083 | 737 | columns = ["store_user", "object_uid"] |
paul@1083 | 738 | values = [user, uid] |
paul@1083 | 739 | |
paul@1083 | 740 | setcolumns = ["status"] |
paul@1083 | 741 | setvalues = ["cancelled"] |
paul@1083 | 742 | |
paul@1083 | 743 | query, values = self.get_query( |
paul@1083 | 744 | "update %(table)s :set :condition" % { |
paul@1083 | 745 | "table" : table |
paul@1083 | 746 | }, |
paul@1083 | 747 | columns, values, setcolumns, setvalues) |
paul@1083 | 748 | |
paul@1083 | 749 | self.cursor.execute(query, values) |
paul@1083 | 750 | return True |
paul@1083 | 751 | |
paul@1083 | 752 | def uncancel_event(self, user, uid, recurrenceid=None): |
paul@1083 | 753 | |
paul@1083 | 754 | """ |
paul@1083 | 755 | Uncancel an event for 'user' having the given 'uid'. If the optional |
paul@1083 | 756 | 'recurrenceid' is specified, a specific instance or occurrence of an |
paul@1083 | 757 | event is uncancelled. |
paul@1083 | 758 | """ |
paul@1083 | 759 | |
paul@1083 | 760 | table = self.get_event_table(recurrenceid) |
paul@1083 | 761 | |
paul@1083 | 762 | if recurrenceid: |
paul@1083 | 763 | columns = ["store_user", "object_uid", "object_recurrenceid"] |
paul@1083 | 764 | values = [user, uid, recurrenceid] |
paul@1083 | 765 | else: |
paul@1083 | 766 | columns = ["store_user", "object_uid"] |
paul@1083 | 767 | values = [user, uid] |
paul@1083 | 768 | |
paul@1083 | 769 | setcolumns = ["status"] |
paul@1083 | 770 | setvalues = ["active"] |
paul@1083 | 771 | |
paul@1083 | 772 | query, values = self.get_query( |
paul@1083 | 773 | "update %(table)s :set :condition" % { |
paul@1083 | 774 | "table" : table |
paul@1083 | 775 | }, |
paul@1083 | 776 | columns, values, setcolumns, setvalues) |
paul@1083 | 777 | |
paul@1083 | 778 | self.cursor.execute(query, values) |
paul@1083 | 779 | return True |
paul@1083 | 780 | |
paul@1083 | 781 | def remove_cancellation(self, user, uid, recurrenceid=None): |
paul@1083 | 782 | |
paul@1083 | 783 | """ |
paul@1083 | 784 | Remove a cancellation for 'user' for the event having the given 'uid'. |
paul@1083 | 785 | If the optional 'recurrenceid' is specified, a specific instance or |
paul@1083 | 786 | occurrence of an event is affected. |
paul@1083 | 787 | """ |
paul@1083 | 788 | |
paul@1083 | 789 | table = self.get_event_table(recurrenceid) |
paul@1083 | 790 | |
paul@1083 | 791 | if recurrenceid: |
paul@1083 | 792 | columns = ["store_user", "object_uid", "object_recurrenceid", "status"] |
paul@1083 | 793 | values = [user, uid, recurrenceid, "cancelled"] |
paul@1083 | 794 | else: |
paul@1083 | 795 | columns = ["store_user", "object_uid", "status"] |
paul@1083 | 796 | values = [user, uid, "cancelled"] |
paul@1083 | 797 | |
paul@1083 | 798 | query, values = self.get_query( |
paul@1083 | 799 | "delete from %(table)s :condition" % { |
paul@1083 | 800 | "table" : table |
paul@1083 | 801 | }, |
paul@1083 | 802 | columns, values) |
paul@1083 | 803 | |
paul@1083 | 804 | self.cursor.execute(query, values) |
paul@1083 | 805 | return True |
paul@1083 | 806 | |
paul@1083 | 807 | class DatabaseJournal(DatabaseStoreBase, JournalBase): |
paul@1083 | 808 | |
paul@1083 | 809 | "A journal system to support quotas." |
paul@1083 | 810 | |
paul@1083 | 811 | # Quota and user identity/group discovery. |
paul@1083 | 812 | |
paul@1083 | 813 | def get_quotas(self): |
paul@1083 | 814 | |
paul@1083 | 815 | "Return a list of quotas." |
paul@1083 | 816 | |
paul@1146 | 817 | query = "select distinct quota from (" \ |
paul@1146 | 818 | "select quota from quota_freebusy " \ |
paul@1146 | 819 | "union all select quota from quota_limits" \ |
paul@1146 | 820 | ") as quotas" |
paul@1083 | 821 | self.cursor.execute(query) |
paul@1083 | 822 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 823 | |
paul@1083 | 824 | def get_quota_users(self, quota): |
paul@1083 | 825 | |
paul@1083 | 826 | "Return a list of quota users." |
paul@1083 | 827 | |
paul@1083 | 828 | columns = ["quota"] |
paul@1083 | 829 | values = [quota] |
paul@1083 | 830 | |
paul@1083 | 831 | query, values = self.get_query( |
paul@1083 | 832 | "select distinct user_group from quota_freebusy :condition", |
paul@1083 | 833 | columns, values) |
paul@1083 | 834 | |
paul@1146 | 835 | self.cursor.execute(query, values) |
paul@1083 | 836 | return [r[0] for r in self.cursor.fetchall()] |
paul@1083 | 837 | |
paul@1083 | 838 | # Groups of users sharing quotas. |
paul@1083 | 839 | |
paul@1083 | 840 | def get_groups(self, quota): |
paul@1083 | 841 | |
paul@1083 | 842 | "Return the identity mappings for the given 'quota' as a dictionary." |
paul@1083 | 843 | |
paul@1083 | 844 | columns = ["quota"] |
paul@1083 | 845 | values = [quota] |
paul@1083 | 846 | |
paul@1083 | 847 | query, values = self.get_query( |
paul@1083 | 848 | "select store_user, user_group from user_groups :condition", |
paul@1083 | 849 | columns, values) |
paul@1083 | 850 | |
paul@1089 | 851 | self.cursor.execute(query, values) |
paul@1083 | 852 | return dict(self.cursor.fetchall()) |
paul@1083 | 853 | |
paul@1142 | 854 | def set_group(self, quota, store_user, user_group): |
paul@1142 | 855 | |
paul@1142 | 856 | """ |
paul@1142 | 857 | For the given 'quota', set a mapping from 'store_user' to 'user_group'. |
paul@1142 | 858 | """ |
paul@1142 | 859 | |
paul@1142 | 860 | columns = ["quota", "store_user"] |
paul@1142 | 861 | values = [quota, store_user] |
paul@1142 | 862 | setcolumns = ["user_group"] |
paul@1142 | 863 | setvalues = [user_group] |
paul@1142 | 864 | |
paul@1142 | 865 | query, values = self.get_query( |
paul@1142 | 866 | "update user_groups :set :condition", |
paul@1142 | 867 | columns, values, setcolumns, setvalues) |
paul@1142 | 868 | |
paul@1142 | 869 | self.cursor.execute(query, values) |
paul@1142 | 870 | |
paul@1142 | 871 | if self.cursor.rowcount > 0: |
paul@1142 | 872 | return True |
paul@1142 | 873 | |
paul@1142 | 874 | columns = ["quota", "store_user", "user_group"] |
paul@1142 | 875 | values = [quota, store_user, user_group] |
paul@1142 | 876 | |
paul@1142 | 877 | query, values = self.get_query( |
paul@1142 | 878 | "insert into user_groups (:columns) values (:values)", |
paul@1142 | 879 | columns, values) |
paul@1142 | 880 | |
paul@1142 | 881 | self.cursor.execute(query, values) |
paul@1142 | 882 | return True |
paul@1142 | 883 | |
paul@1083 | 884 | def get_limits(self, quota): |
paul@1083 | 885 | |
paul@1083 | 886 | """ |
paul@1083 | 887 | Return the limits for the 'quota' as a dictionary mapping identities or |
paul@1083 | 888 | groups to durations. |
paul@1083 | 889 | """ |
paul@1083 | 890 | |
paul@1083 | 891 | columns = ["quota"] |
paul@1083 | 892 | values = [quota] |
paul@1083 | 893 | |
paul@1083 | 894 | query, values = self.get_query( |
paul@1083 | 895 | "select user_group, quota_limit from quota_limits :condition", |
paul@1083 | 896 | columns, values) |
paul@1083 | 897 | |
paul@1089 | 898 | self.cursor.execute(query, values) |
paul@1083 | 899 | return dict(self.cursor.fetchall()) |
paul@1083 | 900 | |
paul@1089 | 901 | def set_limit(self, quota, group, limit): |
paul@1089 | 902 | |
paul@1089 | 903 | """ |
paul@1089 | 904 | For the given 'quota', set for a user 'group' the given 'limit' on |
paul@1089 | 905 | resource usage. |
paul@1089 | 906 | """ |
paul@1089 | 907 | |
paul@1089 | 908 | columns = ["quota", "user_group"] |
paul@1089 | 909 | values = [quota, group] |
paul@1089 | 910 | setcolumns = ["quota_limit"] |
paul@1089 | 911 | setvalues = [limit] |
paul@1089 | 912 | |
paul@1089 | 913 | query, values = self.get_query( |
paul@1089 | 914 | "update quota_limits :set :condition", |
paul@1089 | 915 | columns, values, setcolumns, setvalues) |
paul@1089 | 916 | |
paul@1089 | 917 | self.cursor.execute(query, values) |
paul@1089 | 918 | |
paul@1089 | 919 | if self.cursor.rowcount > 0: |
paul@1089 | 920 | return True |
paul@1089 | 921 | |
paul@1089 | 922 | columns = ["quota", "user_group", "quota_limit"] |
paul@1089 | 923 | values = [quota, group, limit] |
paul@1089 | 924 | |
paul@1089 | 925 | query, values = self.get_query( |
paul@1089 | 926 | "insert into quota_limits (:columns) values (:values)", |
paul@1089 | 927 | columns, values) |
paul@1089 | 928 | |
paul@1089 | 929 | self.cursor.execute(query, values) |
paul@1089 | 930 | return True |
paul@1089 | 931 | |
paul@1083 | 932 | # Free/busy period access for users within quota groups. |
paul@1083 | 933 | |
paul@1142 | 934 | def get_freebusy_users(self, quota): |
paul@1142 | 935 | |
paul@1142 | 936 | """ |
paul@1142 | 937 | Return a list of users whose free/busy details are retained for the |
paul@1142 | 938 | given 'quota'. |
paul@1142 | 939 | """ |
paul@1142 | 940 | |
paul@1142 | 941 | columns = ["quota"] |
paul@1142 | 942 | values = [quota] |
paul@1142 | 943 | |
paul@1142 | 944 | query, values = self.get_query( |
paul@1142 | 945 | "select distinct store_user from user_freebusy :condition", |
paul@1142 | 946 | columns, values) |
paul@1142 | 947 | |
paul@1146 | 948 | self.cursor.execute(query, values) |
paul@1142 | 949 | return [r[0] for r in self.cursor.fetchall()] |
paul@1142 | 950 | |
paul@1083 | 951 | def get_freebusy(self, quota, user, mutable=False): |
paul@1083 | 952 | |
paul@1083 | 953 | "Get free/busy details for the given 'quota' and 'user'." |
paul@1083 | 954 | |
paul@1083 | 955 | table = "user_freebusy" |
paul@1083 | 956 | return FreeBusyDatabaseCollection(self.cursor, table, ["quota", "store_user"], [quota, user], mutable, self.paramstyle) |
paul@1083 | 957 | |
paul@1083 | 958 | def set_freebusy(self, quota, user, freebusy): |
paul@1083 | 959 | |
paul@1083 | 960 | "For the given 'quota' and 'user', set 'freebusy' details." |
paul@1083 | 961 | |
paul@1083 | 962 | table = "user_freebusy" |
paul@1083 | 963 | |
paul@1083 | 964 | if not isinstance(freebusy, FreeBusyDatabaseCollection) or freebusy.table_name != table: |
paul@1083 | 965 | fbc = FreeBusyDatabaseCollection(self.cursor, table, ["quota", "store_user"], [quota, user], True, self.paramstyle) |
paul@1083 | 966 | fbc += freebusy |
paul@1083 | 967 | |
paul@1083 | 968 | return True |
paul@1083 | 969 | |
paul@1083 | 970 | # Journal entry methods. |
paul@1083 | 971 | |
paul@1083 | 972 | def get_entries(self, quota, group, mutable=False): |
paul@1083 | 973 | |
paul@1083 | 974 | """ |
paul@1083 | 975 | Return a list of journal entries for the given 'quota' for the indicated |
paul@1083 | 976 | 'group'. |
paul@1083 | 977 | """ |
paul@1083 | 978 | |
paul@1083 | 979 | table = "quota_freebusy" |
paul@1083 | 980 | return FreeBusyDatabaseCollection(self.cursor, table, ["quota", "user_group"], [quota, group], mutable, self.paramstyle) |
paul@1083 | 981 | |
paul@1083 | 982 | def set_entries(self, quota, group, entries): |
paul@1083 | 983 | |
paul@1083 | 984 | """ |
paul@1083 | 985 | For the given 'quota' and indicated 'group', set the list of journal |
paul@1083 | 986 | 'entries'. |
paul@1083 | 987 | """ |
paul@1083 | 988 | |
paul@1083 | 989 | table = "quota_freebusy" |
paul@1083 | 990 | |
paul@1083 | 991 | if not isinstance(entries, FreeBusyDatabaseCollection) or entries.table_name != table: |
paul@1083 | 992 | fbc = FreeBusyDatabaseCollection(self.cursor, table, ["quota", "user_group"], [quota, group], True, self.paramstyle) |
paul@1083 | 993 | fbc += entries |
paul@1083 | 994 | |
paul@1083 | 995 | return True |
paul@1083 | 996 | |
paul@1083 | 997 | # vim: tabstop=4 expandtab shiftwidth=4 |