From 083e3170287f786c6fef388bde8725f6a37dab0a Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Tue, 18 Feb 2020 10:13:53 +0100 Subject: [PATCH] lib: add helper method for 400 response with message --- passbook/core/models.py | 8 ++++---- passbook/core/templates/error/400.html | 3 +++ passbook/lib/views.py | 8 +++++++- .../providers/saml/migrations/0006_auto_20200217_2031.py | 1 + 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/passbook/core/models.py b/passbook/core/models.py index 9372a1d98..b0380254a 100644 --- a/passbook/core/models.py +++ b/passbook/core/models.py @@ -2,20 +2,20 @@ from datetime import timedelta from random import SystemRandom from time import sleep -from typing import Optional, Any +from typing import Any, Optional from uuid import uuid4 -from jinja2.exceptions import TemplateSyntaxError, UndefinedError -from jinja2.nativetypes import NativeEnvironment from django.contrib.auth.models import AbstractUser from django.contrib.postgres.fields import JSONField from django.db import models -from django.urls import reverse_lazy from django.http import HttpRequest +from django.urls import reverse_lazy from django.utils.timezone import now from django.utils.translation import gettext_lazy as _ from django_prometheus.models import ExportModelOperationsMixin from guardian.mixins import GuardianUserMixin +from jinja2.exceptions import TemplateSyntaxError, UndefinedError +from jinja2.nativetypes import NativeEnvironment from model_utils.managers import InheritanceManager from structlog import get_logger diff --git a/passbook/core/templates/error/400.html b/passbook/core/templates/error/400.html index b4f02ecfd..5cdbb3846 100644 --- a/passbook/core/templates/error/400.html +++ b/passbook/core/templates/error/400.html @@ -19,6 +19,9 @@

{% trans 'Bad Request' %}

+ {% if message %} +

{% trans message %}

+ {% endif %} {% if 'back' in request.GET %} {% trans 'Back' %} {% endif %} diff --git a/passbook/lib/views.py b/passbook/lib/views.py index 2ca27ce80..f7d25f425 100644 --- a/passbook/lib/views.py +++ b/passbook/lib/views.py @@ -1,5 +1,6 @@ """passbook helper views""" - +from django.http import HttpRequest, HttpResponse +from django.shortcuts import render from django.views.generic import CreateView from guardian.shortcuts import assign_perm @@ -22,3 +23,8 @@ class CreateAssignPermView(CreateView): ) assign_perm(full_permission, self.request.user, self.object) return response + + +def bad_request_message(request: HttpRequest, message: str) -> HttpResponse: + """Return generic error page with message, with status code set to 400""" + return render(request, "error/400.html", {"message": message}, status=400) diff --git a/passbook/providers/saml/migrations/0006_auto_20200217_2031.py b/passbook/providers/saml/migrations/0006_auto_20200217_2031.py index 9e190405f..37d39a0e9 100644 --- a/passbook/providers/saml/migrations/0006_auto_20200217_2031.py +++ b/passbook/providers/saml/migrations/0006_auto_20200217_2031.py @@ -1,6 +1,7 @@ # Generated by Django 3.0.3 on 2020-02-17 20:31 from django.db import migrations, models + import passbook.providers.saml.utils.time