2013-05-29 17:39:10 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-05-29 17:39:10 +02:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
2015-10-13 21:44:45 +02:00
|
|
|
from zerver.models import get_user_profile_by_email, UserProfile
|
2013-05-29 17:39:10 +02:00
|
|
|
import os
|
2015-11-01 17:14:36 +01:00
|
|
|
from six.moves.configparser import SafeConfigParser
|
2013-05-29 17:39:10 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2015-10-13 21:44:45 +02:00
|
|
|
help = """Sync your API key from ~/.zuliprc into your development instance"""
|
2013-05-29 17:39:10 +02:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2013-08-07 17:36:46 +02:00
|
|
|
config_file = os.path.join(os.environ["HOME"], ".zuliprc")
|
2013-05-29 17:39:10 +02:00
|
|
|
if not os.path.exists(config_file):
|
2013-08-07 17:36:46 +02:00
|
|
|
raise RuntimeError("No ~/.zuliprc found")
|
2013-05-29 17:39:10 +02:00
|
|
|
config = SafeConfigParser()
|
2015-10-14 22:31:08 +02:00
|
|
|
with open(config_file, 'r') as f:
|
2013-05-29 17:39:10 +02:00
|
|
|
config.readfp(f, config_file)
|
|
|
|
api_key = config.get("api", "key")
|
|
|
|
email = config.get("api", "email")
|
|
|
|
|
2015-10-13 21:44:45 +02:00
|
|
|
try:
|
|
|
|
user_profile = get_user_profile_by_email(email)
|
|
|
|
user_profile.api_key = api_key
|
|
|
|
user_profile.save(update_fields=["api_key"])
|
|
|
|
except UserProfile.DoesNotExist:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("User %s does not exist; not syncing API key" % (email,))
|