From 9b33b83dbd5932f024ba59a60907d42bde927b9c Mon Sep 17 00:00:00 2001 From: Artur Galyamov Date: Tue, 20 Dec 2022 10:47:34 +0500 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=81=D1=82=D0=B0=D1=82=D1=8C=D0=B8=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD=D0=BE=20?= =?UTF-8?q?=D0=B1=D0=B5=D0=B7=20=D1=81=D0=BB=D0=B8=D1=87=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82?= =?UTF-8?q?=D0=B5=D0=BB=D1=8F=20#17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cms/views.py | 19 ++++++++++++------- crossposting_backend/settings.py | 3 +++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cms/views.py b/cms/views.py index 72e1a2e..adc2db6 100644 --- a/cms/views.py +++ b/cms/views.py @@ -2,7 +2,9 @@ import os from json import JSONEncoder 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.shortcuts import render from django.urls import reverse @@ -12,7 +14,7 @@ from cms.forms import ArticleForm, UserForm from cms.models import Article -class ArticleView(View): +class ArticleView(LoginRequiredMixin, View): def _promote_to_telegram(self, article: Article): bot_token = os.getenv('TELEGRAM_BOT_TOKEN') channel_id = os.getenv('TELEGRAM_CHAT_ID') @@ -80,6 +82,7 @@ class ArticleView(View): return render(request, template_name='articles/created.html') +@login_required def new_article(request): article_form = ArticleForm() article_context = { @@ -103,9 +106,11 @@ class AuthenticationView(View): def post(self, request, *args, **kwargs): username = request.POST['username'] password = request.POST['password'] - authenticated = authenticate(username=username, - password=password) - if authenticated: - return HttpResponseRedirect(reverse('new-article')) - else: + authenticated_user = authenticate(username=username, + password=password) + if authenticated_user is None: return HttpResponseRedirect(reverse('authenticate')) + else: + login(request, + user=authenticated_user) + return HttpResponseRedirect(reverse('new-article')) diff --git a/crossposting_backend/settings.py b/crossposting_backend/settings.py index a264ca4..27efb74 100644 --- a/crossposting_backend/settings.py +++ b/crossposting_backend/settings.py @@ -15,6 +15,8 @@ from os import path import dotenv # Build paths inside the project like this: BASE_DIR / 'subdir'. +from django.urls import reverse + BASE_DIR = Path(__file__).resolve().parent.parent env_file = path.join(BASE_DIR, '.env') dotenv.read_dotenv(env_file) @@ -31,6 +33,7 @@ DEBUG = True ALLOWED_HOSTS = [] +LOGIN_URL = '/cms/' # Application definition