← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~ilasc/launchpad:skip-merged-teamowner into launchpad:master

 

Ioana Lasc has proposed merging ~ilasc/launchpad:skip-merged-teamowner into launchpad:master.

Commit message:
Skip merged Person.teamowner in close-account script

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~ilasc/launchpad/+git/launchpad/+merge/395981
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~ilasc/launchpad:skip-merged-teamowner into launchpad:master.
diff --git a/lib/lp/registry/scripts/closeaccount.py b/lib/lp/registry/scripts/closeaccount.py
index 6256ea2..2832e11 100644
--- a/lib/lp/registry/scripts/closeaccount.py
+++ b/lib/lp/registry/scripts/closeaccount.py
@@ -178,6 +178,19 @@ def close_account(username, log):
         # can hopefully provide a garbo job for this eventually.
         ('revisionauthor', 'person'),
         }
+
+    # If all the teams that the user owns
+    # have been deleted (not just one) skip Person.teamowner
+    merged = True
+    persons = list(
+        store.find(Person,
+                   Person.teamownerID == person.id))
+    for person_team_owner in persons:
+        if not ('merged' in person_team_owner.name):
+            merged = False
+    if merged:
+        skip.add(('person', 'teamowner'))
+
     reference_names = {
         (src_tab, src_col) for src_tab, src_col, _, _, _, _ in references}
     for src_tab, src_col in skip:
diff --git a/lib/lp/registry/scripts/tests/test_closeaccount.py b/lib/lp/registry/scripts/tests/test_closeaccount.py
index 32e6531..62d7d8c 100644
--- a/lib/lp/registry/scripts/tests/test_closeaccount.py
+++ b/lib/lp/registry/scripts/tests/test_closeaccount.py
@@ -635,6 +635,27 @@ class TestCloseAccount(TestCaseWithFactory):
             self.runScript(script)
         self.assertRemoved(account_id, person_id)
 
+    def test_skips_teamowner_merged(self):
+        person = self.factory.makePerson()
+        owned_team1 = self.factory.makeTeam(name='target', owner=person)
+        owned_team1.name = 'target-merged'
+        owned_team2 = self.factory.makeTeam(name='target2', owner=person)
+        person_id = person.id
+        account_id = person.account.id
+        script = self.makeScript([six.ensure_str(person.name)])
+
+        # Closing account fails as the user still owns team2
+        with dbuser('launchpad'):
+            self.assertRaises(
+                LaunchpadScriptFailure, self.runScript, script)
+
+        # Account will now close as the user doesn't own
+        # any other teams at this point
+        owned_team2.name = 'target2-merged'
+        with dbuser('launchpad'):
+            self.runScript(script)
+        self.assertRemoved(account_id, person_id)
+
     def test_handles_login_token(self):
         person = self.factory.makePerson()
         email = '%s@xxxxxxxxxxxxxxxxxxx' % person.name