e3db78185ab9b1e262fdc60dba249b0a5a3ba0c4
[plcapi.git] / PLC / NodeTypes.py
1 #
2 # Functions for interacting with the node_types table in the database
3 #
4 # $Id$
5 #
6
7 from PLC.Faults import *
8 from PLC.Parameter import Parameter
9 from PLC.Table import Row, Table
10
11 class NodeType(Row):
12     """
13     Representation of a row in the node_types table. To use,
14     instantiate with a dict of values.
15     """
16
17     table_name = 'node_types'
18     primary_key = 'node_type'
19     join_tables = ['nodes']
20     fields = {
21         'node_type': Parameter(str, "Node type", max = 20),
22         }
23
24     def validate_node_type(self, name):
25         # Make sure name is not blank
26         if not len(name):
27             raise PLCInvalidArgument, "Node type must be specified"
28         
29         # Make sure node type does not alredy exist
30         conflicts = NodeTypes(self.api, [name])
31         if conflicts:
32             raise PLCInvalidArgument, "Node type name already in use"
33
34         return name
35
36 class NodeTypes(Table):
37     """
38     Representation of the node_types table in the database.
39     """
40
41     def __init__(self, api, node_types = None):
42         Table.__init__(self, api, NodeType)
43
44         sql = "SELECT %s FROM node_types" % \
45               ", ".join(NodeType.fields)
46         
47         if node_types:
48             sql += " WHERE node_type IN (%s)" % ", ".join(map(api.db.quote, node_types))
49
50         self.selectall(sql)