X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FSliceAttributeTypes.py;h=6aa4baa280195169b4fa64cacd0d487908f6e7e6;hb=a4b9f5d6a624c5d5b9c459bba26d07c7fe196094;hp=8c05eeae1ed92584445f35925d73852fc0fe30ea;hpb=ed7fa1ebf97ec2f88f18f8fa538e46c6ae9525c4;p=plcapi.git diff --git a/PLC/SliceAttributeTypes.py b/PLC/SliceAttributeTypes.py index 8c05eea..6aa4baa 100644 --- a/PLC/SliceAttributeTypes.py +++ b/PLC/SliceAttributeTypes.py @@ -2,6 +2,7 @@ from types import StringTypes from PLC.Faults import * from PLC.Parameter import Parameter +from PLC.Filter import Filter from PLC.Table import Row, Table from PLC.Roles import Role, Roles @@ -16,24 +17,30 @@ class SliceAttributeType(Row): join_tables = ['slice_attribute'] fields = { 'attribute_type_id': Parameter(int, "Slice attribute type identifier"), - 'name': Parameter(str, "Slice attribute type name", max = 100, optional = False), + 'name': Parameter(str, "Slice attribute type name", max = 100), 'description': Parameter(str, "Slice attribute type description", max = 254), 'min_role_id': Parameter(int, "Minimum (least powerful) role that can set or change this attribute"), } + # for Cache + class_key = 'name' + foreign_fields = ['description','min_role_id'] + foreign_xrefs = [] + def validate_name(self, name): if not len(name): raise PLCInvalidArgument, "Slice attribute type name must be set" conflicts = SliceAttributeTypes(self.api, [name]) - for attribute_type_id, attribute in conflicts.iteritems(): - if 'attribute_type_id' not in self or self['attribute_type_id'] != attribute_type_id: + for attribute in conflicts: + if 'attribute_type_id' not in self or \ + self['attribute_type_id'] != attribute['attribute_type_id']: raise PLCInvalidArgument, "Slice attribute type name already in use" return name def validate_min_role_id(self, role_id): - roles = Roles(self.api) + roles = [row['role_id'] for row in Roles(self.api)] if role_id not in roles: raise PLCInvalidArgument, "Invalid role" @@ -45,26 +52,21 @@ class SliceAttributeTypes(Table): database. """ - def __init__(self, api, attribute_type_id_or_name_list = None): - self.api = api + def __init__(self, api, attribute_type_filter = None, columns = None): + Table.__init__(self, api, SliceAttributeType, columns) - sql = "SELECT %s FROM slice_attribute_types" % \ - ", ".join(SliceAttributeType.fields) + sql = "SELECT %s FROM slice_attribute_types WHERE True" % \ + ", ".join(self.columns) - if attribute_type_id_or_name_list: - # Separate the list into integers and strings - attribute_type_ids = filter(lambda attribute_type_id: isinstance(attribute_type_id, (int, long)), - attribute_type_id_or_name_list) - names = filter(lambda name: isinstance(name, StringTypes), - attribute_type_id_or_name_list) - sql += " WHERE (False" - if attribute_type_ids: - sql += " OR attribute_type_id IN (%s)" % ", ".join(map(str, attribute_type_ids)) - if names: - sql += " OR name IN (%s)" % ", ".join(api.db.quote(names)) - sql += ")" + if attribute_type_filter is not None: + if isinstance(attribute_type_filter, (list, tuple, set)): + # Separate the list into integers and strings + ints = filter(lambda x: isinstance(x, (int, long)), attribute_type_filter) + strs = filter(lambda x: isinstance(x, StringTypes), attribute_type_filter) + attribute_type_filter = Filter(SliceAttributeType.fields, {'attribute_type_id': ints, 'name': strs}) + sql += " AND (%s)" % attribute_type_filter.sql(api, "OR") + elif isinstance(attribute_type_filter, dict): + attribute_type_filter = Filter(SliceAttributeType.fields, attribute_type_filter) + sql += " AND (%s)" % attribute_type_filter.sql(api, "AND") - rows = self.api.db.selectall(sql) - - for row in rows: - self[row['attribute_type_id']] = SliceAttributeType(api, row) + self.selectall(sql)