X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=PLC%2FAddressTypes.py;h=19f9b1689efa18b2c6f46fc00e46fd66f99ddb2d;hb=f9abd41a98993c5f4f18c0ecb7aa34eb86d58c64;hp=90e0b3b3bf4357c50c718680e786ce6305a297e5;hpb=ab3f817a87d0dc7e38c70ae9c05356b49a1b0380;p=plcapi.git diff --git a/PLC/AddressTypes.py b/PLC/AddressTypes.py index 90e0b3b..19f9b16 100644 --- a/PLC/AddressTypes.py +++ b/PLC/AddressTypes.py @@ -4,12 +4,13 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id: AddressTypes.py,v 1.2 2006/10/06 18:19:41 mlhuang Exp $ +# $Id: AddressTypes.py,v 1.8 2006/11/09 03:07:42 mlhuang Exp $ # 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 AddressType(Row): @@ -20,22 +21,16 @@ class AddressType(Row): 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", max = 20), 'description': Parameter(str, "Address type description", max = 254), } - def __init__(self, api, fields = {}): - Row.__init__(self, fields) - self.api = api - def validate_name(self, name): - # Remove leading and trailing spaces - name = name.strip() - - # Make sure name is not blank after we removed the spaces - if not 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 @@ -46,41 +41,26 @@ class AddressType(Row): return name - def delete(self, commit = True): - assert 'address_type_id' in self - - # Clean up miscellaneous join tables - for table in ['address_address_type', 'address_types']: - self.api.db.do("DELETE FROM %s" \ - " WHERE address_type_id = %d" % \ - (table, self['address_type_id']), self) - - if commit: - self.api.db.commit() - class AddressTypes(Table): """ Representation of the address_types table in the database. """ - def __init__(self, api, address_type_id_or_name_list = None): - sql = "SELECT %s FROM address_types" % \ - ", ".join(AddressType.fields) - - if address_type_id_or_name_list: - # Separate the list into integers and strings - address_type_ids = filter(lambda address_type_id: isinstance(address_type_id, (int, long)), - address_type_id_or_name_list) - names = filter(lambda name: isinstance(name, StringTypes), - address_type_id_or_name_list) - sql += " WHERE (False" - if address_type_ids: - sql += " OR address_type_id IN (%s)" % ", ".join(map(str, address_type_ids)) - if names: - sql += " OR name IN (%s)" % ", ".join(api.db.quote(names)) - sql += ")" + 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) - rows = api.db.selectall(sql) + 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)" % address_type_filter.sql(api, "OR") + elif isinstance(address_type_filter, dict): + address_type_filter = Filter(AddressType.fields, address_type_filter) + sql += " AND (%s)" % address_type_filter.sql(api, "AND") - for row in rows: - self[row['address_type_id']] = AddressType(api, row) + self.selectall(sql)