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