X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FAddressTypes.py;h=7156c00b57f901396a7ba58745873e3bcfe7d1ef;hb=refs%2Fheads%2Fplanetlab-4_0-branch;hp=e94f086419950edadd42d3b0282aa5650a060ae8;hpb=24d16d18acab3da7bccc3e09df4927e9cf2d3246;p=plcapi.git diff --git a/PLC/AddressTypes.py b/PLC/AddressTypes.py index e94f086..7156c00 100644 --- a/PLC/AddressTypes.py +++ b/PLC/AddressTypes.py @@ -4,24 +4,63 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id$ +# $Id: AddressTypes.py 5574 2007-10-25 20:33:17Z thierry $ # +from types import StringTypes +from PLC.Faults import * from PLC.Parameter import Parameter +from PLC.Filter import Filter +from PLC.Table import Row, Table -class AddressTypes(dict): +class AddressType(Row): """ - Representation of the address_types table in the database. + Representation of a row in the address_types table. To use, + instantiate with a dict of values. """ + table_name = 'address_types' + primary_key = 'address_type_id' + join_tables = ['address_address_type'] fields = { 'address_type_id': Parameter(int, "Address type identifier"), - 'name': Parameter(str, "Address type name"), + 'name': Parameter(str, "Address type", max = 20), + 'description': Parameter(str, "Address type description", max = 254), } - def __init__(self, api): - sql = "SELECT address_type_id, name FROM address_types" + def validate_name(self, name): + # Make sure name is not blank + if not len(name): + raise PLCInvalidArgument, "Address type must be specified" + + # Make sure address type does not already exist + conflicts = AddressTypes(self.api, [name]) + for address_type_id in conflicts: + if 'address_type_id' not in self or self['address_type_id'] != address_type_id: + raise PLCInvalidArgument, "Address type name already in use" + + return name + +class AddressTypes(Table): + """ + Representation of the address_types table in the database. + """ + + def __init__(self, api, address_type_filter = None, columns = None): + Table.__init__(self, api, AddressType, columns) + + sql = "SELECT %s FROM address_types WHERE True" % \ + ", ".join(self.columns) + + if address_type_filter is not None: + if isinstance(address_type_filter, (list, tuple, set)): + # Separate the list into integers and strings + ints = filter(lambda x: isinstance(x, (int, long)), address_type_filter) + strs = filter(lambda x: isinstance(x, StringTypes), address_type_filter) + address_type_filter = Filter(AddressType.fields, {'address_type_id': ints, 'name': strs}) + sql += " AND (%s) %s" % address_type_filter.sql(api, "OR") + elif isinstance(address_type_filter, dict): + address_type_filter = Filter(AddressType.fields, address_type_filter) + sql += " AND (%s) %s" % address_type_filter.sql(api, "AND") - for row in api.db.selectall(sql): - self[row['address_type_id']] = row['name'] - self[row['name']] = row['address_type_id'] + self.selectall(sql)