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