2 # Functions for interacting with the address_types table in the database
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
7 # $Id: AddressTypes.py,v 1.2 2006/10/06 18:19:41 mlhuang Exp $
10 from types import StringTypes
11 from PLC.Faults import *
12 from PLC.Parameter import Parameter
13 from PLC.Table import Row, Table
15 class AddressType(Row):
17 Representation of a row in the address_types table. To use,
18 instantiate with a dict of values.
21 table_name = 'address_types'
22 primary_key = 'address_type_id'
24 'address_type_id': Parameter(int, "Address type identifier"),
25 'name': Parameter(str, "Address type", max = 20),
26 'description': Parameter(str, "Address type description", max = 254),
29 def __init__(self, api, fields = {}):
30 Row.__init__(self, fields)
33 def validate_name(self, name):
34 # Remove leading and trailing spaces
37 # Make sure name is not blank after we removed the spaces
39 raise PLCInvalidArgument, "Address type must be specified"
41 # Make sure address type does not already exist
42 conflicts = AddressTypes(self.api, [name])
43 for address_type_id in conflicts:
44 if 'address_type_id' not in self or self['address_type_id'] != address_type_id:
45 raise PLCInvalidArgument, "Address type name already in use"
49 def delete(self, commit = True):
50 assert 'address_type_id' in self
52 # Clean up miscellaneous join tables
53 for table in ['address_address_type', 'address_types']:
54 self.api.db.do("DELETE FROM %s" \
55 " WHERE address_type_id = %d" % \
56 (table, self['address_type_id']), self)
61 class AddressTypes(Table):
63 Representation of the address_types table in the database.
66 def __init__(self, api, address_type_id_or_name_list = None):
67 sql = "SELECT %s FROM address_types" % \
68 ", ".join(AddressType.fields)
70 if address_type_id_or_name_list:
71 # Separate the list into integers and strings
72 address_type_ids = filter(lambda address_type_id: isinstance(address_type_id, (int, long)),
73 address_type_id_or_name_list)
74 names = filter(lambda name: isinstance(name, StringTypes),
75 address_type_id_or_name_list)
76 sql += " WHERE (False"
78 sql += " OR address_type_id IN (%s)" % ", ".join(map(str, address_type_ids))
80 sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
83 rows = api.db.selectall(sql)
86 self[row['address_type_id']] = AddressType(api, row)