2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2012-12-13 21:48:45 +01:00
|
|
|
from optparse import make_option
|
|
|
|
from django.core.management.base import BaseCommand
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.models import UserProfile, get_user_profile_by_email
|
|
|
|
from zerver.lib.actions import do_change_password
|
2013-06-18 23:55:55 +02:00
|
|
|
import ujson
|
2012-12-13 21:48:45 +01:00
|
|
|
|
|
|
|
def dump():
|
|
|
|
passwords = []
|
2013-03-28 20:20:31 +01:00
|
|
|
for user_profile in UserProfile.objects.all():
|
2013-03-28 20:43:34 +01:00
|
|
|
passwords.append((user_profile.email, user_profile.password))
|
2013-06-18 23:55:55 +02:00
|
|
|
file("dumped-passwords", "w").write(ujson.dumps(passwords) + "\n")
|
2012-12-13 21:48:45 +01:00
|
|
|
|
|
|
|
def restore(change):
|
2013-06-18 23:55:55 +02:00
|
|
|
for (email, password) in ujson.loads(file("dumped-passwords").read()):
|
2012-12-14 00:55:06 +01:00
|
|
|
try:
|
2013-03-28 20:20:31 +01:00
|
|
|
user_profile = get_user_profile_by_email(email)
|
|
|
|
except UserProfile.DoesNotExist:
|
2012-12-14 00:55:06 +01:00
|
|
|
print "Skipping...", email
|
|
|
|
continue
|
2012-12-13 21:48:45 +01:00
|
|
|
if change:
|
2013-03-29 18:36:27 +01:00
|
|
|
do_change_password(user_profile, password, log=False,
|
|
|
|
hashed_password=True)
|
2012-12-13 21:48:45 +01:00
|
|
|
|
|
|
|
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()
|