assert primary key exists in record before attempting to delete it
[plcapi.git] / PLC / PersonTags.py
index 30869af..5581a69 100644 (file)
@@ -1,26 +1,22 @@
-# $Id$
-# $URL$
 #
 # Thierry Parmentelat - INRIA
 #
 from PLC.Faults import *
 from PLC.Parameter import Parameter
-from PLC.Filter import Filter
-from PLC.Table import Row, Table
+from PLC.Storage.AlchemyObject import AlchemyObj
 from PLC.TagTypes import TagType, TagTypes
 from PLC.Persons import Person
 
-class PersonTag(Row):
+class PersonTag(AlchemyObj):
     """
     Representation of a row in the person_tag.
     To use, instantiate with a dict of values.
     """
 
-    table_name = 'person_tag'
-    primary_key = 'person_tag_id'
+    tablename = 'person_tag'
     fields = {
-        'person_tag_id': Parameter(int, "Person setting identifier"),
-        'person_id': Person.fields['person_id'],
+        'person_tag_id': Parameter(int, "Person setting identifier", primary_key=True),
+        'person_id': Person.fields['id'],
         'email': Person.fields['email'],
         'tag_type_id': TagType.fields['tag_type_id'],
         'tagname': TagType.fields['tagname'],
@@ -31,26 +27,32 @@ class PersonTag(Row):
 
         }
 
-class PersonTags(Table):
+    def sync(self, commit=True, validate=True):
+        AlchemyObj.sync(self, commit=Ture, validate=True)
+        if 'person_tag_id' not in self:
+            AlchemyObj.insert(self, dict(self))
+        else:
+            AlchemyObj.update(self, {'person_tag_id': self['person_tag_id']}, dict(self))
+            
+        
+    def delete(self, commit=True):
+        assert 'person_tag_id' in self
+        AlchemyObj.delete(self, dict(self))
+
+class PersonTags(list):
     """
     Representation of row(s) from the person_tag table in the
     database.
     """
 
     def __init__(self, api, person_tag_filter = None, columns = None):
-        Table.__init__(self, api, PersonTag, columns)
-
-        sql = "SELECT %s FROM view_person_tags WHERE True" % \
-              ", ".join(self.columns)
-
-        if person_tag_filter is not None:
-            if isinstance(person_tag_filter, (list, tuple, set, int, long)):
-                person_tag_filter = Filter(PersonTag.fields, {'person_tag_id': person_tag_filter})
-            elif isinstance(person_tag_filter, dict):
-                person_tag_filter = Filter(PersonTag.fields, person_tag_filter)
-            else:
-                raise PLCInvalidArgument, "Wrong person setting filter %r"%person_tag_filter
-            sql += " AND (%s) %s" % person_tag_filter.sql(api)
-
-
-        self.selectall(sql)
+        if not person_tag_filter:
+            person_tags = PersonTag().select()
+        elif isinstance(person_tag_filter, (list, tuple, set, int, long)):
+            person_tags = PersonTag().select(filter={'person_tag_id': person_tag_filter})
+        elif isinstance(person_tag_filter, dict):
+            person_tags = PersonTag().select(filter=person_tag_filter)
+        else:
+            raise PLCInvalidArgument, "Wrong person setting filter %r"%person_tag_filter
+
+        self.extend(person_tags)