Ticket #5308: 0001-Re-5308-added-delete_user-hook.patch

File 0001-Re-5308-added-delete_user-hook.patch, 2.7 KB (added by Robert, 7 years ago)
  • mediagoblin/db/models.py

    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, \  
    4343from mediagoblin.tools.files import delete_media_files
    4444from mediagoblin.tools.common import import_component
    4545from mediagoblin.tools.routing import extract_url_arguments
     46from mediagoblin.tools.pluginapi import hook_runall
    4647
    4748import six
    4849from six.moves.urllib.parse import urljoin
    class User(Base, UserMixin):  
    283284            # all related files/Comments
    284285            media.delete(del_orphan_tags=False, commit=False)
    285286
     287        #allow plugin-specific deletion
     288        _log.debug("Running all 'delete_user' hooks")
     289        hook_runall("delete_user",self.id)
     290
    286291        # Delete now unused tags
    287292        # TODO: import here due to cyclic imports!!! This cries for refactoring
    288293        from mediagoblin.db.util import clean_orphan_tags
  • mediagoblin/media_types/blog/__init__.py

    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  
    2323
    2424from mediagoblin.tools import pluginapi
    2525
     26from mediagoblin.db.base import Session
     27
    2628PLUGIN_DIR = os.path.dirname(__file__)
    2729MEDIA_TYPE = 'mediagoblin.media_types.blogpost'
    2830
    def add_to_user_home_context(context):  
    111113        context['blogs'] = None
    112114    return context
    113115
     116def 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
    114123
    115124hooks = {
    116125    'setup': setup_plugin,
    117126    ('media_manager', MEDIA_TYPE): lambda: BlogPostMediaManager,
    118127    # Inject blog context on user profile page
    119128    ("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
    121131}