diff options
| author | Bobby <[email protected]> | 2023-01-30 01:07:10 -0500 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-01-30 01:07:10 -0500 |
| commit | 96d5446f193199e92fc45552073ee5b66f1151b7 (patch) | |
| tree | 896003e96542c396bf817a166e5f5ba80563474a | |
| parent | 192ca02e627715e83c61e8a0a5194c76a41348af (diff) | |
| download | thatcomputerscientist-96d5446f193199e92fc45552073ee5b66f1151b7.tar.xz thatcomputerscientist-96d5446f193199e92fc45552073ee5b66f1151b7.zip | |
custom 404 page bot
| -rw-r--r-- | .gitignore | 6 | ||||
| -rw-r--r-- | blog/views.py | 32 | ||||
| -rw-r--r-- | static/images/site/errors/404/1.gif | bin | 0 -> 5305 bytes | |||
| -rw-r--r-- | static/images/site/errors/404/2.gif | bin | 0 -> 5996 bytes | |||
| -rw-r--r-- | static/images/site/errors/404/3.gif | bin | 0 -> 6549 bytes | |||
| -rw-r--r-- | static/images/site/errors/404/4.gif | bin | 0 -> 6570 bytes | |||
| -rw-r--r-- | static/images/site/errors/404/404.gif | bin | 0 -> 5350 bytes | |||
| -rw-r--r-- | static/images/site/errors/404/404_user.gif | bin | 0 -> 5134 bytes | |||
| -rw-r--r-- | static/images/site/skippy.png | bin | 0 -> 273047 bytes | |||
| -rw-r--r-- | templates/400.html | 15 | ||||
| -rw-r--r-- | templates/403.html | 15 | ||||
| -rw-r--r-- | templates/404.html | 50 | ||||
| -rw-r--r-- | templates/500.html | 15 | ||||
| -rw-r--r-- | thatcomputerscientist/error_handler.py | 55 | ||||
| -rw-r--r-- | thatcomputerscientist/settings.py | 2 | ||||
| -rw-r--r-- | thatcomputerscientist/templatetags/random_numbers.py | 10 | ||||
| -rw-r--r-- | thatcomputerscientist/urls.py | 2 |
17 files changed, 124 insertions, 78 deletions
@@ -131,6 +131,6 @@ dmypy.json # .DS_Store for macOS .DS_Store -images -.DS_Store -.DS_Store +images/ + +staticfiles/ diff --git a/blog/views.py b/blog/views.py index 1bf853ae..5a89064c 100644 --- a/blog/views.py +++ b/blog/views.py @@ -1,6 +1,6 @@ from datetime import datetime from django.shortcuts import render, redirect, reverse -from django.http import HttpResponse +from django.http import HttpResponse, Http404 from users.models import UserProfile from django.contrib.auth.models import User from django.core.paginator import Paginator @@ -267,22 +267,26 @@ def articles(request, date=None, cg=None): return render(request, 'blog/articles.html', {'title': 'Articles', 'posts': posts, 'num_pages': num_pages, 'page': page, 'order_by': order_by, 'direction': direction, 'categories': categories, 'category': category, 'type': type, 'date': date if date else '', 'cg': cg if cg else ''}) def user_activity(request, username): - user = User.objects.get(username=username) - user_profile = UserProfile.objects.get(user=user) - if user_profile.is_public or user == request.user: - recent_comments = Comment.objects.filter(user=user).order_by('-created_at')[:5] - else: - recent_comments = [] + try: + user = User.objects.get(username=username) + user_profile = UserProfile.objects.get(user=user) + if user_profile.is_public or user == request.user: + recent_comments = Comment.objects.filter(user=user).order_by('-created_at')[:5] + else: + recent_comments = [] - if user_profile.email_public: - user_email = user.email - else: - user_email = '' + if user_profile.email_public: + user_email = user.email + else: + user_email = '' - for comment in recent_comments: - comment.body = comment_processor(comment.body) + for comment in recent_comments: + comment.body = comment_processor(comment.body) - return render(request, 'blog/activity.html', {'title': 'User Activity', 'activity_user': user, 'activity_user_profile': user_profile, 'activity_recent_comments': recent_comments, 'activity_user_email': user_email}) + return render(request, 'blog/activity.html', {'title': 'User Activity', 'activity_user': user, 'activity_user_profile': user_profile, 'activity_recent_comments': recent_comments, 'activity_user_email': user_email}) + except User.DoesNotExist: + # return default 404 page + raise Http404 def archives(request): archives = Post.objects.filter(is_public=True).dates('date', 'month', order='DESC') diff --git a/static/images/site/errors/404/1.gif b/static/images/site/errors/404/1.gif Binary files differnew file mode 100644 index 00000000..42bcd8f3 --- /dev/null +++ b/static/images/site/errors/404/1.gif diff --git a/static/images/site/errors/404/2.gif b/static/images/site/errors/404/2.gif Binary files differnew file mode 100644 index 00000000..5a89913b --- /dev/null +++ b/static/images/site/errors/404/2.gif diff --git a/static/images/site/errors/404/3.gif b/static/images/site/errors/404/3.gif Binary files differnew file mode 100644 index 00000000..b4e049e6 --- /dev/null +++ b/static/images/site/errors/404/3.gif diff --git a/static/images/site/errors/404/4.gif b/static/images/site/errors/404/4.gif Binary files differnew file mode 100644 index 00000000..879b0907 --- /dev/null +++ b/static/images/site/errors/404/4.gif diff --git a/static/images/site/errors/404/404.gif b/static/images/site/errors/404/404.gif Binary files differnew file mode 100644 index 00000000..2f9cd722 --- /dev/null +++ b/static/images/site/errors/404/404.gif diff --git a/static/images/site/errors/404/404_user.gif b/static/images/site/errors/404/404_user.gif Binary files differnew file mode 100644 index 00000000..4c28be53 --- /dev/null +++ b/static/images/site/errors/404/404_user.gif diff --git a/static/images/site/skippy.png b/static/images/site/skippy.png Binary files differnew file mode 100644 index 00000000..512f7552 --- /dev/null +++ b/static/images/site/skippy.png diff --git a/templates/400.html b/templates/400.html index a8a37a59..41beaa4e 100644 --- a/templates/400.html +++ b/templates/400.html @@ -4,20 +4,7 @@ <div style="text-align: center;"> <h1>400 Bad Request</h1> <p>Unfortunately, something went wrong. The server could not understand your request.</p> - <p><a href="{% url 'blog:home' %}" style="color: #feffcf; - display: inline-block; - text-decoration: none; - font-weight: bold; - font-size: 12px; - padding: 4px 8px; - display: inline-block; - background: #0000FF; - border-top: 2px solid #6666FF; - border-left: 2px solid #6666FF; - border-right: 2px solid #00007C; - border-bottom: 2px solid #00007C; - font-family: 'TopNavbar', sans-serif; - position: relative;">Go to Home</a></p> + <p><a href="{% url 'blog:home' %}" class="button button-special">Go Home</a></p> </div> </div> {% endblock %} diff --git a/templates/403.html b/templates/403.html index c3a74937..1ab86cd6 100644 --- a/templates/403.html +++ b/templates/403.html @@ -4,20 +4,7 @@ <div style="text-align: center;"> <h1>403 Permission Denied</h1> <p>You do not have permission to access this page.</p> - <p><a href="{% url 'blog:home' %}" style="color: #feffcf; - display: inline-block; - text-decoration: none; - font-weight: bold; - font-size: 12px; - padding: 4px 8px; - display: inline-block; - background: #0000FF; - border-top: 2px solid #6666FF; - border-left: 2px solid #6666FF; - border-right: 2px solid #00007C; - border-bottom: 2px solid #00007C; - font-family: 'TopNavbar', sans-serif; - position: relative;">Go to Home</a></p> + <p><<a href="{% url 'blog:home' %}" class="button button-special">Go Home</a></p> </div> </div> {% endblock %} diff --git a/templates/404.html b/templates/404.html index 106bdd5a..ff78fd06 100644 --- a/templates/404.html +++ b/templates/404.html @@ -1,23 +1,37 @@ {% extends 'blog/partials/base.html' %} {% block content %} {% load static %} -<div class="main"> - <div style="text-align: center;"> - <h1>404 Page Not Found</h1> - <p>Sorry, the page you are looking for does not exist. The page may have been removed or relocated.</p> - <p><a href="{% url 'blog:home' %}" style="color: #feffcf; - display: inline-block; - text-decoration: none; - font-weight: bold; - font-size: 12px; - padding: 4px 8px; - display: inline-block; - background: #0000FF; - border-top: 2px solid #6666FF; - border-left: 2px solid #6666FF; - border-right: 2px solid #00007C; - border-bottom: 2px solid #00007C; - font-family: 'TopNavbar', sans-serif; - position: relative;">Go to Home</a></p> + +<div id="notfound" style="margin: 0px auto 0px auto;width: 720px;"> + <img src="{% static 'images/site/skippy.png' %}" style="width: 200px; float: left; padding-right: 20px; display: inline;"> + <div style="width: 500px; display: inline;"> + {% if context.mode == 'article' %} + {% load random_numbers %} + <img src="/static/images/site/errors/404/{% random_numbers 1 4 %}.gif" style="width: 500px;"> + <br><br> + <p>Hey! Skippy here! I am the 404 Assistant Bot for this site. Looks like you are trying to search an article, but I couldn't find the page. {% if context.similar_posts %}Maybe you are looking for one of these?{% endif %}</p> + <ul style="position: relative; left: 30px; top: 10px;"> + {% for post in context.similar_posts %} + <li><a href="{% url 'blog:post' post.slug %}">{{ post.title }}</a></li> + {% endfor %} + </ul> + {% elif context.mode == 'user' %} + <img src="{% static 'images/site/errors/404/404_user.gif' %}" style="width: 500px;"> + <br><br> + <p>Hey! Skippy here! I am the 404 Assistant Bot for this site. Looks like you are trying to search a user with username <b>{{ context.username }}</b> but I couldn't find any user with that username. {% if context.similar_users %}Maybe you are looking for one of these users?{% endif %}</p> + <ul style="position: relative; left: 30px; top: 10px;"> + {% for user in context.similar_users %} + <li><a href="{% url 'blog:user_activity' user.username %}">{{ user.username }}</a></li> + {% endfor %} + </ul> + {% else %} + <img src="{% static 'images/site/errors/404/404.gif' %}" style="width: 500px;"> + <p>Hey! Skippy here! I am the 404 Assistant Bot for this site. Unfortunately, I couldn't find the page you were looking for. Let me guide you home!</p> + {% endif %} + <br> + <p><a href="{% url 'blog:home' %}" class="button button-special">Go Home</a></p> </div> + <div style="clear:both;"></div> + <div style="margin-top: 28px; border-bottom: dotted 1px #fefefe;"></div> </div> + {% endblock %} diff --git a/templates/500.html b/templates/500.html index 79d3e8d8..f61b46fa 100644 --- a/templates/500.html +++ b/templates/500.html @@ -4,20 +4,7 @@ <div style="text-align: center;"> <h1>500 Server Error</h1> <p>Sorry, something went wrong. The server encountered an internal error or misconfiguration and was unable to complete your request.</p> - <p><a href="{% url 'blog:home' %}" style="color: #feffcf; - display: inline-block; - text-decoration: none; - font-weight: bold; - font-size: 12px; - padding: 4px 8px; - display: inline-block; - background: #0000FF; - border-top: 2px solid #6666FF; - border-left: 2px solid #6666FF; - border-right: 2px solid #00007C; - border-bottom: 2px solid #00007C; - font-family: 'TopNavbar', sans-serif; - position: relative;">Go to Home</a></p> + <p><a href="{% url 'blog:home' %}" class="button button-special">Go Home</a></p> </div> </div> {% endblock %} diff --git a/thatcomputerscientist/error_handler.py b/thatcomputerscientist/error_handler.py new file mode 100644 index 00000000..720681a7 --- /dev/null +++ b/thatcomputerscientist/error_handler.py @@ -0,0 +1,55 @@ +from django.shortcuts import render +from django.contrib.auth.models import User +from blog.models import Post +import re +import jellyfish + +def get_similar_posts(slug): + + posts = Post.objects.all().filter(is_public=True) + similar_posts = [] + for post in posts: + + slug_similarity = jellyfish.jaro_winkler(slug, post.slug) + title_similarity = jellyfish.jaro_winkler(slug, post.title) + + if slug_similarity > 0.8 or title_similarity > 0.8: + similar_posts.append(post) + + return similar_posts + +def get_similar_users(username): + + users = User.objects.all() + similar_users = [] + for user in users: + username_similarity = jellyfish.jaro_winkler(username, user.username) + if username_similarity > 0.8: + similar_users.append(user) + + return similar_users + + +def _404(request, exception): + # Your custom 404 view logic here + context = { + 'mode': 'generic', + 'title': '404 Page Not Found', + } + path = request.path[1:] if request.path.startswith('/') else request.path + + if (re.fullmatch(r'[\w-]+', path) or re.fullmatch(r'articles/[\w-]+', path)) and '-' in path: + context['mode'] = 'article' + similar_posts = get_similar_posts(path) + if similar_posts: + context['similar_posts'] = similar_posts + + if path.startswith('~'): + context['mode'] = 'user' + username = path[1:] + similar_users = get_similar_users(username) + if similar_users: + context['similar_users'] = similar_users + context['username'] = username + + return render(request, '404.html', {'context': context, 'title': '404 Page Not Found'}, status=404) diff --git a/thatcomputerscientist/settings.py b/thatcomputerscientist/settings.py index 8c8ac200..1524a633 100644 --- a/thatcomputerscientist/settings.py +++ b/thatcomputerscientist/settings.py @@ -27,7 +27,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = os.getenv('AUTHORIZATION_STRING') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True if os.getenv('ENVIRONMENT') == 'development' else True +DEBUG = True if os.getenv('ENVIRONMENT') == 'development' else False ALLOWED_HOSTS = ["*"] CSRF_TRUSTED_ORIGINS = ['https://*.thatcomputerscientist.com', 'http://*.thatcomputerscientist.com'] diff --git a/thatcomputerscientist/templatetags/random_numbers.py b/thatcomputerscientist/templatetags/random_numbers.py new file mode 100644 index 00000000..05bbbf6d --- /dev/null +++ b/thatcomputerscientist/templatetags/random_numbers.py @@ -0,0 +1,10 @@ +import random +from django import template + +register = template.Library() + [email protected]_tag() +def random_numbers(a, b=None): + if b is None: + a, b = 0, a + return random.randint(a, b) diff --git a/thatcomputerscientist/urls.py b/thatcomputerscientist/urls.py index beecdefe..8335918e 100644 --- a/thatcomputerscientist/urls.py +++ b/thatcomputerscientist/urls.py @@ -28,6 +28,8 @@ sitemaps = { 'github': GithubSitemap, } +handler404 = 'thatcomputerscientist.error_handler._404' + urlpatterns = [ path('admin/', admin.site.urls), path('', include('blog.urls', namespace='blog')), |
