aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Spaeth <Sebastian@SSpaeth.de>2012-11-28 15:46:40 +0100
committerSebastian Spaeth <Sebastian@SSpaeth.de>2013-01-17 11:48:49 +0100
commit03b4fc500cf490fb56b6dba8507d560dffcd2a8b (patch)
tree0f2fdfad991048a61586edb1cdb220737f3ec925
parent242776e3637e886c510cd39d4d6195932bb973cd (diff)
downloadmediagoblin-03b4fc500cf490fb56b6dba8507d560dffcd2a8b.tar.lz
mediagoblin-03b4fc500cf490fb56b6dba8507d560dffcd2a8b.tar.xz
mediagoblin-03b4fc500cf490fb56b6dba8507d560dffcd2a8b.zip
Implement User.delete() (#540)
Set User.collections to her Collections using the backref feature. This way we can iterate a user's collections and delete them all. Delete all MediaEntries/Files/attachments/comments/collections etc before finally deleting the User object. This is the backend work for issue 302 (allow a user to delete ones own account)
-rw-r--r--mediagoblin/db/models.py23
1 files changed, 22 insertions, 1 deletions
diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py
index e8616404..1c6cbda7 100644
--- a/mediagoblin/db/models.py
+++ b/mediagoblin/db/models.py
@@ -81,6 +81,27 @@ class User(Base, UserMixin):
'admin' if self.is_admin else 'user',
self.username)
+ 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)
+
+ media_entries = MediaEntry.query.filter(MediaEntry.uploader == self.id)
+ for media in media_entries:
+ # TODO: Make sure that "MediaEntry.delete()" also deletes
+ # all related files/Comments
+ media.delete(del_orphan_tags=False, commit=False)
+
+ # Delete now unused tags
+ # TODO: import here due to cyclic imports!!! This cries for refactoring
+ from mediagoblin.db.sql.util import clean_orphan_tags
+ clean_orphan_tags(commit=False)
+
+ # Delete user, pass through commit=False/True in kwargs
+ super(User, self).delete(**kwargs)
+ _log.info('Deleted user "{0}" account'.format(self.username))
+
class MediaEntry(Base, MediaEntryMixin):
"""
@@ -393,7 +414,7 @@ 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)
+ get_creator = relationship(User, backref="collections")
def get_collection_items(self, ascending=False):
#TODO, is this still needed with self.collection_items being available?