fix NameError
[plcapi.git] / PLC / Addresses.py
1 # $Id$
2 # $URL$
3
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
9
10 class Address(Row):
11     """
12     Representation of a row in the addresses table. To use, instantiate
13     with a dict of values.
14     """
15
16     table_name = 'addresses'
17     primary_key = 'address_id'
18     join_tables = ['address_address_type', 'site_address']
19     fields = {
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"),
30         }
31
32     def add_address_type(self, address_type, commit = True):
33         """
34         Add address type to existing address.
35         """
36
37         assert 'address_id' in self
38         assert isinstance(address_type, AddressType)
39         assert 'address_type_id' in address_type
40
41         address_id = self['address_id']
42         address_type_id = address_type['address_type_id']
43
44         if address_type_id not in self['address_type_ids']:
45             assert address_type['name'] not in self['address_types']
46
47             self.api.db.do("INSERT INTO address_address_type (address_id, address_type_id)" \
48                            " VALUES(%(address_id)d, %(address_type_id)d)",
49                            locals())
50
51             if commit:
52                 self.api.db.commit()
53
54             self['address_type_ids'].append(address_type_id)
55             self['address_types'].append(address_type['name'])
56
57     def remove_address_type(self, address_type, commit = True):
58         """
59         Add address type to existing address.
60         """
61
62         assert 'address_id' in self
63         assert isinstance(address_type, AddressType)
64         assert 'address_type_id' in address_type
65
66         address_id = self['address_id']
67         address_type_id = address_type['address_type_id']
68
69         if address_type_id in self['address_type_ids']:
70             assert address_type['name'] in self['address_types']
71
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",
75                            locals())
76
77             if commit:
78                 self.api.db.commit()
79
80             self['address_type_ids'].remove(address_type_id)
81             self['address_types'].remove(address_type['name'])
82
83 class Addresses(Table):
84     """
85     Representation of row(s) from the addresses table in the
86     database.
87     """
88
89     def __init__(self, api, address_filter = None, columns = None):
90         Table.__init__(self, api, Address, columns)
91
92         sql = "SELECT %s FROM view_addresses WHERE True" % \
93               ", ".join(self.columns)
94
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)
101
102         self.selectall(sql)