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
11 from types import StringTypes
12 from PLC.Faults import *
13 from PLC.Parameter import Parameter
14 from PLC.Filter import Filter
15 from PLC.Table import Row, Table
17 class AddressType(Row):
19 Representation of a row in the address_types table. To use,
20 instantiate with a dict of values.
23 table_name = 'address_types'
24 primary_key = 'address_type_id'
25 join_tables = ['address_address_type']
27 'address_type_id': Parameter(int, "Address type identifier"),
28 'name': Parameter(str, "Address type", max = 20),
29 'description': Parameter(str, "Address type description", max = 254),
32 def validate_name(self, name):
33 # Make sure name is not blank
35 raise PLCInvalidArgument, "Address type must be specified"
37 # Make sure address type does not already exist
38 conflicts = AddressTypes(self.api, [name])
39 for address_type_id in conflicts:
40 if 'address_type_id' not in self or self['address_type_id'] != address_type_id:
41 raise PLCInvalidArgument, "Address type name already in use"
45 class AddressTypes(Table):
47 Representation of the address_types table in the database.
50 def __init__(self, api, address_type_filter = None, columns = None):
51 Table.__init__(self, api, AddressType, columns)
53 sql = "SELECT %s FROM address_types WHERE True" % \
54 ", ".join(self.columns)
56 if address_type_filter is not None:
57 if isinstance(address_type_filter, (list, tuple, set)):
58 # Separate the list into integers and strings
59 ints = filter(lambda x: isinstance(x, (int, long)), address_type_filter)
60 strs = filter(lambda x: isinstance(x, StringTypes), address_type_filter)
61 address_type_filter = Filter(AddressType.fields, {'address_type_id': ints, 'name': strs})
62 sql += " AND (%s) %s" % address_type_filter.sql(api, "OR")
63 elif isinstance(address_type_filter, dict):
64 address_type_filter = Filter(AddressType.fields, address_type_filter)
65 sql += " AND (%s) %s" % address_type_filter.sql(api, "AND")