-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ridwaanhall:ridwaanhall/issue9
Add 404 error handling and improve routing in blog
- Loading branch information
Showing
16 changed files
with
237 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,11 @@ | ||
""" | ||
URL configuration for LUMINA project. | ||
The `urlpatterns` list routes URLs to views. For more information please see: | ||
https://docs.djangoproject.com/en/4.2/topics/http/urls/ | ||
Examples: | ||
Function views | ||
1. Add an import: from my_app import views | ||
2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
Class-based views | ||
1. Add an import: from other_app.views import Home | ||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
Including another URLconf | ||
1. Import the include() function: from django.urls import include, path | ||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
""" | ||
from django.urls import path, include | ||
from . import views | ||
|
||
urlpatterns = [ | ||
path('@ridwaanhall', views.PortfolioWebsiteView.as_view(), name='ridwaanhall'), | ||
path('lanpage/', views.LandingPageView.as_view(), name='lanpage'), | ||
path('<path:url>', views.CatchAllView.as_view(), name='catch_all'), | ||
|
||
path('', include('absen.urls')), | ||
path('blog/', include('blog.urls')), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,52 @@ | ||
from django.shortcuts import render | ||
from django.views import View | ||
from django.http import HttpResponseRedirect | ||
from django.core.exceptions import SuspiciousOperation | ||
from urllib.parse import urlparse | ||
|
||
class PortfolioWebsiteView(View): | ||
def get(self, request, *args, **kwargs): | ||
return HttpResponseRedirect('https://ridwaanhall.vercel.app/') | ||
try: | ||
url = 'https://ridwaanhall.vercel.app/' | ||
if not urlparse(url).scheme or not urlparse(url).netloc: | ||
raise SuspiciousOperation("Invalid redirect URL") | ||
return HttpResponseRedirect(url) | ||
except SuspiciousOperation as e: | ||
context = { | ||
'error_code': 400, | ||
'error_message': f'Bad Request: {e}' | ||
} | ||
return render(request, 'error.html', context, status=400) | ||
except Exception as e: | ||
context = { | ||
'error_code': 500, | ||
'error_message': f'Internal Server Error: {e}' | ||
} | ||
return render(request, 'error.html', context, status=500) | ||
|
||
class LandingPageView(View): | ||
def get(self, request, *args, **kwargs): | ||
return HttpResponseRedirect('https://ngoding-me.vercel.app/') | ||
try: | ||
url = 'https://ngoding-me.vercel.app/' | ||
if not urlparse(url).scheme or not urlparse(url).netloc: | ||
raise SuspiciousOperation("Invalid redirect URL") | ||
return HttpResponseRedirect(url) | ||
except SuspiciousOperation as e: | ||
context = { | ||
'error_code': 400, | ||
'error_message': f'Bad Request: {e}' | ||
} | ||
return render(request, 'error.html', context, status=400) | ||
except Exception as e: | ||
context = { | ||
'error_code': 500, | ||
'error_message': f'Internal Server Error: {e}' | ||
} | ||
return render(request, 'error.html', context, status=500) | ||
|
||
class CatchAllView(View): | ||
def get(self, request, *args, **kwargs): | ||
context = { | ||
'error_code': 404 | ||
} | ||
return render(request, 'error.html', context, status=404) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,38 @@ | ||
from django.shortcuts import render | ||
from django.views import View | ||
|
||
# Create your views here. | ||
|
||
class LuminaAppView(View): | ||
def get(self, request): | ||
return render(request, 'absen/absen.html') | ||
|
||
try: | ||
return render(request, 'absen/absen.html') | ||
except (FileNotFoundError, ImportError) as e: | ||
context = { | ||
'error_code': 500, | ||
'error_message': f'Module Error: {e}' | ||
} | ||
return render(request, 'error.html', context, status=500) | ||
except Exception as e: | ||
context = { | ||
'error_code': 500, | ||
'error_message': f'Unexpected Error: {e}' | ||
} | ||
return render(request, 'error.html', context, status=500) | ||
|
||
|
||
class TermsView(View): | ||
def get(self, request): | ||
return render(request, 'absen/terms.html') | ||
try: | ||
return render(request, 'absen/terms.html') | ||
except (FileNotFoundError, ImportError) as e: | ||
context = { | ||
'error_code': 500, | ||
'error_message': f'Module Error: {e}' | ||
} | ||
return render(request, 'error.html', context, status=500) | ||
except Exception as e: | ||
context = { | ||
'error_code': 500, | ||
'error_message': f'Unexpected Error: {e}' | ||
} | ||
return render(request, 'error.html', context, status=500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from datetime import datetime | ||
from django.utils import timezone | ||
|
||
|
||
class BlogData: | ||
blogs = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,75 @@ | ||
from django.shortcuts import render | ||
from django.views import View | ||
from django.utils.text import slugify | ||
from django.core.exceptions import SuspiciousOperation | ||
|
||
from . import blog_data | ||
|
||
|
||
class BlogListView(View): | ||
def get(self, request): | ||
blogs = blog_data.BlogData.blogs | ||
try: | ||
blogs = blog_data.BlogData.blogs | ||
context = { | ||
'blogs': blogs | ||
} | ||
return render(request, 'blog/blog.html', context) | ||
|
||
context = { | ||
'blogs': blogs | ||
} | ||
return render(request, 'blog/blog.html', context) | ||
except AttributeError as e: | ||
context = { | ||
'error_code': 500, | ||
'error_message': f'AttributeError: {e}' | ||
} | ||
return render(request, 'error.html', context, status=500) | ||
except (TypeError, KeyError) as e: | ||
context = { | ||
'error_code': 500, | ||
'error_message': f'Data Error: {e}' | ||
} | ||
return render(request, 'error.html', context, status=500) | ||
except (FileNotFoundError, ImportError) as e: | ||
context = { | ||
'error_code': 500, | ||
'error_message': f'Module Error: {e}' | ||
} | ||
return render(request, 'error.html', context, status=500) | ||
except Exception as e: | ||
context = { | ||
'error_code': 500, | ||
'error_message': f'Unexpected Error: {e}' | ||
} | ||
return render(request, 'error.html', context, status=500) | ||
|
||
class BlogDetailView(View): | ||
def get(self, request, title): | ||
|
||
blogs = blog_data.BlogData.blogs | ||
|
||
blog_post = next((item for item in blogs if slugify(item['title']) == title), None) | ||
other_blogs = [item for item in blogs if slugify(item['title']) != title] | ||
try: | ||
if not isinstance(title, str): | ||
raise SuspiciousOperation("Invalid title format") | ||
|
||
blog_post = next((item for item in blogs if slugify(item['title']) == title), None) | ||
other_blogs = [item for item in blogs if slugify(item['title']) != title] | ||
|
||
if blog_post: | ||
if blog_post: | ||
context = { | ||
'blog': blog_post, | ||
'other_blogs': other_blogs | ||
} | ||
return render(request, 'blog/blog_detail.html', context) | ||
else: | ||
context = { | ||
'error_code': 404 | ||
} | ||
return render(request, 'error.html', context, status=404) | ||
|
||
except SuspiciousOperation as e: | ||
context = { | ||
'error_code': 400 | ||
} | ||
return render(request, 'error.html', context, status=400) | ||
except Exception as e: | ||
context = { | ||
'blog' : blog_post, | ||
'other_blogs': other_blogs | ||
'error_code': 500 | ||
} | ||
return render(request, 'blog/blog_detail.html', context) | ||
else: | ||
return render(request, 'blog/blog_not_found.html', status=404) | ||
return render(request, 'error.html', context, status=500) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.