2 # Thierry Parmentelat - INRIA
6 from types import StringTypes
8 from PLC.Faults import *
9 from PLC.Parameter import Parameter
10 from PLC.Filter import Filter
11 from PLC.Table import Row, Table
12 from PLC.Roles import Role, Roles
14 class IlinkType (Row):
17 Representation of a row in the ilink_types table.
20 table_name = 'ilink_types'
21 primary_key = 'ilink_type_id'
22 join_tables = ['ilink']
24 'ilink_type_id': Parameter(int, "ilink type identifier"),
25 'name': Parameter(str, "ilink type name", max = 100),
26 'description': Parameter(str, "ilink type description", max = 254),
27 'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
30 def validate_name(self, name):
32 raise PLCInvalidArgument, "ilink type name must be set"
34 conflicts = IlinkTypes(self.api, [name])
35 for tag_type in conflicts:
36 if 'ilink_type_id' not in self or \
37 self['ilink_type_id'] != tag_type['ilink_type_id']:
38 raise PLCInvalidArgument, "ilink type name already in use"
42 def validate_min_role_id(self, role_id):
43 roles = [row['role_id'] for row in Roles(self.api)]
44 if role_id not in roles:
45 raise PLCInvalidArgument, "Invalid role"
49 class IlinkTypes(Table):
51 Representation of row(s) from the ilink_types table
55 def __init__(self, api, ilink_type_filter = None, columns = None):
56 Table.__init__(self, api, IlinkType, columns)
58 sql = "SELECT %s FROM ilink_types WHERE True" % \
59 ", ".join(self.columns)
61 if ilink_type_filter is not None:
62 if isinstance(ilink_type_filter, (list, tuple, set)):
63 # Separate the list into integers and strings
64 ints = filter(lambda x: isinstance(x, (int, long)), ilink_type_filter)
65 strs = filter(lambda x: isinstance(x, StringTypes), ilink_type_filter)
66 ilink_type_filter = Filter(IlinkType.fields, {'ilink_type_id': ints, 'name': strs})
67 sql += " AND (%s) %s" % ilink_type_filter.sql(api, "OR")
68 elif isinstance(ilink_type_filter, dict):
69 ilink_type_filter = Filter(IlinkType.fields, ilink_type_filter)
70 sql += " AND (%s) %s" % ilink_type_filter.sql(api, "AND")
71 elif isinstance (ilink_type_filter, StringTypes):
72 ilink_type_filter = Filter(IlinkType.fields, {'name':[ilink_type_filter]})
73 sql += " AND (%s) %s" % ilink_type_filter.sql(api, "AND")
75 raise PLCInvalidArgument, "Wrong ilink type filter %r"%ilink_type_filter