Fix KeyTypes, Keys
[plcapi.git] / PLC / KeyTypes.py
index e491b45..a3b048b 100644 (file)
@@ -4,51 +4,56 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id$
-# $URL$
-#
 
 from PLC.Faults import *
 from PLC.Parameter import Parameter
-from PLC.Table import Row, Table
+from PLC.Storage.AlchemyObject import AlchemyObj
 
-class KeyType(Row):
+class KeyType(AlchemyObj):
     """
     Representation of a row in the key_types table. To use,
     instantiate with a dict of values.
     """
 
-    table_name = 'key_types'
-    primary_key = 'key_type'
-    join_tables = ['keys']
+    tablename = 'key_types'
     fields = {
-        'key_type': Parameter(str, "Key type", max = 20),
+        'key_type': Parameter(str, "Key type", max = 20, primary_key=True),
         }
 
     def validate_key_type(self, name):
-       # Make sure name is not blank
+        # Make sure name is not blank
         if not len(name):
             raise PLCInvalidArgument, "Key type must be specified"
-       
-       # Make sure key type does not alredy exist
-       conflicts = KeyTypes(self.api, [name])
+
+        # Make sure key type does not alredy exist
+        conflicts = KeyType().select(filter={'key_type': name})
         if conflicts:
             raise PLCInvalidArgument, "Key type name already in use"
 
-       return name
-        
-class KeyTypes(Table):
+        return name
+
+    def sync(self, commit = True, validate=True):
+        AlchemyObj.sync(self, commit=commit, validate=validate)
+        AlchemyObj.insert(self, dict(self))
+
+    def delete(self, commit=True):
+        """
+        Delete existing key type
+        """
+        assert 'key_type' in self
+        AlchemyObj.delete(self, filter={'key_type': self['key_type']})  
+
+class KeyTypes(list):
     """
     Representation of the key_types table in the database.
     """
 
     def __init__(self, api, key_types = None):
-        Table.__init__(self, api, KeyType)
-
-        sql = "SELECT %s FROM key_types" % \
-              ", ".join(KeyType.fields)
-        
-        if key_types:
-            sql += " WHERE key_type IN (%s)" % ", ".join(map(api.db.quote, key_types))
-
-        self.selectall(sql)
+       if key_types:
+            key_types = KeyType().select(filter={'key_type': key_types})
+        else:
+            key_types = KeyType().select()
+
+        for key_type in key_types:
+            key_type = KeyType(api, object=key_type)
+            self.append(key_type)