Setting tag plcapi-5.4-2
[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
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
13
14 class AddressType(Row):
15     """
16     Representation of a row in the address_types table. To use,
17     instantiate with a dict of values.
18     """
19
20     table_name = 'address_types'
21     primary_key = 'address_type_id'
22     join_tables = ['address_address_type']
23     fields = {
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),
27         }
28
29     def validate_name(self, name):
30         # Make sure name is not blank
31         if not len(name):
32             raise PLCInvalidArgument, "Address type must be specified"
33
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"
39
40         return name
41
42 class AddressTypes(Table):
43     """
44     Representation of the address_types table in the database.
45     """
46
47     def __init__(self, api, address_type_filter = None, columns = None):
48         Table.__init__(self, api, AddressType, columns)
49
50         sql = "SELECT %s FROM address_types WHERE True" % \
51               ", ".join(self.columns)
52
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")
69             else:
70                 raise PLCInvalidArgument, "Wrong address type filter %r"%address_type_filter
71
72         self.selectall(sql)