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.Storage.AlchemyObj import AlchemyObj
13 from PLC.Table import Row, Table
15 class AddressType(AlchemyObj):
17 Representation of a row in the address_types table. To use,
18 instantiate with a dict of values.
21 tablename = 'address_types'
22 join_tables = ['address_address_type']
24 'address_type_id': Parameter(int, "Address type identifier", primary_key=True),
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 def sync(self, insert=False, validate=True):
43 AlchemyObj.sync(self, insert, validate):
44 if insert == True or 'address_type_id' not in self:
45 AlchemyObj.insert(self, dict(self))
47 AlchemyObj.update(self, dict(self))
50 def delete(self, commit=True):
51 AlchemyObj.delete(self, dict(self))
53 class AddressTypes(list):
55 Representation of the address_types table in the database.
58 def __init__(self, api, address_type_filter = None, columns = None):
59 if not address_type_filter:
60 address_types = AddressType().select()
61 elif isinstance(address_type_filter, (list, tuple, set)):
62 # Separate the list into integers and strings
63 ints = filter(lambda x: isinstance(x, (int, long)), address_type_filter)
64 strs = filter(lambda x: isinstance(x, StringTypes), address_type_filter)
65 address_types = AddressType().select(filter={'address_type_id': ints, 'name': strs})
66 elif isinstance(address_type_filter, dict):
67 address_types = AddressType().select(filter=address_type_filter)
68 elif isinstance(address_type_filter, (int, long)):
69 address_types = AddressType().select(filter={'address_type_id': address_type_filter})
70 elif isinstance(address_type_filter, StringTypes):
71 address_types = AddressType().select(filter={'name': address_type_filter})
73 raise PLCInvalidArgument, "Wrong address type filter %r"%address_type_filter
75 for address_type in address_types:
76 self.append(address_type)