Repository: translate/pootle

Issue:
merge_user should also remove the old user
Currently `merge_user` does not actually remove the old user.  You need to run `purge_user` following `merge_user` to completely remove the user.  This is dangerous and error prone, especially on older instances that have a large number of users.



Discussion:
Currently I've scripted the following to work around this problem:

``` bash
#!/bin/bash

if [ $# -ne 2 ]; then
        echo "$(basename $0) from_user to_user"
        exit 1
fi

from_user=$1
to_user=$2

./manage.py merge_user $from_user $to_user && ./manage.py purge_user $from_user
```

@dwaynebailey i didnt add this because i can see a situation where you dont want to delete the user when merging.

How about we add a "--delete" flag to delete when merge has completed?

@phlax could you elaborate the situation where you don't want to delete the user?  I'd do the inverse of `--delete`, that is by default purge the $from user and with an option, don't delete her.  I think the use case to remove the user is much more likely than the other.  Though I can't think of a reason to keep the user if you've just removed all their contributions.

+1 that this is an issue. By default, the merged user should be deleted. You may want to add an option _not_ to delete the user, like `--leave`, but I'm not sure in which situations this will be useful. Mind elaborating?

Haha, wrote the same thing as @dwaynebailey )

hi @iafan @dwaynebailey - the use case i imagined is that you want to copy over profile info or perhaps permissions (also maybe alt_src_languages?) - as these are not currently dealt with.

The other use case i had in mind was in relation to an idea that @dwaynebailey mentioned of being able to set times between which you want to merge data - eg if someone had been borrowing someone elses account.

I can see the consensus is with making deletion the default option so ill add that in - and i reckon add a `--no-delete` option to keep the user object


Resolving patch:
diff --git a/pootle/apps/accounts/management/commands/merge_user.py b/pootle/apps/accounts/management/commands/merge_user.py
--- a/pootle/apps/accounts/management/commands/merge_user.py
+++ b/pootle/apps/accounts/management/commands/merge_user.py
@@ -7,6 +7,8 @@
 # or later license. See the LICENSE file for a copy of the license and the
 # AUTHORS file for copyright and authorship information.
 
+from optparse import make_option
+
 import accounts
 
 from . import UserCommand
@@ -15,8 +17,22 @@
 class Command(UserCommand):
     args = "user other_user"
     help = "Merge user to other_user"
+    shared_option_list = (
+        make_option("--no-delete",
+                    dest='delete',
+                    action="store_false",
+                    default=True,
+                    help="Don't delete user after merging."),
+    )
+    option_list = UserCommand.option_list + shared_option_list
 
     def handle(self, *args, **kwargs):
         super(Command, self).handle(*args, **kwargs)
-        accounts.utils.UserMerger(self.get_user(username=args[0]),
+        src_user = self.get_user(username=args[0])
+        accounts.utils.UserMerger(src_user,
                                   self.get_user(username=args[1])).merge()
+
+        if kwargs.get("delete"):
+            self.stdout.write("Deleting user: %s...\n" % src_user.username)
+            src_user.delete()
+            self.stdout.write("User deleted: %s\n" % src_user.username)


Test patch: