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
8 from types import StringTypes
9 from PLC.Faults import *
10 from PLC.Parameter import Parameter
11 from PLC.Filter import Filter
12 from PLC.Table import Row, Table
14 class AddressType(Row):
16 Representation of a row in the address_types table. To use,
17 instantiate with a dict of values.
20 table_name = 'address_types'
21 primary_key = 'address_type_id'
22 join_tables = ['address_address_type']
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 validate_name(self, name):
30 # Make sure name is not blank
32 raise PLCInvalidArgument, "Address type must be specified"
34 # Make sure address type does not already exist
35 conflicts = AddressTypes(self.api, [name])
36 for address_type_id in conflicts:
37 if 'address_type_id' not in self or self['address_type_id'] != address_type_id:
38 raise PLCInvalidArgument, "Address type name already in use"
42 class AddressTypes(Table):
44 Representation of the address_types table in the database.
47 def __init__(self, api, address_type_filter = None, columns = None):
48 Table.__init__(self, api, AddressType, columns)
50 sql = "SELECT %s FROM address_types WHERE True" % \
51 ", ".join(self.columns)
53 if address_type_filter is not None:
54 if isinstance(address_type_filter, (list, tuple, set)):
55 # Separate the list into integers and strings
56 ints = filter(lambda x: isinstance(x, (int, long)), address_type_filter)
57 strs = filter(lambda x: isinstance(x, StringTypes), address_type_filter)
58 address_type_filter = Filter(AddressType.fields, {'address_type_id': ints, 'name': strs})
59 sql += " AND (%s) %s" % address_type_filter.sql(api, "OR")
60 elif isinstance(address_type_filter, dict):
61 address_type_filter = Filter(AddressType.fields, address_type_filter)
62 sql += " AND (%s) %s" % address_type_filter.sql(api, "AND")
63 elif isinstance(address_type_filter, (int, long)):
64 address_type_filter = Filter(AddressType.fields, {'address_type_id': address_type_filter})
65 sql += " AND (%s) %s" % address_type_filter.sql(api, "AND")
66 elif isinstance(address_type_filter, StringTypes):
67 address_type_filter = Filter(AddressType.fields, {'name': address_type_filter})
68 sql += " AND (%s) %s" % address_type_filter.sql(api, "AND")
70 raise PLCInvalidArgument, "Wrong address type filter %r"%address_type_filter