Skip to content

Commit

Permalink
Modified views
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniDiazTech committed May 26, 2021
1 parent b8c8ccd commit 749eb9c
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions config/photoapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.shortcuts import get_object_or_404

from django.http import HttpResponseNotAllowed
from django.core.exceptions import PermissionDenied

from django.urls.resolvers import get_resolver

Expand Down Expand Up @@ -39,7 +39,7 @@ def get_tag(self):
return self.kwargs.get('tag')

def get_queryset(self):
return Photo.objects.filter(tag__slug_in=[self.get_tag()])
return Photo.objects.filter(tags__slug=self.get_tag())

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
Expand All @@ -51,27 +51,23 @@ class PhotoDetailView(DetailView):

model = Photo

context_object_name = 'photo'

template_name = 'photoapp/detail.html'

context_object_name = 'photo'


class PhotoCreateView(LoginRequiredMixin, CreateView):

template_name = 'photoapp/create.html'

model = Photo

form = CreatePhotoForm
form_class = CreatePhotoForm

success_url = reverse_lazy('photo:list')

def form_valid(self, form):

form.user = self.request.user
form.save()
# Without this next line the tags won't be saved.
form.save_m2m()
form.instance.submitter = self.request.user

return super().form_valid(form)

class UserIsSubmitter(UserPassesTestMixin):
Expand All @@ -82,18 +78,18 @@ def get_photo(self):

def test_func(self):

if self.request.user.is_authenticated():
return self.request.user is self.get_photo().submitter
if self.request.user.is_authenticated:
return self.request.user == self.get_photo().submitter
else:
raise HttpResponseNotAllowed('Sorry you are not allowed here')
raise PermissionDenied('Sorry you are not allowed here')

class PhotoUpdateView(UserIsSubmitter, UpdateView):

template_name = 'photoapp/update.html'

model = Photo

form = UpdatePhotoForm
form_class = UpdatePhotoForm

success_url = reverse_lazy('photo:list')

Expand Down

0 comments on commit 749eb9c

Please sign in to comment.