Добавление статьи невозможно без сличения пользователя #17

This commit is contained in:
Artur Galyamov 2022-12-20 10:47:34 +05:00
parent 9655132f5e
commit 9b33b83dbd
2 changed files with 15 additions and 7 deletions

View File

@ -2,7 +2,9 @@ import os
from json import JSONEncoder from json import JSONEncoder
import requests import requests
from django.contrib.auth import authenticate from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
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
from django.urls import reverse from django.urls import reverse
@ -12,7 +14,7 @@ from cms.forms import ArticleForm, UserForm
from cms.models import Article from cms.models import Article
class ArticleView(View): class ArticleView(LoginRequiredMixin, View):
def _promote_to_telegram(self, article: Article): def _promote_to_telegram(self, article: Article):
bot_token = os.getenv('TELEGRAM_BOT_TOKEN') bot_token = os.getenv('TELEGRAM_BOT_TOKEN')
channel_id = os.getenv('TELEGRAM_CHAT_ID') channel_id = os.getenv('TELEGRAM_CHAT_ID')
@ -80,6 +82,7 @@ class ArticleView(View):
return render(request, template_name='articles/created.html') return render(request, template_name='articles/created.html')
@login_required
def new_article(request): def new_article(request):
article_form = ArticleForm() article_form = ArticleForm()
article_context = { article_context = {
@ -103,9 +106,11 @@ class AuthenticationView(View):
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
username = request.POST['username'] username = request.POST['username']
password = request.POST['password'] password = request.POST['password']
authenticated = authenticate(username=username, authenticated_user = authenticate(username=username,
password=password) password=password)
if authenticated: if authenticated_user is None:
return HttpResponseRedirect(reverse('new-article'))
else:
return HttpResponseRedirect(reverse('authenticate')) return HttpResponseRedirect(reverse('authenticate'))
else:
login(request,
user=authenticated_user)
return HttpResponseRedirect(reverse('new-article'))

View File

@ -15,6 +15,8 @@ from os import path
import dotenv import dotenv
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
from django.urls import reverse
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
env_file = path.join(BASE_DIR, '.env') env_file = path.join(BASE_DIR, '.env')
dotenv.read_dotenv(env_file) dotenv.read_dotenv(env_file)
@ -31,6 +33,7 @@ DEBUG = True
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
LOGIN_URL = '/cms/'
# Application definition # Application definition