Реализована форма продвижения статьи #13

This commit is contained in:
Artur Galyamov 2022-12-19 10:47:31 +05:00
parent 2c1cdec1c2
commit c4a41df849
4 changed files with 36 additions and 6 deletions

5
cms/forms.py Normal file
View File

@ -0,0 +1,5 @@
from django.forms import ModelForm
class ArticleForm(ModelForm):
pass

View File

@ -0,0 +1,13 @@
<h1>Заполните данные статьи для продвижения в соц. сетях</h1>
<form
method="post"
enctype="application/x-www-form-urlencoded"
action="{% url 'create-article' %}"
>
{% csrf_token %}
<label for="body">Краткий текст статьи</label>
<textarea id="body" name="body"></textarea>
<label for="link">Ссылка</label>
<input type="text" name="link" id="link">
<input type="submit" value="Продвинуть">
</form>

View File

@ -1,7 +1,8 @@
from django.urls import path from django.urls import path
from .views import ArticleView from .views import ArticleView, new_article
urlpatterns = [ urlpatterns = [
path('articles/', ArticleView.as_view()) path('articles/', ArticleView.as_view(), name='create-article'),
path('articles/new/', new_article, name='new-article'),
] ]

View File

@ -2,9 +2,11 @@ import os
from json import JSONDecoder, JSONEncoder from json import JSONDecoder, JSONEncoder
import requests import requests
from django.http import JsonResponse from django.http import JsonResponse, HttpRequest
from django.shortcuts import render
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views import View from django.views import View
from django.urls import reverse
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from cms.models import Article from cms.models import Article
@ -69,11 +71,20 @@ class ArticleView(View):
gid='70000001426867', gid='70000001426867',
attachment=encoded_attachments) attachment=encoded_attachments)
def post(self, request): def post(self, request: HttpRequest):
article_data = JSONDecoder().decode(request.body.decode()) post_data = request.POST
article = Article.objects.create(**article_data) article = Article.objects.create(body=post_data['body'],
link=post_data['link'])
self._promote_to_telegram(article) self._promote_to_telegram(article)
self._promote_to_ok(article) self._promote_to_ok(article)
self._promote_to_vk(article) self._promote_to_vk(article)
response = {'ok': True} response = {'ok': True}
return JsonResponse(response) return JsonResponse(response)
def new_article(request):
return render(request,
template_name='articles/new.html',)