4 from PLC.Faults import *
5 from PLC.Parameter import Parameter
6 from PLC.Table import Row, Table
7 from PLC.Filter import Filter
8 from PLC.AddressTypes import AddressType, AddressTypes
12 Representation of a row in the addresses table. To use, instantiate
13 with a dict of values.
16 table_name = 'addresses'
17 primary_key = 'address_id'
18 join_tables = ['address_address_type', 'site_address']
20 'address_id': Parameter(int, "Address identifier"),
21 'line1': Parameter(str, "Address line 1", max = 254),
22 'line2': Parameter(str, "Address line 2", max = 254, nullok = True),
23 'line3': Parameter(str, "Address line 3", max = 254, nullok = True),
24 'city': Parameter(str, "City", max = 254),
25 'state': Parameter(str, "State or province", max = 254),
26 'postalcode': Parameter(str, "Postal code", max = 64),
27 'country': Parameter(str, "Country", max = 128),
28 'address_type_ids': Parameter([int], "Address type identifiers"),
29 'address_types': Parameter([str], "Address types"),
32 def add_address_type(self, address_type, commit = True):
34 Add address type to existing address.
37 assert 'address_id' in self
38 assert isinstance(address_type, AddressType)
39 assert 'address_type_id' in address_type
41 address_id = self['address_id']
42 address_type_id = address_type['address_type_id']
44 if address_type_id not in self['address_type_ids']:
45 assert address_type['name'] not in self['address_types']
47 self.api.db.do("INSERT INTO address_address_type (address_id, address_type_id)" \
48 " VALUES(%(address_id)d, %(address_type_id)d)",
54 self['address_type_ids'].append(address_type_id)
55 self['address_types'].append(address_type['name'])
57 def remove_address_type(self, address_type, commit = True):
59 Add address type to existing address.
62 assert 'address_id' in self
63 assert isinstance(address_type, AddressType)
64 assert 'address_type_id' in address_type
66 address_id = self['address_id']
67 address_type_id = address_type['address_type_id']
69 if address_type_id in self['address_type_ids']:
70 assert address_type['name'] in self['address_types']
72 self.api.db.do("DELETE FROM address_address_type" \
73 " WHERE address_id = %(address_id)d" \
74 " AND address_type_id = %(address_type_id)d",
80 self['address_type_ids'].remove(address_type_id)
81 self['address_types'].remove(address_type['name'])
83 class Addresses(Table):
85 Representation of row(s) from the addresses table in the
89 def __init__(self, api, address_filter = None, columns = None):
90 Table.__init__(self, api, Address, columns)
92 sql = "SELECT %s FROM view_addresses WHERE True" % \
93 ", ".join(self.columns)
95 if address_filter is not None:
96 if isinstance(address_filter, (list, tuple, set, int, long)):
97 address_filter = Filter(Address.fields, {'address_id': address_filter})
98 elif isinstance(address_filter, dict):
99 address_filter = Filter(Address.fields, address_filter)
100 sql += " AND (%s) %s" % address_filter.sql(api)