From fc7173b9f4bd4a885e24f26bb973b7b6197acd5a Mon Sep 17 00:00:00 2001 From: Artur Galyamov Date: Fri, 9 Dec 2022 23:49:14 +0500 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D1=81=D1=8B=D0=BB=D0=B0=D0=B5?= =?UTF-8?q?=D0=BC=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=20=D0=B2=20web-se?= =?UTF-8?q?rvices=20API=20Joomla=20#8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cms/views.py | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/cms/views.py b/cms/views.py index 71b7c3d..fcadb18 100644 --- a/cms/views.py +++ b/cms/views.py @@ -1,4 +1,4 @@ -from json import JSONDecoder +from json import JSONDecoder, JSONEncoder import requests import os from django.http import JsonResponse @@ -11,7 +11,7 @@ from cms.models import Article @method_decorator(csrf_exempt, name='dispatch') class ArticleView(View): - def _send_to_channel(self, article: Article): + def _promote_to_channel(self, article: Article): bot_token = os.getenv('TELEGRAM_BOT_TOKEN') channel_id = os.getenv('TELEGRAM_CHAT_ID') @@ -26,9 +26,32 @@ class ArticleView(View): else: print('Похоже, нас послали доделывать приложение :-(') + def _promote_to_joomla(self, article: Article): + joomla_token = os.getenv('JOOMLA_TOKEN') + headers = { + 'X-Joomla-Token': joomla_token, + 'Content-Type': 'application/json' + } + article_json = { + "alias": article.title, + "articletext": article.body, + "catid": "8", + "language": "*", + "metadesc": "", + "metakey": "", + "title": article.title, + "state": 1 + } + response = requests.post('http://zv.mirokod.ru/api/index.php/v1/content/articles', + headers=headers, + data=JSONEncoder().encode(article_json)) + result = response.json() + print(result) + def post(self, request): article_data = JSONDecoder().decode(request.body.decode()) article = Article.objects.create(**article_data) - self._send_to_channel(article) + self._promote_to_channel(article) + self._promote_to_joomla(article) response = {'ok': True} return JsonResponse(response) \ No newline at end of file