From 2d971b9328f5410c0f769c5761fce70c9e66c97b Mon Sep 17 00:00:00 2001 From: George Tantiras Date: Tue, 18 Aug 2020 12:04:12 +0300 Subject: [PATCH] Url bug (#42) * Fixed url bug * Blacked --- README.md | 6 ++++-- letsagree/helpers.py | 32 ++++++++++++++++++++++++++++++++ letsagree/middleware.py | 10 +++------- letsagree/tests/test_views.py | 33 +++++++++++++++------------------ letsagree/views.py | 30 +++--------------------------- setup.py | 4 ++-- 6 files changed, 59 insertions(+), 56 deletions(-) create mode 100644 letsagree/helpers.py diff --git a/README.md b/README.md index 469a486..297a834 100644 --- a/README.md +++ b/README.md @@ -119,7 +119,7 @@ Settings LETSAGREE_CACHE = False LETSAGREE_CSS = {} LETSAGREE_JS = () -LETSAGREE_LOGOUT_APP_NAME = 'admin:logout' (Deprecated) +LETSAGREE_LOGOUT_APP_NAME = '' (Deprecated -> default value was 'admin') LETSAGREE_LOGOUT_URL = 'admin:logout' LETSAGREE_BROWSER_TITLE = '' LETSAGREE_BORDER_HEADER = '' @@ -197,7 +197,7 @@ In that case, bear in mind that if `{{ empty_form }}` is False, `{{ form }}` con * `LETSAGREE_LOGOUT_URL`: String that represents a namespaced URL. - For example: `'admin:logout'` is the default. + For example: `'admin:logout'` is the default, it can be any string. If the url is not found, it fails silently resulting in the disappearance of the logout option. * `LETSAGREE_BROWSER_TITLE`: A title for the default template. * `LETSAGREE_BORDER_HEADER`: Text that will appear on the top left corner of the default template. @@ -252,6 +252,8 @@ Unfortunatelly, the test suite is rather complicated. Sorry! Changelog --------- +1.1.5: Fixed bug in LETSAGREE_LOGOUT_URL setting. + 1.1.4: Deprecated `LETSAGREE_LOGOUT_APP_NAME` in favor of `LETSAGREE_LOGOUT_URL` 1.1.3: Locked to Django-3.0 until #39 is resolved diff --git a/letsagree/helpers.py b/letsagree/helpers.py new file mode 100644 index 0000000..0886cd0 --- /dev/null +++ b/letsagree/helpers.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +# ============================================================================== +# +# File Name : letsagree/helpers.py +# +# Creation Date : Tue 18 Aug 2020 10:56:37 AM EEST (10:56) +# +# Last Modified : Tue 18 Aug 2020 11:24:24 AM EEST (11:24) +# +# ============================================================================== +from django.conf import settings +from django.urls import reverse, NoReverseMatch + + +def get_named_url(): + app_name = getattr(settings, "LETSAGREE_LOGOUT_APP_NAME", False) + logout_named_url = getattr(settings, "LETSAGREE_LOGOUT_URL", "admin:logout") + + if app_name: + return "{0}:logout".format(app_name) + else: + return logout_named_url + + +def get_logout_url(): + try: + return reverse(get_named_url()) + except NoReverseMatch: + return "" diff --git a/letsagree/middleware.py b/letsagree/middleware.py index cf393f3..f1c3d93 100644 --- a/letsagree/middleware.py +++ b/letsagree/middleware.py @@ -7,7 +7,7 @@ # # Creation Date : Mon 28 Jan 2019 01:20:20 PM EET (13:20) # -# Last Modified : Fri 27 Mar 2020 08:50:03 PM EET (20:50) +# Last Modified : Tue 18 Aug 2020 11:16:43 AM EEST (11:16) # # ============================================================================== @@ -16,6 +16,7 @@ from django.shortcuts import redirect from django.urls import reverse from letsagree.models import Term +from letsagree.helpers import get_logout_url class LetsAgreeMiddleware: @@ -49,12 +50,7 @@ def validate_user_intention(self): do so if he has already agreed to the current term in effect.) * """ - logout_url_app = ( - getattr(settings, "LETSAGREE_LOGOUT_APP_NAME", "admin") or "admin" - ) - logout_url = ( - reverse("{0}:logout".format(logout_url_app)) if logout_url_app else "" - ) + logout_url = get_logout_url() satisfied_prerequisites = all( ( self.user_id, diff --git a/letsagree/tests/test_views.py b/letsagree/tests/test_views.py index 00fcc3c..7161efb 100644 --- a/letsagree/tests/test_views.py +++ b/letsagree/tests/test_views.py @@ -8,7 +8,7 @@ # # Creation Date : Sat 23 Mar 2019 08:42:45 PM EET (20:42) # -# Last Modified : Sun 16 Aug 2020 08:49:57 PM EEST (20:49) +# Last Modified : Tue 18 Aug 2020 11:26:18 AM EEST (11:26) # # ============================================================================== @@ -34,8 +34,7 @@ reason="Window Function is not supported in this SQLite version", ) @pytest.mark.parametrize( - "terms_agreed,request_url,agree_queries", - [(True, "/", 2), (False, "/", 1), (None, "/", 1)], + "terms_agreed,request_url,agree_queries", [(True, "/", 2), (False, "/", 1)] ) def test_view_structure( queries, @@ -52,8 +51,6 @@ def test_view_structure( # Set once the logout url to test if it is rendered if terms_agreed: settings.LETSAGREE_LOGOUT_APP_NAME = None # By default is 'admin' - elif terms_agreed is None: - settings.LETSAGREE_LOGOUT_URL = "admin:logout" else: settings.LETSAGREE_LOGOUT_APP_NAME = "foo" # Test Pending View @@ -75,19 +72,19 @@ def test_view_structure( assert "LOG OUT" not in response.rendered_content -@pytest.mark.parametrize( - "the_string,the_result", - [ - ("admin", "admin:logout"), - ("admin:", "admin:logout"), - ("admin:logout_view", "admin:logout_view"), - ("", None), - (False, None), - ], -) -def test_named_url(the_string, the_result): - view = views.PendingView() - assert view.get_logout_string(the_string) == the_result +# @pytest.mark.parametrize( +# "the_string,the_result", +# [ +# ("admin", "admin:logout"), +# ("admin:", "admin:logout"), +# ("admin:logout_view", "admin:logout_view"), +# ("", None), +# (False, None), +# ], +# ) +# def test_named_url(the_string, the_result): +# view = views.PendingView() +# assert view.get_logout_string(the_string) == the_result @pytest.mark.skipif( diff --git a/letsagree/views.py b/letsagree/views.py index 043eaed..20b5c83 100644 --- a/letsagree/views.py +++ b/letsagree/views.py @@ -8,7 +8,7 @@ # # Creation Date : Sun 27 Jan 2019 07:54:42 PM EET (19:54) # -# Last Modified : Sun 16 Aug 2020 08:37:39 PM EEST (20:37) +# Last Modified : Tue 18 Aug 2020 11:15:32 AM EEST (11:15) # # ============================================================================== @@ -16,11 +16,11 @@ from django.conf import settings from django.core.cache import cache from django.http import Http404 -from django.urls import reverse, NoReverseMatch from django.utils.translation import gettext_lazy as _ from django.views.generic import FormView from letsagree import models from letsagree.forms import PendingAgreementFormSet +from letsagree.helpers import get_logout_url class PendingView(FormView): @@ -62,18 +62,6 @@ def form_valid(self, form): cache.set(cache_key, False, 24 * 3600) return super().form_valid(form) - @staticmethod - def get_logout_string(the_string): - if the_string: - prep = list(filter(None, the_string.split(":"))) - choices = { - 1: "{0}:logout".format(prep[0]), - 2: "{0}:{1}".format(prep[0], prep[-1]), - } - return choices[len(prep)] - else: - return None - def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["browser_title"] = getattr( @@ -81,19 +69,7 @@ def get_context_data(self, **kwargs): ) context["border_header"] = getattr(settings, "LETSAGREE_BORDER_HEADER", "") context["user"] = self.request.user - - try: - logout_url = ( - self.get_logout_string( - getattr(settings, "LETSAGREE_LOGOUT_APP_NAME", "admin") - ) - or getattr(settings, "LETSAGREE_LOGOUT_URL", "admin:logout") - or "admin:logout" - ) - - context["logout_url"] = reverse(logout_url) - except NoReverseMatch: - pass + context["logout_url"] = get_logout_url() if len(context["form"]) == 0: context["empty_form"] = True diff --git a/setup.py b/setup.py index 1f29ec7..7e7aada 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ # # Creation Date : Mon 08 Apr 2019 07:00:40 PM EEST (19:00) # -# Last Modified : Sun 16 Aug 2020 09:06:10 PM EEST (21:06) +# Last Modified : Tue 18 Aug 2020 11:27:52 AM EEST (11:27) # # ============================================================================== @@ -22,7 +22,7 @@ setup( name="django-letsagree", - version="1.1.4", + version="1.1.5", python_requires=">=3.5", description=( "A django application that associates Groups with Terms "