From 2d0a5dbf3061da6ae8e2dfc7950dc2304615981b Mon Sep 17 00:00:00 2001
From: Robert Smith <smithrobertlawrence@gmail.com>
Date: Mon, 12 Jun 2017 21:32:58 -0400
Subject: [PATCH] Re #5308 added 'delete_user' hook
added hook callback in blog plugin's __init__.py
deletes blogs on user deletion
---
mediagoblin/db/models.py | 5 +++++
mediagoblin/media_types/blog/__init__.py | 12 +++++++++++-
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py
index f4644b9f..1b183df2 100644
a
|
b
|
from mediagoblin.db.mixin import UserMixin, MediaEntryMixin, \
|
43 | 43 | from mediagoblin.tools.files import delete_media_files |
44 | 44 | from mediagoblin.tools.common import import_component |
45 | 45 | from mediagoblin.tools.routing import extract_url_arguments |
| 46 | from mediagoblin.tools.pluginapi import hook_runall |
46 | 47 | |
47 | 48 | import six |
48 | 49 | from six.moves.urllib.parse import urljoin |
… |
… |
class User(Base, UserMixin):
|
283 | 284 | # all related files/Comments |
284 | 285 | media.delete(del_orphan_tags=False, commit=False) |
285 | 286 | |
| 287 | #allow plugin-specific deletion |
| 288 | _log.debug("Running all 'delete_user' hooks") |
| 289 | hook_runall("delete_user",self.id) |
| 290 | |
286 | 291 | # Delete now unused tags |
287 | 292 | # TODO: import here due to cyclic imports!!! This cries for refactoring |
288 | 293 | from mediagoblin.db.util import clean_orphan_tags |
diff --git a/mediagoblin/media_types/blog/__init__.py b/mediagoblin/media_types/blog/__init__.py
index 859c2162..67df9cc9 100644
a
|
b
|
from mediagoblin.media_types.blog.models import Blog, BlogPostData
|
23 | 23 | |
24 | 24 | from mediagoblin.tools import pluginapi |
25 | 25 | |
| 26 | from mediagoblin.db.base import Session |
| 27 | |
26 | 28 | PLUGIN_DIR = os.path.dirname(__file__) |
27 | 29 | MEDIA_TYPE = 'mediagoblin.media_types.blogpost' |
28 | 30 | |
… |
… |
def add_to_user_home_context(context):
|
111 | 113 | context['blogs'] = None |
112 | 114 | return context |
113 | 115 | |
| 116 | def delete_user_hook(user_id): |
| 117 | _log.debug("Running blog deletion hook on user with id {0}".format(user_id)) |
| 118 | blogs = Session.query(Blog).filter(Blog.author == user_id) |
| 119 | for blog in blogs: |
| 120 | #SQLAlchemy will automatically delete blogposts that are connected to the deleted blogs |
| 121 | blog.delete() |
| 122 | |
114 | 123 | |
115 | 124 | hooks = { |
116 | 125 | 'setup': setup_plugin, |
117 | 126 | ('media_manager', MEDIA_TYPE): lambda: BlogPostMediaManager, |
118 | 127 | # Inject blog context on user profile page |
119 | 128 | ("mediagoblin.user_pages.user_home", |
120 | | "mediagoblin/user_pages/user.html"): add_to_user_home_context |
| 129 | "mediagoblin/user_pages/user.html"): add_to_user_home_context, |
| 130 | "delete_user": delete_user_hook |
121 | 131 | } |