From c0191fc6c400fd8c12739278da5c3f4e24790283 Mon Sep 17 00:00:00 2001 From: Commander1024 Date: Wed, 12 Aug 2020 18:59:21 +0200 Subject: [PATCH] Added LoginRequiredMixin to check for authenticated user. --- lostplaces/lostplaces_app/views.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lostplaces/lostplaces_app/views.py b/lostplaces/lostplaces_app/views.py index 47e25bb..9f0e1c9 100644 --- a/lostplaces/lostplaces_app/views.py +++ b/lostplaces/lostplaces_app/views.py @@ -8,7 +8,7 @@ from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.views import View from django.http import Http404 from django.contrib import messages -from django.contrib.auth.mixins import UserPassesTestMixin +from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin from .forms import ( ExplorerCreationForm, @@ -19,6 +19,10 @@ from .models import Place, PlaceImage, Voucher # Create your views here. +# BaseView that checks if user is logged in +class IsAuthenticated(LoginRequiredMixin, View): + redirect_field_name = 'redirect_to' + class SignUpView(CreateView): form_class = ExplorerCreationForm success_url = reverse_lazy('login') @@ -41,7 +45,7 @@ class HomeView(View): } return render(request, 'home.html', context) -class PlaceUpdateView(UpdateView): +class PlaceUpdateView(IsAuthenticated, UpdateView): template_name = 'place/place_update.html' model = Place form_class = PlaceForm @@ -49,7 +53,7 @@ class PlaceUpdateView(UpdateView): def get_success_url(self): return reverse_lazy('place_detail', kwargs={'pk':self.get_object().pk}) -class PlaceCreateView(View): +class PlaceCreateView(IsAuthenticated, View): def get(self, request, *args, **kwargs): place_image_form = PlaceImageCreateForm() @@ -97,7 +101,7 @@ class PlaceCreateView(View): ) place_image.save() -class PlaceDeleteView(UserPassesTestMixin, DeleteView): +class PlaceDeleteView(IsAuthenticated, UserPassesTestMixin, DeleteView): template_name = 'place/place_delete.html' model = Place success_url = reverse_lazy('place_list')