посылаем запрос в web-services API Joomla #8
This commit is contained in:
parent
4faa13edb8
commit
fc7173b9f4
29
cms/views.py
29
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)
|
Loading…
x
Reference in New Issue
Block a user