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.4 2006/10/20 17:43:30 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, optional = False),
27 'description': Parameter(str, "Address type description", max = 254),
30 def validate_name(self, name):
31 # Make sure name is not blank
33 raise PLCInvalidArgument, "Address type must be specified"
35 # Make sure address type does not already exist
36 conflicts = AddressTypes(self.api, [name])
37 for address_type_id in conflicts:
38 if 'address_type_id' not in self or self['address_type_id'] != address_type_id:
39 raise PLCInvalidArgument, "Address type name already in use"
43 class AddressTypes(Table):
45 Representation of the address_types table in the database.
48 def __init__(self, api, address_type_id_or_name_list = None):
49 sql = "SELECT %s FROM address_types" % \
50 ", ".join(AddressType.fields)
52 if address_type_id_or_name_list:
53 # Separate the list into integers and strings
54 address_type_ids = filter(lambda address_type_id: isinstance(address_type_id, (int, long)),
55 address_type_id_or_name_list)
56 names = filter(lambda name: isinstance(name, StringTypes),
57 address_type_id_or_name_list)
58 sql += " WHERE (False"
60 sql += " OR address_type_id IN (%s)" % ", ".join(map(str, address_type_ids))
62 sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
65 rows = api.db.selectall(sql)
68 self[row['address_type_id']] = AddressType(api, row)