2 # Functions for interacting with the roles table in the database
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
11 from types import StringTypes
12 from PLC.Faults import *
13 from PLC.Parameter import Parameter
14 from PLC.Filter import Filter
15 from PLC.Table import Row, Table
19 Representation of a row in the roles table. To use,
20 instantiate with a dict of values.
24 primary_key = 'role_id'
25 join_tables = ['person_role', ('tag_types', 'min_role_id')]
27 'role_id': Parameter(int, "Role identifier"),
28 'name': Parameter(str, "Role", max = 100),
31 def validate_role_id(self, role_id):
32 # Make sure role does not already exist
33 conflicts = Roles(self.api, [role_id])
35 raise PLCInvalidArgument, "Role ID already in use"
39 def validate_name(self, name):
40 # Make sure name is not blank
42 raise PLCInvalidArgument, "Role must be specified"
44 # Make sure role does not already exist
45 conflicts = Roles(self.api, [name])
47 raise PLCInvalidArgument, "Role name already in use"
53 Representation of the roles table in the database.
56 def __init__(self, api, role_filter = None):
57 Table.__init__(self, api, Role)
59 sql = "SELECT %s FROM roles WHERE True" % \
60 ", ".join(Role.fields)
62 if role_filter is not None:
63 if isinstance(role_filter, (list, tuple, set)):
64 # Separate the list into integers and strings
65 ints = filter(lambda x: isinstance(x, (int, long)), role_filter)
66 strs = filter(lambda x: isinstance(x, StringTypes), role_filter)
67 role_filter = Filter(Role.fields, {'role_id': ints, 'name': strs})
68 sql += " AND (%s) %s" % role_filter.sql(api, "OR")
69 elif isinstance(role_filter, dict):
70 role_filter = Filter(Role.fields, role_filter)
71 sql += " AND (%s) %s" % role_filter.sql(api, "AND")