|
|
@ -3,31 +3,52 @@ from django.contrib.auth import authenticate, login |
|
|
|
from django.contrib.auth.decorators import login_required |
|
|
|
from django.contrib.auth.decorators import login_required |
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin |
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin |
|
|
|
from django.http import HttpRequest, HttpResponseRedirect |
|
|
|
from django.http import HttpRequest, HttpResponseRedirect |
|
|
|
from django.shortcuts import render |
|
|
|
from django.shortcuts import render, redirect |
|
|
|
from django.urls import reverse |
|
|
|
from django.urls import reverse |
|
|
|
from django.views import View |
|
|
|
from django.views import View |
|
|
|
|
|
|
|
from requests import request |
|
|
|
|
|
|
|
|
|
|
|
from cms import promoters |
|
|
|
from cms import promoters |
|
|
|
from cms.forms import ArticleForm, UserForm |
|
|
|
from cms.forms import ArticleForm, UserForm |
|
|
|
from cms.models import Article |
|
|
|
from cms.models import Article |
|
|
|
|
|
|
|
from cms.tasks import delayed_post |
|
|
|
|
|
|
|
from datetime import datetime, timezone |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ArticleView(LoginRequiredMixin, View): |
|
|
|
class ArticleView(LoginRequiredMixin, View): |
|
|
|
def post(self, request: HttpRequest): |
|
|
|
def post(self, request: HttpRequest): |
|
|
|
post_data = request.POST |
|
|
|
post_data = request.POST |
|
|
|
|
|
|
|
if 'publication_time' not in post_data or post_data['publication_time'] == "": |
|
|
|
|
|
|
|
# Значение publication_time не указано |
|
|
|
article = Article.objects.create(body=post_data['body'], |
|
|
|
article = Article.objects.create(body=post_data['body'], |
|
|
|
link=post_data['link']) |
|
|
|
link=post_data['link'], |
|
|
|
|
|
|
|
publication_time=datetime.now()) |
|
|
|
|
|
|
|
|
|
|
|
marketer = promoters.Marketer(article) |
|
|
|
marketer = promoters.Marketer(article) |
|
|
|
try: |
|
|
|
try: |
|
|
|
marketer.promote() |
|
|
|
marketer.promote() |
|
|
|
|
|
|
|
article.is_published = 1 |
|
|
|
message_type = messages.SUCCESS |
|
|
|
message_type = messages.SUCCESS |
|
|
|
message_text = 'Продвижение статьи прошло успешно' |
|
|
|
message_text = 'Продвижение статьи прошло успешно' |
|
|
|
|
|
|
|
article.is_published = True |
|
|
|
|
|
|
|
article.save() |
|
|
|
except promoters.PromoteError as exc: |
|
|
|
except promoters.PromoteError as exc: |
|
|
|
message_type = messages.ERROR |
|
|
|
message_type = messages.ERROR |
|
|
|
message_text = 'Произошла ошибка: %s' % str(exc) |
|
|
|
message_text = 'Произошла ошибка: %s' % str(exc) |
|
|
|
messages.add_message(request=request, |
|
|
|
messages.add_message(request=request, |
|
|
|
level=message_type, |
|
|
|
level=message_type, |
|
|
|
message=message_text) |
|
|
|
message=message_text) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
# Значение publication_time указано |
|
|
|
|
|
|
|
publication_time = post_data['publication_time'] |
|
|
|
|
|
|
|
publication_time = datetime.fromisoformat(publication_time) |
|
|
|
|
|
|
|
publication_time = publication_time.astimezone(timezone.utc) |
|
|
|
|
|
|
|
article = Article.objects.create(body=post_data['body'], |
|
|
|
|
|
|
|
link=post_data['link'], |
|
|
|
|
|
|
|
publication_time=publication_time) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
delayed_post.apply_async(args=(article.id, publication_time), eta=publication_time) |
|
|
|
return HttpResponseRedirect(reverse('new-article')) |
|
|
|
return HttpResponseRedirect(reverse('new-article')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -69,3 +90,13 @@ class AuthenticationView(View): |
|
|
|
login(request, |
|
|
|
login(request, |
|
|
|
user=authenticated_user) |
|
|
|
user=authenticated_user) |
|
|
|
return HttpResponseRedirect(reverse('new-article')) |
|
|
|
return HttpResponseRedirect(reverse('new-article')) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def plannedView(request): |
|
|
|
|
|
|
|
data = Article.objects.filter(is_published=False) |
|
|
|
|
|
|
|
return render(request, 'articles/planned.html', context={'post':data}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def articleDelete(request, id): |
|
|
|
|
|
|
|
article = Article.objects.get(id=id) |
|
|
|
|
|
|
|
article.delete() |
|
|
|
|
|
|
|
return redirect('planned') |