1 from types import StringTypes
3 from PLC.Faults import *
4 from PLC.Parameter import Parameter
5 from PLC.Table import Row, Table
6 from PLC.Roles import Roles
10 Representation of a row in the attributes table. To use, instantiate
11 with a dict of values.
14 table_name = 'attributes'
15 primary_key = 'attribute_id'
17 'attribute_id': Parameter(int, "Attribute identifier"),
18 'name': Parameter(str, "Attribute name", max = 100),
19 'description': Parameter(str, "Attribute description", max = 254),
20 'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
23 def __init__(self, api, fields = {}):
24 Row.__init__(self, fields)
27 def validate_name(self, name):
31 raise PLCInvalidArgument, "Attribute name must be set"
33 conflicts = Attributes(self.api, [name])
34 for attribute_id, attribute in conflicts.iteritems():
35 if 'attribute_id' not in self or self['attribute_id'] != attribute_id:
36 raise PLCInvalidArgument, "Attribute name already in use"
40 def validate_min_role_id(self, role_id):
41 roles = Roles(self.api)
42 if role_id not in roles:
43 raise PLCInvalidArgument, "Invalid role"
47 def delete(self, commit = True):
49 Delete existing slice attribute type.
52 assert 'attribute_id' in self
54 # Clean up miscellaneous join tables
55 for table in ['attributes', 'slice_attribute']:
56 self.api.db.do("DELETE FROM %s" \
57 " WHERE attribute_id = %d" % \
58 (table, self['attribute_id']), self)
63 class Attributes(Table):
65 Representation of row(s) from the attributes table in the
69 def __init__(self, api, attribute_id_or_name_list = None):
72 sql = "SELECT %s FROM attributes" % \
73 ", ".join(Attribute.fields)
75 if attribute_id_or_name_list:
76 # Separate the list into integers and strings
77 attribute_ids = filter(lambda attribute_id: isinstance(attribute_id, (int, long)),
78 attribute_id_or_name_list)
79 names = filter(lambda name: isinstance(name, StringTypes),
80 attribute_id_or_name_list)
81 sql += " WHERE (False"
83 sql += " OR attribute_id IN (%s)" % ", ".join(map(str, attribute_ids))
85 sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
88 rows = self.api.db.selectall(sql)
91 self[row['attribute_id']] = Attribute(api, row)