From df67bcf639054b361c43abd7c93bf309a742f39f Mon Sep 17 00:00:00 2001 From: Leonhard Strohmidel Date: Sun, 25 Sep 2022 18:02:59 +0200 Subject: [PATCH] #64 explorer draft view --- .../lostplaces/templates/explorer/drafts.html | 28 +++++++++++++++++++ .../lostplaces/templates/global.html | 3 +- .../tests/views/test_explorer_views.py | 28 +++++++++++++++++++ django_lostplaces/lostplaces/urls.py | 2 ++ .../lostplaces/views/explorer_views.py | 22 +++++++++++++-- 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 django_lostplaces/lostplaces/templates/explorer/drafts.html diff --git a/django_lostplaces/lostplaces/templates/explorer/drafts.html b/django_lostplaces/lostplaces/templates/explorer/drafts.html new file mode 100644 index 0000000..ef9f122 --- /dev/null +++ b/django_lostplaces/lostplaces/templates/explorer/drafts.html @@ -0,0 +1,28 @@ +{% extends 'global.html'%} +{% load static %} +{% load i18n %} + +{% load svg_icon %} + +{% block maincontent %} +
+
+

+ {% if user.username == explorer.user.username %} + {% translate 'Your drafts' %} + {% else %} + {{explorer.user.username}}{% translate '\'s drafts' %} + {% endif %} +

+ +
+
+{% endblock maincontent %} \ No newline at end of file diff --git a/django_lostplaces/lostplaces/templates/global.html b/django_lostplaces/lostplaces/templates/global.html index 280c504..78a207c 100644 --- a/django_lostplaces/lostplaces/templates/global.html +++ b/django_lostplaces/lostplaces/templates/global.html @@ -32,7 +32,8 @@ {% if user.is_authenticated %} Hi {{ user.username }}! {% translate 'Logout' %} | - {% translate 'Profile' %} + {% translate 'Profile' %} | + {% translate 'Drafts' %} {% if user.is_superuser %} | {% translate 'Admin' %} {% endif %} diff --git a/django_lostplaces/lostplaces/tests/views/test_explorer_views.py b/django_lostplaces/lostplaces/tests/views/test_explorer_views.py index 04cce21..4aa3b42 100644 --- a/django_lostplaces/lostplaces/tests/views/test_explorer_views.py +++ b/django_lostplaces/lostplaces/tests/views/test_explorer_views.py @@ -84,6 +84,34 @@ class TestExplorerProfileView(GlobalTemplateTestCaseMixin, ViewTestCase): msg='Expecting the latest place to be visible on the submitters profile page' ) + def test_draft_in_explorer_places(self): + user = User.objects.get(username='testpeter') + Place.objects.create( + name='Im the latest place in draft 3671', + submitted_when=timezone.now(), + submitted_by=user.explorer, + location='Test %d town' % 5, + latitude=50.5 + 5/10, + longitude=7.0 - 5/10, + description='This is just a test, do not worry %d' % 5, + level=3, + mode='draft' + ) + + self.client.login(username='testpeter', password='Develop123') + + response = self.client.get( + reverse('explorer_profile', kwargs={ + 'explorer_id': user.explorer.id + }) + ) + + self.assertHttpOK(response) + self.assertFalse( + 'Im the latest place in draft 3671' in response.content.decode(), + msg='Expecting a place in draft mode to not show up on the submitters profile page' + ) + def test_explorer_image(self): user = User.objects.get(username='testpeter') place = Place.objects.create( diff --git a/django_lostplaces/lostplaces/urls.py b/django_lostplaces/lostplaces/urls.py index ca4cf5c..6cc6f38 100644 --- a/django_lostplaces/lostplaces/urls.py +++ b/django_lostplaces/lostplaces/urls.py @@ -23,6 +23,7 @@ from lostplaces.views import ( PhotoAlbumDeleteView, ExplorerProfileView, ExplorerProfileUpdateView, + ExplorerDraftsView, PlaceVoteView ) @@ -33,6 +34,7 @@ urlpatterns = [ path('explorer//', ExplorerProfileView.as_view(), name='explorer_profile'), path('explorer/update/', ExplorerProfileUpdateView.as_view(), name='explorer_profile_update'), + path('explorer//drafts', ExplorerDraftsView.as_view(), name='explorer_drafts'), path('place/', PlaceListView.as_view(), name='place_list'), path('place//', PlaceDetailView.as_view(), name='place_detail'), diff --git a/django_lostplaces/lostplaces/views/explorer_views.py b/django_lostplaces/lostplaces/views/explorer_views.py index 4ad74c5..3cf99c2 100644 --- a/django_lostplaces/lostplaces/views/explorer_views.py +++ b/django_lostplaces/lostplaces/views/explorer_views.py @@ -8,6 +8,7 @@ from django.utils.translation import gettext as _ from django.shortcuts import render, redirect, get_object_or_404 from django.urls import reverse_lazy from django.contrib import messages +from django.contrib.auth.mixins import UserPassesTestMixin from lostplaces.common import get_all_subclasses from lostplaces.views.base_views import IsAuthenticatedMixin @@ -18,7 +19,7 @@ from lostplaces.forms import ExplorerChangeForm, ExplorerUserChangeForm class ExplorerProfileView(IsAuthenticatedMixin, View): def get(self, request, explorer_id): explorer = get_object_or_404(Explorer, pk=explorer_id) - place_list = explorer.places.all() + place_list = explorer.get_place_list_to_display() place_count = place_list.count() context={ @@ -81,4 +82,21 @@ class ExplorerProfileUpdateView(IsAuthenticatedMixin, View): _('Please fill in all required fields.') ) return redirect(reverse_lazy('explorer_profile_update')) - \ No newline at end of file + +class ExplorerDraftsView(IsAuthenticatedMixin, UserPassesTestMixin, View): + + def get_explorer(self): + return get_object_or_404(Explorer, pk=self.kwargs['explorer_id']) + + def test_func(self): + if not hasattr(self.request, 'user'): + return False + return self.request.user == self.get_explorer().user or self.request.user.is_superuser + + def get(self, request, *args, **kwargs): + context = { + 'explorer': self.get_explorer(), + 'place_list': self.get_explorer().get_drafts() + } + return render(request, 'explorer/drafts.html', context) + \ No newline at end of file