Commit 31e5371d authored by Samuel GAIST's avatar Samuel GAIST Committed by Flavio TARSETTI
Browse files

[navigation][all] Pre-commit cleanup

parent 4557dfce
Pipeline #42595 passed with stage
in 14 minutes and 55 seconds
......@@ -29,12 +29,11 @@ from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from .models import Agreement
from ..accounts.models import AccountSettings
from ..accounts.models import SupervisionTrack
from ..accounts.models import Profile
from ..accounts.models import SupervisionTrack
from ..accounts.models import TemporaryUrl
from .models import Agreement
# ----------------------------------------------------------
......
......@@ -28,10 +28,11 @@
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _
class NavigationConfig(AppConfig):
name = 'beat.web.navigation'
verbose_name = _('Navigation')
name = "beat.web.navigation"
verbose_name = _("Navigation")
def ready(self):
from .signals import create_agreement
from .signals import create_agreement # noqa: F401
......@@ -26,6 +26,7 @@
###############################################################################
from django.conf import settings
from .. import __version__
......
......@@ -27,35 +27,37 @@
"""Django Middleware to check for agreements to Legal Disclaimers and ToS"""
from django.contrib import messages
from django.conf import settings
from django.contrib import messages
from django.urls import reverse
from django.utils.safestring import mark_safe
def remove_banner_message(request):
'''Silences all banner messages'''
"""Silences all banner messages"""
storage = messages.get_messages(request)
for message in storage:
if message.extra_tags and 'banner' in message.extra_tags:
message.extra_tags = message.extra_tags.replace('banner', '')
if message.extra_tags and "banner" in message.extra_tags:
message.extra_tags = message.extra_tags.replace("banner", "")
message.extra_tags = message.extra_tags.strip()
message.level = 0
def add_banner_message(request, text):
'''Adds a new banner message if one is not already set'''
"""Adds a new banner message if one is not already set"""
storage = messages.get_messages(request)
for message in storage:
if message.extra_tags and 'banner' in message.extra_tags:
if message.extra_tags and "banner" in message.extra_tags:
message.message = text
return
messages.warning(request, text, extra_tags='banner', fail_silently=True)
messages.warning(request, text, extra_tags="banner", fail_silently=True)
class AgreementMiddleware():
'''Checks for user agreement on legal disclaimers or terms-of-service.'''
class AgreementMiddleware:
"""Checks for user agreement on legal disclaimers or terms-of-service."""
def __init__(self, get_response):
self.get_response = get_response
......@@ -68,14 +70,26 @@ class AgreementMiddleware():
if request.user.is_authenticated:
if settings.TERMS_OF_SERVICE_VERSION > request.user.agreement.version:
add_banner_message(request, mark_safe('We have modified our <a href="%s">Terms of Service (please take a moment to read them)</a>' % reverse('terms-of-service')))
add_banner_message(
request,
mark_safe( # nosec
'We have modified our <a href="%s">Terms of Service (please take a moment to read them)</a>'
% reverse("terms-of-service")
),
)
else:
remove_banner_message(request)
else:
user_version = request.session.get('beat_disclaimer_version', 0)
user_version = request.session.get("beat_disclaimer_version", 0)
if settings.LEGAL_DISCLAIMER_VERSION > user_version:
add_banner_message(request, mark_safe('By continuing to browse this website, you implicitly agree with our <a href="%s">Legal Disclaimer</a>' % reverse('legal-disclaimer')))
add_banner_message(
request,
mark_safe( # nosec
'By continuing to browse this website, you implicitly agree with our <a href="%s">Legal Disclaimer</a>'
% reverse("legal-disclaimer")
),
)
else:
remove_banner_message(request)
......
......@@ -25,9 +25,8 @@
# #
###############################################################################
from django.db import models
from django.contrib.auth.models import User
from django.db import models
# ----------------------------------------------------------
......
......@@ -25,17 +25,17 @@
# #
###############################################################################
from django.conf import settings
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from django.conf import settings
from .models import Agreement
@receiver(post_save, sender=User)
def create_agreement(sender, instance, created, **kwargs):
"""If the user was just created, make sure to instantiate an agreement"""
if created and not instance.pk == settings.ANONYMOUS_USER_ID:
agreement = Agreement(user=instance,
version=settings.TERMS_OF_SERVICE_VERSION)
agreement = Agreement(user=instance, version=settings.TERMS_OF_SERVICE_VERSION)
agreement.save()
......@@ -25,17 +25,17 @@
# #
###############################################################################
from django.conf import settings
from django.shortcuts import render
from django.template import loader
from django.conf import settings
from .middleware import remove_banner_message
def terms_of_service(request):
'''Returns an HTML page with the Terms of Service to the user'''
"""Returns an HTML page with the Terms of Service to the user"""
template = loader.get_template('navigation/terms_of_service.rst')
template = loader.get_template("navigation/terms_of_service.rst")
contents = template.render()
# if user is logged-in, make sure to udpate its agreement status
......@@ -44,22 +44,19 @@ def terms_of_service(request):
request.user.agreement.save()
else:
# equivalent behavior for non-logged in users
request.session['beat_disclaimer_version'] = \
settings.LEGAL_DISCLAIMER_VERSION
request.session["beat_disclaimer_version"] = settings.LEGAL_DISCLAIMER_VERSION
remove_banner_message(request)
return render(request,
'navigation/terms_of_service.html',
dict(contents=contents))
return render(request, "navigation/terms_of_service.html", dict(contents=contents))
#------------------------------------------------
# ------------------------------------------------
def legal_disclaimer(request):
'''Returns an HTML page with the Legal Disclaimeir to the user'''
"""Returns an HTML page with the Legal Disclaimeir to the user"""
template = loader.get_template('navigation/legal_disclaimer.rst')
template = loader.get_template("navigation/legal_disclaimer.rst")
contents = template.render()
# if user is logged-in, make sure to udpate its agreement status
......@@ -68,21 +65,17 @@ def legal_disclaimer(request):
request.user.agreement.save()
else:
# equivalent behavior for non-logged in users
request.session['beat_disclaimer_version'] = \
settings.LEGAL_DISCLAIMER_VERSION
request.session["beat_disclaimer_version"] = settings.LEGAL_DISCLAIMER_VERSION
remove_banner_message(request)
return render(request,
'navigation/legal_disclaimer.html',
dict(contents=contents))
return render(request, "navigation/legal_disclaimer.html", dict(contents=contents))
#------------------------------------------------
# ------------------------------------------------
def contact(request):
'''Returns an HTML page with the contact information'''
"""Returns an HTML page with the contact information"""
return render(request, 'navigation/contact.html')
return render(request, "navigation/contact.html")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment