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
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
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
|
2016-12-21 17:17:24 +01:00
|
|
|
from six.moves.configparser import ConfigParser
|
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):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
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")
|
2016-12-21 17:17:24 +01:00
|
|
|
config = ConfigParser()
|
2015-10-14 22:31:08 +02:00
|
|
|
with open(config_file, 'r') as f:
|
2016-12-21 17:17:24 +01:00
|
|
|
# Apparently, six.moves.configparser.ConfigParser is not
|
|
|
|
# consistent between Python 2 and 3!
|
|
|
|
if hasattr(config, 'read_file'):
|
|
|
|
config.read_file(f, config_file)
|
|
|
|
else:
|
|
|
|
config.readfp(f, config_file)
|
2013-05-29 17:39:10 +02:00
|
|
|
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,))
|