Setting tag plcapi-5.4-2
[plcapi.git] / PLC / Roles.py
1 #
2 # Functions for interacting with the roles table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7
8 from types import StringTypes
9 from PLC.Faults import *
10 from PLC.Parameter import Parameter
11 from PLC.Filter import Filter
12 from PLC.Table import Row, Table
13
14 class Role(Row):
15     """
16     Representation of a row in the roles table. To use,
17     instantiate with a dict of values.
18     """
19
20     table_name = 'roles'
21     primary_key = 'role_id'
22     join_tables = ['person_role', 'tag_type_role' ]
23     fields = {
24         'role_id': Parameter(int, "Role identifier"),
25         'name': Parameter(str, "Role", max = 100),
26         }
27
28     def validate_role_id(self, role_id):
29         # Make sure role does not already exist
30         conflicts = Roles(self.api, [role_id])
31         if conflicts:
32             raise PLCInvalidArgument, "Role ID already in use"
33
34         return role_id
35
36     def validate_name(self, name):
37         # Make sure name is not blank
38         if not len(name):
39             raise PLCInvalidArgument, "Role must be specified"
40
41         # Make sure role does not already exist
42         conflicts = Roles(self.api, [name])
43         if conflicts:
44             raise PLCInvalidArgument, "Role name already in use"
45
46         return name
47
48 class Roles(Table):
49     """
50     Representation of the roles table in the database.
51     """
52
53     def __init__(self, api, role_filter = None):
54         Table.__init__(self, api, Role)
55
56         sql = "SELECT %s FROM roles WHERE True" % \
57               ", ".join(Role.fields)
58
59         if role_filter is not None:
60             if isinstance(role_filter, (list, tuple, set)):
61                 # Separate the list into integers and strings
62                 ints = filter(lambda x: isinstance(x, (int, long)), role_filter)
63                 strs = filter(lambda x: isinstance(x, StringTypes), role_filter)
64                 role_filter = Filter(Role.fields, {'role_id': ints, 'name': strs})
65                 sql += " AND (%s) %s" % role_filter.sql(api, "OR")
66             elif isinstance(role_filter, dict):
67                 role_filter = Filter(Role.fields, role_filter)
68                 sql += " AND (%s) %s" % role_filter.sql(api, "AND")
69             elif isinstance(role_filter, (int, long)):
70                 role_filter = Filter(Role.fields, {'role_id': role_filter})
71                 sql += " AND (%s) %s" % role_filter.sql(api, "AND")
72             elif isinstance(role_filter, StringTypes):
73                 role_filter = Filter(Role.fields, {'name': role_filter})
74                 sql += " AND (%s) %s" % role_filter.sql(api, "AND")
75             else:
76                 raise PLCInvalidArgument, "Wrong role filter %r"%role_filter
77
78         self.selectall(sql)