Adds a new property to taggable which automatically deletes an empty tag.
authorDaniel Hans <daniel.m.hans@gmail.com>
Sun, 08 Nov 2009 12:08:05 -0800
changeset 3081 14077185c46e
parent 3080 30a33926ab91
child 3082 da8cc38cabe9
Adds a new property to taggable which automatically deletes an empty tag. Reviewed by: Lennard de Rijk
app/taggable/taggable.py
--- a/app/taggable/taggable.py	Sun Nov 08 11:54:44 2009 -0800
+++ b/app/taggable/taggable.py	Sun Nov 08 12:08:05 2009 -0800
@@ -16,6 +16,9 @@
   tagged_count = db.IntegerProperty(default=0)
   "The number of entities in tagged."
 
+  auto_delete = db.BooleanProperty(required=True, default=False)
+  "If true, a tag instance should be deleted when tagged_count reaches zero."
+
   @classmethod
   def __key_name(cls, tag_name):
     return cls.__name__ + '_' + tag_name
@@ -25,7 +28,10 @@
       if key in self.tagged:
         self.tagged.remove(key)
         self.tagged_count -= 1
-        self.put()
+        if not self.tagged_count and self.auto_delete:
+          self.delete()
+        else:
+          self.put()
     db.run_in_transaction(remove_tagged_txn)
     self.__class__.expire_cached_tags()
 
@@ -40,9 +46,12 @@
     
   def clear_tagged(self):
     def clear_tagged_txn():
-      self.tagged = []
-      self.tagged_count = 0
-      self.put()
+      if self.auto_delete:
+        self.delete()
+      else:
+        self.tagged = []
+        self.tagged_count = 0
+        self.put()
     db.run_in_transaction(clear_tagged_txn)
     self.__class__.expire_cached_tags()