Files
zulip/zephyr/management/commands/dump_passwords.py
Tim Abbott 198480ef99 Use get_user_profile_by_email more consistently.
The previous situation was bad for two reasons:

(1) It had a lot of copies of the code, some of them missing pieces:
  UserProfile.objects.get(user__email__iexact=foo)

This was in particular going to be inconvenient since we are dropping
the __user part of that.

(2) It didn't take advantage of our memcached caching.

(imported from commit 2325795f288a7cf306cdae191f5d3080aac0651a)
2013-03-29 16:18:38 -04:00

33 lines
1.1 KiB
Python

from optparse import make_option
from django.core.management.base import BaseCommand
from zephyr.models import UserProfile, get_user_profile_by_email
import simplejson
def dump():
passwords = []
for user_profile in UserProfile.objects.all():
passwords.append((user_profile.user.email, user_profile.password))
file("dumped-passwords", "w").write(simplejson.dumps(passwords) + "\n")
def restore(change):
for (email, password) in simplejson.loads(file("dumped-passwords").read()):
try:
user_profile = get_user_profile_by_email(email)
except UserProfile.DoesNotExist:
print "Skipping...", email
continue
if change:
user_profile.user.password = password
user_profile.user.save()
class Command(BaseCommand):
option_list = BaseCommand.option_list + (
make_option('--restore', default=False, action='store_true'),
make_option('--dry-run', '-n', default=False, action='store_true'),)
def handle(self, *args, **options):
if options["restore"]:
restore(change=not options['dry_run'])
else:
dump()