2017-11-16 00:55:49 +01:00
|
|
|
import os
|
|
|
|
from configparser import ConfigParser
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2017-11-16 00:55:49 +01:00
|
|
|
|
2024-05-24 16:49:56 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2023-12-15 02:14:24 +01:00
|
|
|
from zerver.models import UserProfile
|
|
|
|
from zerver.models.realms import get_realm
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models.users import get_user_by_delivery_email
|
2013-05-29 17:39:10 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2024-05-24 16:49:56 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
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
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-10-27 12:57:54 +02:00
|
|
|
def handle(self, *args: Any, **options: 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()
|
2020-04-09 21:51:58 +02:00
|
|
|
with open(config_file) as f:
|
2017-11-06 03:10:47 +01:00
|
|
|
config.read_file(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:
|
2017-07-18 19:39:48 +02:00
|
|
|
realm = get_realm("zulip")
|
2018-12-07 00:05:57 +01:00
|
|
|
user_profile = get_user_by_delivery_email(email, realm)
|
2015-10-13 21:44:45 +02:00
|
|
|
user_profile.api_key = api_key
|
|
|
|
user_profile.save(update_fields=["api_key"])
|
|
|
|
except UserProfile.DoesNotExist:
|
2020-06-10 06:41:04 +02:00
|
|
|
print(f"User {email} does not exist; not syncing API key")
|