Skip to content

Commit

Permalink
Url bug (#42)
Browse files Browse the repository at this point in the history
* Fixed url bug

* Blacked
  • Loading branch information
raratiru authored Aug 18, 2020
1 parent c754147 commit 2d971b9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 56 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions letsagree/helpers.py
Original file line number Diff line number Diff line change
@@ -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 ""
10 changes: 3 additions & 7 deletions letsagree/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#
# ==============================================================================

Expand All @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
33 changes: 15 additions & 18 deletions letsagree/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#
# ==============================================================================

Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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(
Expand Down
30 changes: 3 additions & 27 deletions letsagree/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
#
# 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)
#
# ==============================================================================

from django.db import transaction
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):
Expand Down Expand Up @@ -62,38 +62,14 @@ 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(
settings, "LETSAGREE_BROWSER_TITLE", _("Let's Agree")
)
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
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#
# ==============================================================================

Expand All @@ -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 "
Expand Down

0 comments on commit 2d971b9

Please sign in to comment.