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,v 1.2 2006/10/06 18:19:41 mlhuang Exp $
10 from types import StringTypes
11 from PLC.Faults import *
12 from PLC.Parameter import Parameter
13 from PLC.Table import Row, Table
17 Representation of a row in the roles table. To use,
18 instantiate with a dict of values.
22 primary_key = 'role_id'
24 'role_id': Parameter(int, "Role identifier"),
25 'name': Parameter(str, "Role", max = 100),
28 def __init__(self, api, fields = {}):
29 Row.__init__(self, fields)
32 def validate_role_id(self, role_id):
33 # Make sure role does not already exist
34 conflicts = Roles(self.api, [role_id])
36 raise PLCInvalidArgument, "Role ID already in use"
40 def validate_name(self, name):
41 # Remove leading and trailing spaces
44 # Make sure name is not blank after we removed the spaces
46 raise PLCInvalidArgument, "Role must be specified"
48 # Make sure role does not already exist
49 conflicts = Roles(self.api, [name])
51 raise PLCInvalidArgument, "Role name already in use"
55 def delete(self, commit = True):
56 assert 'role_id' in self
58 # Clean up miscellaneous join tables
59 for table in ['person_role', 'roles']:
60 self.api.db.do("DELETE FROM %s" \
61 " WHERE role_id = %d" % \
62 (table, self['role_id']), self)
64 self.api.db.do("DELETE FROM attributes WHERE min_role_id = %d" % \
72 Representation of the roles table in the database.
75 def __init__(self, api, role_id_or_name_list = None):
76 sql = "SELECT %s FROM roles" % \
77 ", ".join(Role.fields)
79 if role_id_or_name_list:
80 # Separate the list into integers and strings
81 role_ids = filter(lambda role_id: isinstance(role_id, (int, long)),
83 names = filter(lambda name: isinstance(name, StringTypes),
85 sql += " WHERE (False"
87 sql += " OR role_id IN (%s)" % ", ".join(map(str, role_ids))
89 sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
92 rows = api.db.selectall(sql)
95 self[row['role_id']] = Role(api, row)