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
17 Representation of a row in the link_types table.
20 table_name = 'link_types'
21 primary_key = 'link_type_id'
22 join_tables = ['ilink']
24 'link_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 = LinkTypes(self.api, [name])
35 for tag_type in conflicts:
36 if 'link_type_id' not in self or \
37 self['link_type_id'] != tag_type['link_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 LinkTypes(Table):
51 Representation of row(s) from the link_types table
55 def __init__(self, api, link_type_filter = None, columns = None):
56 Table.__init__(self, api, LinkType, columns)
58 sql = "SELECT %s FROM link_types WHERE True" % \
59 ", ".join(self.columns)
61 if link_type_filter is not None:
62 if isinstance(link_type_filter, (list, tuple, set)):
63 # Separate the list into integers and strings
64 ints = filter(lambda x: isinstance(x, (int, long)), link_type_filter)
65 strs = filter(lambda x: isinstance(x, StringTypes), link_type_filter)
66 link_type_filter = Filter(LinkType.fields, {'link_type_id': ints, 'name': strs})
67 sql += " AND (%s) %s" % link_type_filter.sql(api, "OR")
68 elif isinstance(link_type_filter, dict):
69 link_type_filter = Filter(LinkType.fields, link_type_filter)
70 sql += " AND (%s) %s" % link_type_filter.sql(api, "AND")
71 elif isinstance (link_type_filter, StringTypes):
72 link_type_filter = Filter(LinkType.fields, {'name':[link_type_filter]})
73 sql += " AND (%s) %s" % link_type_filter.sql(api, "AND")
75 raise PLCInvalidArgument, "Wrong ilink type filter %r"%link_type_filter