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
7 # $Id: Roles.py 5574 2007-10-25 20:33:17Z thierry $
10 from types import StringTypes
11 from PLC.Faults import *
12 from PLC.Parameter import Parameter
13 from PLC.Filter import Filter
14 from PLC.Table import Row, Table
18 Representation of a row in the roles table. To use,
19 instantiate with a dict of values.
23 primary_key = 'role_id'
24 join_tables = ['person_role', ('slice_attribute_types', 'min_role_id')]
26 'role_id': Parameter(int, "Role identifier"),
27 'name': Parameter(str, "Role", max = 100),
30 def validate_role_id(self, role_id):
31 # Make sure role does not already exist
32 conflicts = Roles(self.api, [role_id])
34 raise PLCInvalidArgument, "Role ID already in use"
38 def validate_name(self, name):
39 # Make sure name is not blank
41 raise PLCInvalidArgument, "Role must be specified"
43 # Make sure role does not already exist
44 conflicts = Roles(self.api, [name])
46 raise PLCInvalidArgument, "Role name already in use"
52 Representation of the roles table in the database.
55 def __init__(self, api, role_filter = None, columns = None):
56 Table.__init__(self, api, Role)
58 sql = "SELECT %s FROM roles WHERE True" % \
59 ", ".join(Role.fields)
61 if role_filter is not None:
62 if isinstance(role_filter, (list, tuple, set)):
63 # Separate the list into integers and strings
64 ints = filter(lambda x: isinstance(x, (int, long)), role_filter)
65 strs = filter(lambda x: isinstance(x, StringTypes), role_filter)
66 role_filter = Filter(Role.fields, {'role_id': ints, 'name': strs})
67 sql += " AND (%s) %s" % role_filter.sql(api, "OR")
68 elif isinstance(role_filter, dict):
69 role_filter = Filter(Role.fields, role_filter)
70 sql += " AND (%s) %s" % role_filter.sql(api, "AND")