You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2015/11/25 20:38:06 UTC

[12/17] allura git commit: [#7999] ticket:861 Move purge_project to ProjectRegistrationProvider

[#7999] ticket:861 Move purge_project to ProjectRegistrationProvider


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/d360404c
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/d360404c
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/d360404c

Branch: refs/heads/master
Commit: d360404cf49ba78ab7c2c23922ecf17bf82316de
Parents: e6fceed
Author: Igor Bondarenko <je...@gmail.com>
Authored: Mon Nov 16 14:47:56 2015 +0200
Committer: Dave Brondsema <da...@brondsema.net>
Committed: Wed Nov 25 14:37:42 2015 -0500

----------------------------------------------------------------------
 Allura/allura/lib/plugin.py                 | 42 ++++++++++++++++++++-
 Allura/allura/scripts/delete_projects.py    | 48 ++----------------------
 Allura/allura/tests/test_delete_projects.py |  7 ++--
 3 files changed, 47 insertions(+), 50 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/d360404c/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index eb19152..91c926f 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -44,17 +44,18 @@ import pkg_resources
 import tg
 from tg import config, request, redirect, response
 from pylons import tmpl_context as c, app_globals as g
-from webob import exc
+from webob import exc, Request
 from paste.deploy.converters import asbool, asint
 
 from ming.utils import LazyProperty
 from ming.orm import state
-from ming.orm import ThreadLocalORMSession, session
+from ming.orm import ThreadLocalORMSession, session, Mapper
 
 from allura.lib import helpers as h
 from allura.lib import security
 from allura.lib import exceptions as forge_exc
 from allura.lib import utils
+from allura.tasks.index_tasks import solr_del_project_artifacts
 
 log = logging.getLogger(__name__)
 
@@ -989,6 +990,43 @@ class ProjectRegistrationProvider(object):
         for sp in project.subprojects:
             self.undelete_project(sp, user)
 
+    def purge_project(self, project, disable_users=False, reason=None):
+        from allura.model import AppConfig
+        pid = project._id
+        solr_del_project_artifacts.post(pid)
+        if disable_users:
+            # Disable users if necessary BEFORE removing all project-related documents
+            self.disable_project_users(project, reason)
+        app_config_ids = [ac._id for ac in AppConfig.query.find(dict(project_id=pid))]
+        for m in Mapper.all_mappers():
+            mcls = m.mapped_class
+            if 'project_id' in m.property_index:
+                # Purge the things directly related to the project
+                mcls.query.remove(dict(project_id=pid))
+            elif 'app_config_id' in m.property_index:
+                # ... and the things related to its apps
+                mcls.query.remove(dict(app_config_id={'$in': app_config_ids}))
+        project.delete()
+        session(project).flush()
+        g.post_event('project_deleted', project_id=pid, reason=reason)
+
+    def disable_project_users(self, project, reason=None):
+        from allura.model import AuditLog
+        provider = AuthenticationProvider.get(Request.blank('/'))
+        users = project.admins() + project.users_with_role('Developer')
+        for user in users:
+            if user.disabled:
+                continue
+            provider.disable_user(user, audit=False)
+            msg = u'Account disabled because project {}{} is deleted. Reason: {}'.format(
+                project.neighborhood.url_prefix,
+                project.shortname,
+                reason)
+            AuditLog.log_user(msg, user=user)
+            # `users` can contain duplicates. Make sure changes are visible
+            # to next iterations, so that `user.disabled` check works.
+            session(user).expunge(user)
+
     def best_download_url(self, project):
         '''This is the url needed to render a download button.
            It should be overridden for your specific envirnoment'''

http://git-wip-us.apache.org/repos/asf/allura/blob/d360404c/Allura/allura/scripts/delete_projects.py
----------------------------------------------------------------------
diff --git a/Allura/allura/scripts/delete_projects.py b/Allura/allura/scripts/delete_projects.py
index 1c53b76..6c83c9b 100644
--- a/Allura/allura/scripts/delete_projects.py
+++ b/Allura/allura/scripts/delete_projects.py
@@ -18,14 +18,9 @@
 import argparse
 import logging
 
-from ming.odm import Mapper, session
-from pylons import app_globals as g
-from webob import Request
-
 from allura.scripts import ScriptTask
 from allura import model as M
-from allura.lib.plugin import AuthenticationProvider
-from allura.tasks.index_tasks import solr_del_project_artifacts
+from allura.lib.plugin import ProjectRegistrationProvider
 
 
 log = logging.getLogger(__name__)
@@ -35,11 +30,12 @@ class DeleteProjects(ScriptTask):
 
     @classmethod
     def execute(cls, options):
+        provider = ProjectRegistrationProvider.get()
         for proj in options.projects:
             proj = cls.get_project(proj)
             if proj:
                 log.info('Purging %s%s. Reason: %s', proj.neighborhood.url_prefix, proj.shortname, options.reason)
-                cls.purge_project(proj, options)
+                provider.purge_project(proj, disable_users=options.disable_users, reason=options.reason)
 
     @classmethod
     def get_project(cls, proj):
@@ -55,44 +51,6 @@ class DeleteProjects(ScriptTask):
         return p
 
     @classmethod
-    def purge_project(cls, project, options):
-        pid = project._id
-        solr_del_project_artifacts.post(pid)
-        if options.disable_users:
-            # Disable users if necessary BEFORE removing all project-related documents
-            cls.disable_users(project, options)
-        app_config_ids = [ac._id for ac in M.AppConfig.query.find(dict(project_id=pid))]
-        for m in Mapper.all_mappers():
-            mcls = m.mapped_class
-            if 'project_id' in m.property_index:
-                # Purge the things directly related to the project
-                mcls.query.remove(dict(project_id=pid))
-            elif 'app_config_id' in m.property_index:
-                # ... and the things related to its apps
-                mcls.query.remove(dict(app_config_id={'$in': app_config_ids}))
-        project.delete()
-        session(project).flush()
-        g.post_event('project_deleted', project_id=pid, reason=options.reason)
-
-    @classmethod
-    def disable_users(cls, project, options):
-        provider = AuthenticationProvider.get(Request.blank('/'))
-        users = project.admins() + project.users_with_role('Developer')
-        for user in users:
-            if user.disabled:
-                continue
-            log.info(u'Disabling user %s', user.username)
-            provider.disable_user(user, audit=False)
-            msg = u'Account disabled because project {}{} is deleted. Reason: {}'.format(
-                project.neighborhood.url_prefix,
-                project.shortname,
-                options.reason)
-            M.AuditLog.log_user(msg, user=user)
-            # `users` can contain duplicates. Make sure changes are visible
-            # to next iterations, so that `user.disabled` check works.
-            session(user).expunge(user)
-
-    @classmethod
     def parser(cls):
         parser = argparse.ArgumentParser(description='Completely delete projects')
         parser.add_argument('projects', metavar='nbhd/project', type=str, nargs='+',

http://git-wip-us.apache.org/repos/asf/allura/blob/d360404c/Allura/allura/tests/test_delete_projects.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_delete_projects.py b/Allura/allura/tests/test_delete_projects.py
index 784a5b9..6eab1fe 100644
--- a/Allura/allura/tests/test_delete_projects.py
+++ b/Allura/allura/tests/test_delete_projects.py
@@ -23,6 +23,7 @@ from alluratest.controller import TestController
 from allura.tests.decorators import audits, out_audits
 from allura import model as M
 from allura.scripts import delete_projects
+from allura.lib import plugin
 
 
 class TestDeleteProjects(TestController):
@@ -69,19 +70,19 @@ class TestDeleteProjects(TestController):
         things = self.things_related_to_project(pid)
         assert len(things) == 0, 'Not all things are deleted: %s' % things
 
-    @patch('allura.scripts.delete_projects.solr_del_project_artifacts', autospec=True)
+    @patch('allura.lib.plugin.solr_del_project_artifacts', autospec=True)
     def test_solr_index_is_deleted(self, del_solr):
         pid = M.Project.query.get(shortname=self.p_shortname)._id
         self.run_script(['p/{}'.format(self.p_shortname)])
         del_solr.post.assert_called_once_with(pid)
 
-    @patch.object(delete_projects.g, 'post_event', autospec=True)
+    @patch.object(plugin.g, 'post_event', autospec=True)
     def test_event_is_fired(self, post_event):
         pid = M.Project.query.get(shortname=self.p_shortname)._id
         self.run_script(['p/{}'.format(self.p_shortname)])
         post_event.assert_called_once_with('project_deleted', project_id=pid, reason=None)
 
-    @patch.object(delete_projects.g, 'post_event', autospec=True)
+    @patch.object(plugin.g, 'post_event', autospec=True)
     @patch('allura.scripts.delete_projects.log', autospec=True)
     def test_delete_with_reason(self, log, post_event):
         p = M.Project.query.get(shortname=self.p_shortname)