Use better relationships to delete collections.
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Tue, 22 Jan 2013 21:00:41 +0000 (22:00 +0100)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Wed, 23 Jan 2013 16:36:03 +0000 (17:36 +0100)
When deleting a User, his/her collections can be deleted by
sqlalchemy: Collections do not need any special code to be
executed on deletion.

mediagoblin/db/models.py

index 7e2cc7d20dd831a14ca0172d0e18f2721dcbe1c6..de491e966d008b568c8c13c0dcd29461cef4117f 100644 (file)
@@ -84,9 +84,7 @@ class User(Base, UserMixin):
 
     def delete(self, **kwargs):
         """Deletes a User and all related entries/comments/files/..."""
-        # Delete this user's Collections and all contained CollectionItems
-        for collection in self.collections:
-            collection.delete(commit=False)
+        # Collections get deleted by relationships.
 
         media_entries = MediaEntry.query.filter(MediaEntry.uploader == self.id)
         for media in media_entries:
@@ -415,7 +413,10 @@ class Collection(Base, CollectionMixin):
     # TODO: No of items in Collection. Badly named, can we migrate to num_items?
     items = Column(Integer, default=0)
 
-    get_creator = relationship(User, backref="collections")
+    # Cascade: Collections are owned by their creator. So do the full thing.
+    get_creator = relationship(User,
+                               backref=backref("collections",
+                                               cascade="all, delete-orphan"))
 
     def get_collection_items(self, ascending=False):
         #TODO, is this still needed with self.collection_items being available?
@@ -436,7 +437,9 @@ class CollectionItem(Base, CollectionItemMixin):
     note = Column(UnicodeText, nullable=True)
     added = Column(DateTime, nullable=False, default=datetime.datetime.now)
     position = Column(Integer)
-    in_collection = relationship("Collection",
+
+    # Cascade: CollectionItems are owned by their Collection. So do the full thing.
+    in_collection = relationship(Collection,
                                  backref=backref(
                                      "collection_items",
                                      cascade="all, delete-orphan"))