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