fix(404):404 and permission denied page

This commit is contained in:
bobbyvish
2024-05-27 15:29:57 +05:30
parent be1ae7262a
commit a78cf241dd
9 changed files with 143 additions and 4 deletions

View File

@@ -1,5 +1,6 @@
from functools import wraps
from django.core.exceptions import PermissionDenied
from django.shortcuts import render
from . import models
from django.db.models import Q
# import logging
@@ -69,7 +70,7 @@ class ResourcePermissionRequiredMixin:
def dispatch(self, request, *args, **kwargs):
if not self.has_resource_permission(request.user, self.resource):
# logger.warning(f"Permission denied for user {request.user} accessing {self.resource}:{self.action}")
raise PermissionDenied("You do not have permission to access this resource.")
return render(request, "module_iam/permission_denied.html")
return super().dispatch(request, *args, **kwargs)
@classmethod
@@ -80,7 +81,7 @@ class ResourcePermissionRequiredMixin:
instance = cls()
instance.resource = resource
if not instance.has_resource_permission(request.user, instance.resource):
raise PermissionDenied("You do not have permission to access this resource.")
return render(request, "module_iam/permission_denied.html")
return view_func(request, *args, **kwargs)
return _wrapped_view
return decorator

View File

@@ -32,7 +32,7 @@ if READ_DOT_ENV_FILE:
SECRET_KEY = env.str("SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env.bool("DJANGO_DEBUG", True)
DEBUG = env.bool("DJANGO_DEBUG", False)
ALLOWED_HOSTS = []

View File

@@ -17,8 +17,18 @@ Including another URLconf
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.shortcuts import render
from django.urls import include, path
def handler404(request, exception):
if request.user.is_authenticated:
print("True")
return render(request, '404.html')
else:
print("False")
return render(request, '1404.html')
urlpatterns = [
path('admin/', admin.site.urls),
@@ -40,8 +50,13 @@ urlpatterns = [
path('api/notification/', include("module_notification.api.urls")),
]
# handler404 = handler404
if settings.DEBUG:
import debug_toolbar
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += [path('__debug__/', include(debug_toolbar.urls))]
if not settings.DEBUG:
handler404 = handler404

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 MiB

61
templates/1404.html Normal file
View File

@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, shrink-to-fit=no">
<title>Digest</title>
{% load static %}
<link rel="icon" type="image/x-icon" href="../src/assets/img/favicon.ico"/>
<!-- BEGIN GLOBAL MANDATORY STYLES -->
<link href="https://fonts.googleapis.com/css?family=Nunito:400,600,700" rel="stylesheet">
<link href="{% static 'src/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet" type="text/css" />
<link href="{% static 'layouts/collapsible-menu/css/light/plugins.css' %}" rel="stylesheet" type="text/css" />
<link href="{% static 'src/assets/css/light/pages/error/error.css' %}" rel="stylesheet" type="text/css" />
<!-- END GLOBAL MANDATORY STYLES -->
<style>
body.dark .theme-logo.light-element {
display: none;
}
.theme-logo.light-element {
display: inline-block;
}
.error-gif{
background-image: url(https://cdn.dribbble.com/users/722246/screenshots/3066818/404-page.gif);
height: 600px;
background-position: center;
background-repeat: no-repeat;
}
</style>
</head>
<body class="error text-center">
<div class="container-fluid">
<div class="row">
<div class="col-md-4 mr-auto mt-5 text-md-left text-center">
{% comment %} <a href="index.html" class="ml-md-5">
<img alt="image-404" src="../src/assets/img/logo.svg" class="dark-element theme-logo">
<img alt="image-404" src="../src/assets/img/logo2.svg" class="light-element theme-logo">
</a> {% endcomment %}
</div>
</div>
</div>
<div class="container-fluid error-content">
<div class="">
<h2>Ooops!</h2>
<h3 class="m-1">The page you requested was not found!</h3>
<a href="{% url 'module_auth:login'%}" class="btn btn-dark m-1">Login Page</a>
<div class="error-gif"></div>
</div>
</div>
<!-- BEGIN GLOBAL MANDATORY SCRIPTS -->
<script src="{% static 'src/bootstrap/js/bootstrap.bundle.min.js' %}"></script>
<!-- END GLOBAL MANDATORY SCRIPTS -->
</body>
</html>

36
templates/404.html Normal file
View File

@@ -0,0 +1,36 @@
{% extends 'base_structure/layout/base_template.html' %}
{% load static %}
{% block stylesheet %}
{% endblock %}
{% block content %}
<div class="row layout-top-spacing">
<div id="content-wrapper">
<div class="container-fluid">
<!-- Breadcrumbs-->
<ol class="breadcrumb">
<li class="breadcrumb-item">
<a href="{% url 'module_iam:dashboard'%}">Dashboard</a>
</li>
<li class="breadcrumb-item active">404 Error</li>
</ol>
<!-- Page Content -->
<h1 class="display-1">404</h1>
<p class="lead">Page not found. You can
<a class="btn" href="javascript:history.back()">go back</a>
to the previous page, or
<a class="btn" href="{% url 'module_iam:dashboard'%}">return home</a>.</p>
</div>
</div>
{% endblock content %}
{% block javascript %}
{% endblock %}

View File

@@ -24,7 +24,7 @@
</div>
<div class="shadow-bottom"></div>
<ul class="list-unstyled menu-categories" id="accordionExample">
<li class="menu {% if page_name == iam_constants_context.RESOURCE_MANAGE_DASHBOARD %}active{% endif %}">
<li class="menu {% if page_name == iam_constants_context.RESOURCE_MANAGE_DASHBOARD or not page_name%}active{% endif %}">
<a href="{% url 'module_iam:dashboard'%}" aria-expanded="false" class="dropdown-toggle">
<div class="">
<span class="material-symbols-outlined">dashboard</span>

View File

@@ -0,0 +1,25 @@
{% extends 'base_structure/layout/base_template.html' %}
{% load static %}
{% block stylesheet %}
{% endblock %}
{% block content %}
<div class="row layout-top-spacing">
<div id="content-wrapper">
<div class="container-fluid text-center">
<h1 class="w3-jumbo w3-animate-top w3-center"><code>Access Denied</code></h1>
<hr class="w3-border-white w3-animate-left" style="margin:auto;width:50%">
<h3 class="w3-center w3-animate-right">You dont have permission to view this site.</h3>
<h3 class="w3-center w3-animate-zoom">🚫🚫🚫🚫</h3>
<h6 class="w3-center w3-animate-zoom">error code:403 forbidden</h6>
</div>
</div>
</div>
{% endblock content %}
{% block javascript %}
{% endblock %}

View File

@@ -127,6 +127,7 @@ function initializeDataTable(tableName, mainUrl) {
data: "comment",
render: function (data, type, row) {
return `<button class="btn btn-primary view-comment" data-comment="${data}">View</button>`;
}
}
],