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