Files
zulip/zilencer/management/commands/sync_api_key.py
Anders Kaseorg c734bbd95d python: Modernize legacy Python 2 syntax with pyupgrade.
Generated by `pyupgrade --py3-plus --keep-percent-format` on all our
Python code except `zthumbor` and `zulip-ec2-configure-interfaces`,
followed by manual indentation fixes.

Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-09 16:43:22 -07:00

30 lines
1.1 KiB
Python

import os
from configparser import ConfigParser
from typing import Any
from django.core.management.base import BaseCommand
from zerver.models import UserProfile, get_realm, get_user_by_delivery_email
class Command(BaseCommand):
help = """Sync your API key from ~/.zuliprc into your development instance"""
def handle(self, *args: Any, **options: Any) -> None:
config_file = os.path.join(os.environ["HOME"], ".zuliprc")
if not os.path.exists(config_file):
raise RuntimeError("No ~/.zuliprc found")
config = ConfigParser()
with open(config_file) as f:
config.read_file(f, config_file)
api_key = config.get("api", "key")
email = config.get("api", "email")
try:
realm = get_realm("zulip")
user_profile = get_user_by_delivery_email(email, realm)
user_profile.api_key = api_key
user_profile.save(update_fields=["api_key"])
except UserProfile.DoesNotExist:
print("User %s does not exist; not syncing API key" % (email,))