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.3 2006/10/10 21:52:08 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'
23 join_tables = ['address_address_type']
25 'address_type_id': Parameter(int, "Address type identifier"),
26 'name': Parameter(str, "Address type", max = 20),
27 'description': Parameter(str, "Address type description", max = 254),
30 def validate_name(self, name):
31 # Remove leading and trailing spaces
34 # Make sure name is not blank after we removed the spaces
36 raise PLCInvalidArgument, "Address type must be specified"
38 # Make sure address type does not already exist
39 conflicts = AddressTypes(self.api, [name])
40 for address_type_id in conflicts:
41 if 'address_type_id' not in self or self['address_type_id'] != address_type_id:
42 raise PLCInvalidArgument, "Address type name already in use"
46 class AddressTypes(Table):
48 Representation of the address_types table in the database.
51 def __init__(self, api, address_type_id_or_name_list = None):
52 sql = "SELECT %s FROM address_types" % \
53 ", ".join(AddressType.fields)
55 if address_type_id_or_name_list:
56 # Separate the list into integers and strings
57 address_type_ids = filter(lambda address_type_id: isinstance(address_type_id, (int, long)),
58 address_type_id_or_name_list)
59 names = filter(lambda name: isinstance(name, StringTypes),
60 address_type_id_or_name_list)
61 sql += " WHERE (False"
63 sql += " OR address_type_id IN (%s)" % ", ".join(map(str, address_type_ids))
65 sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
68 rows = api.db.selectall(sql)
71 self[row['address_type_id']] = AddressType(api, row)