- use base class __init__() and delete() implementations
[plcapi.git] / PLC / SliceAttributeTypes.py
1 from types import StringTypes
2
3 from PLC.Faults import *
4 from PLC.Parameter import Parameter
5 from PLC.Table import Row, Table
6 from PLC.Roles import Role, Roles
7
8 class SliceAttributeType(Row):
9     """
10     Representation of a row in the slice_attribute_types table. To
11     use, instantiate with a dict of values.
12     """
13
14     table_name = 'slice_attribute_types'
15     primary_key = 'attribute_type_id'
16     join_tables = ['slice_attribute']
17     fields = {
18         'attribute_type_id': Parameter(int, "Slice attribute type identifier"),
19         'name': Parameter(str, "Slice attribute type name", max = 100),
20         'description': Parameter(str, "Slice attribute type description", max = 254),
21         'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"),
22         }
23
24     def validate_name(self, name):
25         name = name.strip()
26
27         if not name:
28             raise PLCInvalidArgument, "Slice attribute type name must be set"
29
30         conflicts = SliceAttributeTypes(self.api, [name])
31         for attribute_type_id, attribute in conflicts.iteritems():
32             if 'attribute_type_id' not in self or self['attribute_type_id'] != attribute_type_id:
33                 raise PLCInvalidArgument, "Slice attribute type name already in use"
34
35         return name
36
37     def validate_min_role_id(self, role_id):
38         roles = Roles(self.api)
39         if role_id not in roles:
40             raise PLCInvalidArgument, "Invalid role"
41
42         return role_id
43
44 class SliceAttributeTypes(Table):
45     """
46     Representation of row(s) from the slice_attribute_types table in the
47     database.
48     """
49
50     def __init__(self, api, attribute_type_id_or_name_list = None):
51         self.api = api
52
53         sql = "SELECT %s FROM slice_attribute_types" % \
54               ", ".join(SliceAttributeType.fields)
55
56         if attribute_type_id_or_name_list:
57             # Separate the list into integers and strings
58             attribute_type_ids = filter(lambda attribute_type_id: isinstance(attribute_type_id, (int, long)),
59                                    attribute_type_id_or_name_list)
60             names = filter(lambda name: isinstance(name, StringTypes),
61                            attribute_type_id_or_name_list)
62             sql += " WHERE (False"
63             if attribute_type_ids:
64                 sql += " OR attribute_type_id IN (%s)" % ", ".join(map(str, attribute_type_ids))
65             if names:
66                 sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
67             sql += ")"
68
69         rows = self.api.db.selectall(sql)
70  
71         for row in rows:
72             self[row['attribute_type_id']] = SliceAttributeType(api, row)