Fix KeyTypes, Keys
[plcapi.git] / PLC / KeyTypes.py
index df15643..a3b048b 100644 (file)
@@ -7,19 +7,17 @@
 
 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):
@@ -28,24 +26,34 @@ class KeyType(Row):
             raise PLCInvalidArgument, "Key type must be specified"
 
         # Make sure key type does not alredy exist
-        conflicts = KeyTypes(self.api, [name])
+        conflicts = KeyType().select(filter={'key_type': name})
         if conflicts:
             raise PLCInvalidArgument, "Key type name already in use"
 
         return name
 
-class KeyTypes(Table):
+    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( [ api.db.quote (t) for t in 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)