X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FRoles.py;h=fcc05f4bd609edf609d01d7047d8b53ef46ff84b;hb=refs%2Fheads%2Fremove-xmlrpc;hp=c035fe6b0b6dcc2d66d6986022a08bc36eb03540;hpb=369bddedd50b4f9a3e779e09d67fdfb9a2fbd086;p=plcapi.git diff --git a/PLC/Roles.py b/PLC/Roles.py index c035fe6..fcc05f4 100644 --- a/PLC/Roles.py +++ b/PLC/Roles.py @@ -4,22 +4,75 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: Roles.py,v 1.1 2006/09/06 15:36:07 mlhuang Exp $ -# -class Roles(dict): +from types import StringTypes +from PLC.Faults import * +from PLC.Parameter import Parameter +from PLC.Filter import Filter +from PLC.Table import Row, Table + +class Role(Row): + """ + Representation of a row in the roles table. To use, + instantiate with a dict of values. + """ + + table_name = 'roles' + primary_key = 'role_id' + join_tables = ['person_role', 'tag_type_role' ] + fields = { + 'role_id': Parameter(int, "Role identifier"), + 'name': Parameter(str, "Role", max = 100), + } + + def validate_role_id(self, role_id): + # Make sure role does not already exist + conflicts = Roles(self.api, [role_id]) + if conflicts: + raise PLCInvalidArgument, "Role ID already in use" + + return role_id + + def validate_name(self, name): + # Make sure name is not blank + if not len(name): + raise PLCInvalidArgument, "Role must be specified" + + # Make sure role does not already exist + conflicts = Roles(self.api, [name]) + if conflicts: + raise PLCInvalidArgument, "Role name already in use" + + return name + +class Roles(Table): """ Representation of the roles table in the database. """ - # Role IDs equal to or lower than this number are for use by real - # accounts. Other role IDs are used internally. - role_max = 500 + def __init__(self, api, role_filter = None): + Table.__init__(self, api, Role) + + sql = "SELECT %s FROM roles WHERE True" % \ + ", ".join(Role.fields) - def __init__(self, api): - sql = "SELECT * FROM roles" \ - " WHERE role_id <= %d" % self.role_max + if role_filter is not None: + if isinstance(role_filter, (list, tuple, set)): + # Separate the list into integers and strings + ints = filter(lambda x: isinstance(x, (int, long)), role_filter) + strs = filter(lambda x: isinstance(x, StringTypes), role_filter) + role_filter = Filter(Role.fields, {'role_id': ints, 'name': strs}) + sql += " AND (%s) %s" % role_filter.sql(api, "OR") + elif isinstance(role_filter, dict): + role_filter = Filter(Role.fields, role_filter) + sql += " AND (%s) %s" % role_filter.sql(api, "AND") + elif isinstance(role_filter, (int, long)): + role_filter = Filter(Role.fields, {'role_id': role_filter}) + sql += " AND (%s) %s" % role_filter.sql(api, "AND") + elif isinstance(role_filter, StringTypes): + role_filter = Filter(Role.fields, {'name': role_filter}) + sql += " AND (%s) %s" % role_filter.sql(api, "AND") + else: + raise PLCInvalidArgument, "Wrong role filter %r"%role_filter - for row in api.db.selectall(sql): - self[row['role_id']] = row['name'] - self[row['name']] = row['role_id'] + self.selectall(sql)