aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/db/base.py
diff options
context:
space:
mode:
authorJessica Tallon <tsyesika@tsyesika.se>2016-01-12 11:41:21 +0000
committerJessica Tallon <tsyesika@tsyesika.se>2016-01-15 09:20:15 +0000
commiteebd0063feb19ea696f632dbb60588f0591c037b (patch)
treedc86a6276ec094f5c3e20dcc9e0a86b0f91f31c4 /mediagoblin/db/base.py
parentc17f755eeaf9447ecae3e32b9602ec19633f5108 (diff)
downloadmediagoblin-eebd0063feb19ea696f632dbb60588f0591c037b.tar.lz
mediagoblin-eebd0063feb19ea696f632dbb60588f0591c037b.tar.xz
mediagoblin-eebd0063feb19ea696f632dbb60588f0591c037b.zip
Fix #5382 - Add migration and fix so tombstones are removed from collections
When an item is deleted it should be removed from all collections, this commit makes that happen. It's got two changes: 1. Adds the code so when an object is soft deleted, it's automatically removed from all collection items 2. Add a migration to fix this issue for those who have tombstones (Graveyard objects) in their collections because of this bug. This commit requires you to run a migration
Diffstat (limited to 'mediagoblin/db/base.py')
-rw-r--r--mediagoblin/db/base.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/mediagoblin/db/base.py b/mediagoblin/db/base.py
index 11afbcec..af2337df 100644
--- a/mediagoblin/db/base.py
+++ b/mediagoblin/db/base.py
@@ -92,6 +92,31 @@ class GMGTableBase(object):
if deletion is None:
deletion = self.deletion_mode
+ # If the item is in any collection then it should be removed, this will
+ # cause issues if it isn't. See #5382.
+ # Import here to prevent cyclic imports.
+ from mediagoblin.db.models import CollectionItem, GenericModelReference
+
+ # Some of the models don't have an "id" field which means they can't be
+ # used with GMR, these models won't be in collections because they
+ # can't be. We can skip all of this.
+ if hasattr(self, "id"):
+ # First find the GenericModelReference for this object
+ gmr = GenericModelReference.query.filter_by(
+ obj_pk=self.id,
+ model_type=self.__tablename__
+ ).first()
+
+ # If there is no gmr then we've got lucky, a GMR is a requirement of
+ # being in a collection.
+ if gmr is not None:
+ items = CollectionItem.query.filter_by(
+ object_id=gmr.id
+ )
+
+ # Delete any CollectionItems found.
+ items.delete()
+
# Hand off to the correct deletion function.
if deletion == self.HARD_DELETE:
return self.hard_delete(commit=commit)
@@ -132,6 +157,7 @@ class GMGTableBase(object):
"model_type": tombstone.__tablename__,
})
+
# Now we can go ahead and actually delete the model.
return self.hard_delete(commit=commit)