15b0ea1b5156d0354fdeaa0333dd419ce2fca074
[plcapi.git] / PLC / AddressTypes.py
1 #
2 # Functions for interacting with the address_types table in the database
3 #
4 # Mark Huang <mlhuang@cs.princeton.edu>
5 # Copyright (C) 2006 The Trustees of Princeton University
6 #
7 # $Id$
8 # $URL$
9 #
10
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
16
17 class AddressType(Row):
18     """
19     Representation of a row in the address_types table. To use,
20     instantiate with a dict of values.
21     """
22
23     table_name = 'address_types'
24     primary_key = 'address_type_id'
25     join_tables = ['address_address_type']
26     fields = {
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),
30         }
31
32     def validate_name(self, name):
33         # Make sure name is not blank
34         if not len(name):
35             raise PLCInvalidArgument, "Address type must be specified"
36
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"
42
43         return name
44
45 class AddressTypes(Table):
46     """
47     Representation of the address_types table in the database.
48     """
49
50     def __init__(self, api, address_type_filter = None, columns = None):
51         Table.__init__(self, api, AddressType, columns)
52
53         sql = "SELECT %s FROM address_types WHERE True" % \
54               ", ".join(self.columns)
55
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")
66             elif isinstance(address_type_filter, (int, long)):
67                 address_type_filter = Filter(AddressType.fields, {'address_type_id': address_type_filter})
68                 sql += " AND (%s) %s" % address_type_filter.sql(api, "AND")
69             elif isinstance(address_type_filter, StringTypes):
70                 address_type_filter = Filter(AddressType.fields, {'name': address_type_filter})
71                 sql += " AND (%s) %s" % address_type_filter.sql(api, "AND")
72             else:
73                 raise PLCInvalidArgument, "Wrong address type filter %r"%address_type_filter
74
75         self.selectall(sql)