diff options
Diffstat (limited to 'askbot/management/commands/merge_users.py')
-rw-r--r-- | askbot/management/commands/merge_users.py | 54 |
1 files changed, 35 insertions, 19 deletions
diff --git a/askbot/management/commands/merge_users.py b/askbot/management/commands/merge_users.py index 4691c4b3..3c7069e5 100644 --- a/askbot/management/commands/merge_users.py +++ b/askbot/management/commands/merge_users.py @@ -1,18 +1,12 @@ from django.core.management.base import CommandError, BaseCommand from askbot.models import User - -class Command(BaseCommand): +class MergeUsersBaseCommand(BaseCommand): args = '<from_user_id> <to_user_id>' help = 'Merge an account and all information from a <user_id> to a <user_id>, deleting the <from_user>' - def parse_arguments(self, *arguments): - if len(arguments) != 2: - raise CommandError('Arguments are <from_user_id> to <to_user_id>') - self.from_user = User.objects.get(id = arguments[0]) - self.to_user = User.objects.get(id = arguments[1]) - def handle(self, *arguments, **options): + self.parse_arguments(*arguments) for rel in User._meta.get_all_related_objects(): @@ -26,21 +20,23 @@ class Command(BaseCommand): self.process_m2m_field(rel.model, rel.field.name) except Exception, error: self.stdout.write(u'Warning: %s\n' % error) - - self.to_user.reputation += self.from_user.reputation - 1 - self.to_user.gold += self.from_user.gold - self.to_user.silver += self.from_user.silver - self.to_user.bronze += self.from_user.bronze - if self.from_user.last_seen > self.to_user.last_seen: - self.to_user.last_seen = self.from_user.last_seen + self.process_custom_user_fields() - if self.from_user.date_joined < self.to_user.date_joined: - self.to_user.date_joined = self.from_user.date_joined + self.cleanup() - self.to_user.save() + def cleanup(self): + raise Exception, 'Not implemented' + + def process_custom_user_fields(self): + """Put app specific logic here.""" + raise Exception, 'Not implemented' - self.from_user.delete() + def parse_arguments(self, *arguments): + if len(arguments) != 2: + raise CommandError('Arguments are <from_user_id> to <to_user_id>') + self.from_user = User.objects.get(id = arguments[0]) + self.to_user = User.objects.get(id = arguments[1]) def process_field(self, model, field_name): """reassigns the related object to the new user""" @@ -58,3 +54,23 @@ class Command(BaseCommand): m2m_field = getattr(obj, field_name) m2m_field.remove(self.from_user) m2m_field.add(self.to_user) + + +class Command(MergeUsersBaseCommand): + + def process_custom_user_fields(self): + self.to_user.reputation += self.from_user.reputation - 1 + self.to_user.gold += self.from_user.gold + self.to_user.silver += self.from_user.silver + self.to_user.bronze += self.from_user.bronze + + if self.from_user.last_seen > self.to_user.last_seen: + self.to_user.last_seen = self.from_user.last_seen + + if self.from_user.date_joined < self.to_user.date_joined: + self.to_user.date_joined = self.from_user.date_joined + + def cleanup(self): + self.to_user.save() + self.from_user.delete() + |