90e0b3b3bf4357c50c718680e786ce6305a297e5
[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: AddressTypes.py,v 1.2 2006/10/06 18:19:41 mlhuang Exp $
8 #
9
10 from types import StringTypes
11 from PLC.Faults import *
12 from PLC.Parameter import Parameter
13 from PLC.Table import Row, Table
14
15 class AddressType(Row):
16     """
17     Representation of a row in the address_types table. To use,
18     instantiate with a dict of values.
19     """
20
21     table_name = 'address_types'
22     primary_key = 'address_type_id'
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 __init__(self, api, fields = {}):
30         Row.__init__(self, fields)
31         self.api = api
32
33     def validate_name(self, name):
34         # Remove leading and trailing spaces
35         name = name.strip()
36
37         # Make sure name is not blank after we removed the spaces
38         if not name:
39             raise PLCInvalidArgument, "Address type must be specified"
40         
41         # Make sure address type does not already exist
42         conflicts = AddressTypes(self.api, [name])
43         for address_type_id in conflicts:
44             if 'address_type_id' not in self or self['address_type_id'] != address_type_id:
45                raise PLCInvalidArgument, "Address type name already in use"
46
47         return name
48
49     def delete(self, commit = True):
50         assert 'address_type_id' in self
51
52         # Clean up miscellaneous join tables
53         for table in ['address_address_type', 'address_types']:
54             self.api.db.do("DELETE FROM %s" \
55                            " WHERE address_type_id = %d" % \
56                            (table, self['address_type_id']), self)
57
58         if commit:
59             self.api.db.commit()
60         
61 class AddressTypes(Table):
62     """
63     Representation of the address_types table in the database.
64     """
65
66     def __init__(self, api, address_type_id_or_name_list = None):
67         sql = "SELECT %s FROM address_types" % \
68               ", ".join(AddressType.fields)
69         
70         if address_type_id_or_name_list:
71             # Separate the list into integers and strings
72             address_type_ids = filter(lambda address_type_id: isinstance(address_type_id, (int, long)),
73                                    address_type_id_or_name_list)
74             names = filter(lambda name: isinstance(name, StringTypes),
75                            address_type_id_or_name_list)
76             sql += " WHERE (False"
77             if address_type_ids:
78                 sql += " OR address_type_id IN (%s)" % ", ".join(map(str, address_type_ids))
79             if names:
80                 sql += " OR name IN (%s)" % ", ".join(api.db.quote(names))
81             sql += ")"
82
83         rows = api.db.selectall(sql)
84
85         for row in rows:
86             self[row['address_type_id']] = AddressType(api, row)