Coverage for library\urls.py: 50%

18 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-06-29 10:02 +0100

1""" 

2URL configuration for library project. 

3 

4The `urlpatterns` list routes URLs to views. For more information please see: 

5 https://docs.djangoproject.com/en/4.2/topics/http/urls/ 

6Examples: 

7Function views 

8 1. Add an import: from my_app import views 

9 2. Add a URL to urlpatterns: path('', views.home, name='home') 

10Class-based views 

11 1. Add an import: from other_app.views import Home 

12 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 

13Including another URLconf 

14 1. Import the include() function: from django.urls import include, path 

15 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 

16""" 

17from django.contrib import admin 

18from django.shortcuts import redirect 

19from django.urls import path, include 

20from django.views.generic import TemplateView 

21from django.views.defaults import ( 

22 page_not_found as default_page_not_found, 

23 server_error as default_server_error, 

24 permission_denied as default_permission_denied, 

25) 

26 

27urlpatterns = [ 

28 path('', lambda request: redirect('account_login')), 

29 path("books/", include("book.urls")), 

30 path("authors/", include("author.urls")), 

31 path("profile/", include("reader.urls")), 

32 path("accounts/", include("allauth.urls")), 

33 path('admin/', admin.site.urls), 

34 path('403/', TemplateView.as_view(template_name="403.html"), name='403'), 

35] 

36 

37 

38def custom_permission_denied(request, exception): 

39 """ 

40 Custom view for handling permission denied errors. 

41 

42 Args: 

43 request (HttpRequest): The request object. 

44 exception (Exception): The exception that caused the permission denied 

45 error. 

46 

47 Returns: 

48 HttpResponse: The response object with the updated template name. 

49 """ 

50 response = default_permission_denied(request, exception) 

51 response.template_name = '403.html' 

52 return response 

53 

54 

55def custom_page_not_found(request, exception): 

56 """ 

57 Custom handler for page not found (404) errors. 

58 

59 Args: 

60 request (HttpRequest): The HTTP request object. 

61 exception (Exception): The exception that triggered the 404 error. 

62 

63 Returns: 

64 HttpResponse: The response with the custom 404 page. 

65 """ 

66 response = default_page_not_found(request, exception) 

67 response.template_name = '404.html' 

68 return response 

69 

70 

71def custom_server_error(request): 

72 """ 

73 Custom view for handling server errors (500). 

74 

75 Args: 

76 request (HttpRequest): The HTTP request object. 

77 

78 Returns: 

79 HttpResponse: The HTTP response object with the custom error template. 

80 """ 

81 response = default_server_error(request) 

82 response.template_name = '500.html' 

83 return response