# 382 - Redirect as 404
* extends 404 template (user should check the URL)
* introduces new decorator "uuid_required" which performs a check on a given 'uuid' or 'id' parameter
* throws a Http404 exception --> redirect to 404 template instead of 500 error template
This commit is contained in:
@@ -7,9 +7,11 @@ Created on: 16.11.20
|
||||
"""
|
||||
|
||||
from functools import wraps
|
||||
from uuid import UUID
|
||||
|
||||
from bootstrap_modal_forms.mixins import is_ajax
|
||||
from django.contrib import messages
|
||||
from django.http import Http404
|
||||
from django.shortcuts import redirect, get_object_or_404, render
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@@ -171,3 +173,20 @@ def login_required_modal(function):
|
||||
return render(request, template, context)
|
||||
return function(request, *args, **kwargs)
|
||||
return wrap
|
||||
|
||||
|
||||
def uuid_required(function):
|
||||
"""
|
||||
Checks whether the given input is a valid UUID
|
||||
"""
|
||||
@wraps(function)
|
||||
def wrap(request, *args, **kwargs):
|
||||
uuid = kwargs.get("uuid", None) or kwargs.get("id", None)
|
||||
try:
|
||||
uuid = UUID(uuid)
|
||||
except ValueError:
|
||||
raise Http404(
|
||||
"Invalid UUID"
|
||||
)
|
||||
return function(request, *args, **kwargs)
|
||||
return wrap
|
||||
|
||||
Reference in New Issue
Block a user