diff --git a/cms/management/commands/encode_file.py b/cms/management/commands/encode_file.py index 0ecba64..9c83f62 100644 --- a/cms/management/commands/encode_file.py +++ b/cms/management/commands/encode_file.py @@ -9,7 +9,7 @@ class Command(BaseCommand): environments = {} with open(BASE_DIR / '.env') as env_file: for line in env_file: - env_key, env_value = line.split('=') + env_key, env_value = line.strip().split('=') environments[env_key] = env_value signer = signing.Signer(salt=SALT) diff --git a/cms/promoters.py b/cms/promoters.py index 46e1ab9..1d0d7df 100644 --- a/cms/promoters.py +++ b/cms/promoters.py @@ -1,10 +1,10 @@ import abc -import os from urllib.parse import urlencode import requests from cms.models import Article +from crossposting_backend.settings import promoter_secrets class PromoteError(Exception): @@ -21,13 +21,13 @@ class Promoter(abc.ABC): class TelegramPromoter(Promoter): def promote(self): - from crossposting_backend.settings import BOT_TOKEN, CHANNEL_ID - long_text = f'{self.article.body}\n{self.article.link}' - querystring = (('chat_id', CHANNEL_ID), ('text', long_text)) + channel_id = promoter_secrets['TELEGRAM_CHAT_ID'] + bot_token = promoter_secrets['TELEGRAM_BOT_TOKEN'] + querystring = (('chat_id', channel_id), ('text', long_text)) encoded_querystring = urlencode(querystring) - send_message_url = f'https://api.telegram.org/bot{BOT_TOKEN}/sendMessage?{encoded_querystring}' + send_message_url = f'https://api.telegram.org/bot{bot_token}/sendMessage?{encoded_querystring}' response = requests.get(send_message_url) result = response.json() @@ -37,9 +37,9 @@ class TelegramPromoter(Promoter): class VkontaktePromoter(Promoter): def promote(self): - vk_login = os.getenv('VK_LOGIN') - vk_password = os.getenv('VK_PASSWORD') - vk_owner_id = os.getenv('VK_OWNER_ID') + vk_login = promoter_secrets['VK_LOGIN'] + vk_password = promoter_secrets['VK_PASSWORD'] + vk_owner_id = promoter_secrets['VK_OWNER_ID'] import vk_api session = vk_api.VkApi(login=vk_login, @@ -60,9 +60,9 @@ class OdnoklassnikiPromoter(Promoter): from json import JSONEncoder import ok_api - ok_access_token = os.getenv('OK_ACCESS_TOKEN') - ok_application_key = os.getenv('OK_APPLICATION_KEY') - ok_application_secret_key = os.getenv('OK_APPLICATION_SECRET_KEY') + ok_access_token = promoter_secrets['OK_ACCESS_TOKEN'] + ok_application_key = promoter_secrets['OK_APPLICATION_KEY'] + ok_application_secret_key = promoter_secrets['OK_APPLICATION_SECRET_KEY'] session = ok_api.OkApi(access_token=ok_access_token, application_key=ok_application_key, diff --git a/crossposting_backend/settings.py b/crossposting_backend/settings.py index b931c5c..1ae0c33 100644 --- a/crossposting_backend/settings.py +++ b/crossposting_backend/settings.py @@ -18,11 +18,27 @@ from django.core import signing from .private.settings import * + +def decode_env(env_key: str) -> str: + signer = signing.Signer(salt=SALT) + signed_telegram_chat_id_dict = getenv(env_key) + return signer.unsign_object(signed_telegram_chat_id_dict)[env_key] + + BASE_DIR = Path(__file__).resolve().parent.parent env_file = path.join(BASE_DIR, '.env') dotenv.read_dotenv(env_file) +promoter_env_keys = ( + 'TELEGRAM_BOT_TOKEN', 'TELEGRAM_CHAT_ID', 'JOOMLA_TOKEN', + 'VK_LOGIN', 'VK_PASSWORD', 'VK_OWNER_ID', 'OK_ACCESS_TOKEN', 'OK_APPLICATION_KEY', + 'OK_APPLICATION_SECRET_KEY', +) +promoter_secrets = {} +for promoter_env_key in promoter_env_keys: + promoter_secrets[promoter_env_key] = decode_env(promoter_env_key) + # Build paths inside the project like this: BASE_DIR / 'subdir'. # Quick-start development settings - unsuitable for production