- use base class __init__() and delete() implementations
[plcapi.git] / PLC / KeyTypes.py
1 #
2 # Functions for interacting with the key_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: KeyTypes.py,v 1.1 2006/10/10 22:09:31 mlhuang Exp $
8 #
9
10 from PLC.Faults import *
11 from PLC.Parameter import Parameter
12 from PLC.Table import Row, Table
13
14 class KeyType(Row):
15     """
16     Representation of a row in the key_types table. To use,
17     instantiate with a dict of values.
18     """
19
20     table_name = 'key_types'
21     primary_key = 'key_type'
22     join_tables = ['keys']
23     fields = {
24         'key_type': Parameter(str, "Key type", max = 20),
25         }
26
27     def validate_key_type(self, name):
28         # Remove leading and trailing spaces
29         name = name.strip()
30
31         # Make sure name is not blank after we removed the spaces
32         if not name:
33             raise PLCInvalidArgument, "Key type must be specified"
34         
35         # Make sure key type does not alredy exist
36         conflicts = KeyTypes(self.api, [name])
37         if conflicts:
38             raise PLCInvalidArgument, "Key type name already in use"
39
40         return name
41         
42 class KeyTypes(Table):
43     """
44     Representation of the key_types table in the database.
45     """
46
47     def __init__(self, api, names = None):
48         sql = "SELECT %s FROM key_types" % \
49               ", ".join(KeyType.fields)
50         
51         if names:
52             # Separate the list into integers and strings
53             sql += " WHERE key_type IN (%s)" % ", ".join(api.db.quote(names))
54
55         rows = api.db.selectall(sql)
56
57         for row in rows:
58             self[row['key_type']] = KeyType(api, row)