1 from types import StringTypes
3 from PLC.Faults import *
4 from PLC.Parameter import Parameter
5 from PLC.Filter import Filter
6 from PLC.Table import Row, Table
7 from PLC.Roles import Role, Roles
9 class SliceAttributeType(Row):
11 Representation of a row in the slice_attribute_types table. To
12 use, instantiate with a dict of values.
15 table_name = 'slice_attribute_types'
16 primary_key = 'attribute_type_id'
17 join_tables = ['slice_attribute']
19 'attribute_type_id': Parameter(int, "Slice attribute type identifier"),
20 'name': Parameter(str, "Slice attribute type name", max = 100),
21 'description': Parameter(str, "Slice attribute type description", max = 254),
22 'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
25 def validate_name(self, name):
27 raise PLCInvalidArgument, "Slice attribute type name must be set"
29 conflicts = SliceAttributeTypes(self.api, [name])
30 for attribute_type_id, attribute in conflicts.iteritems():
31 if 'attribute_type_id' not in self or self['attribute_type_id'] != attribute_type_id:
32 raise PLCInvalidArgument, "Slice attribute type name already in use"
36 def validate_min_role_id(self, role_id):
37 roles = Roles(self.api)
38 if role_id not in roles:
39 raise PLCInvalidArgument, "Invalid role"
43 class SliceAttributeTypes(Table):
45 Representation of row(s) from the slice_attribute_types table in the
49 def __init__(self, api, attribute_type_filter = None):
50 Table.__init__(self, api, SliceAttributeType)
52 sql = "SELECT %s FROM slice_attribute_types WHERE True" % \
53 ", ".join(SliceAttributeType.fields)
55 if attribute_type_filter is not None:
56 if isinstance(attribute_type_filter, (list, tuple, set)):
57 # Separate the list into integers and strings
58 ints = filter(lambda x: isinstance(x, (int, long)), attribute_type_filter)
59 strs = filter(lambda x: isinstance(x, StringTypes), attribute_type_filter)
60 attribute_type_filter = Filter(SliceAttributeType.fields, {'attribute_type_id': ints, 'name': strs})
61 sql += " AND (%s)" % attribute_type_filter.sql(api, "OR")
62 elif isinstance(attribute_type_filter, dict):
63 attribute_type_filter = Filter(SliceAttributeType.fields, attribute_type_filter)
64 sql += " AND (%s)" % attribute_type_filter.sql(api, "AND")