From 2c4a9641205338c4687cdb1c7cbfb7b3b19babcb Mon Sep 17 00:00:00 2001 From: Victoria Jury Date: Thu, 17 Oct 2024 11:37:33 +0200 Subject: [PATCH] add non-www middleware --- personal_portfolio/non_www_middleware.py | 18 ++++++++++++++++++ personal_portfolio/settings.py | 6 ++++-- profile/tests.py | 20 ++++++-------------- 3 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 personal_portfolio/non_www_middleware.py diff --git a/personal_portfolio/non_www_middleware.py b/personal_portfolio/non_www_middleware.py new file mode 100644 index 0000000..4f07381 --- /dev/null +++ b/personal_portfolio/non_www_middleware.py @@ -0,0 +1,18 @@ +from django.http import HttpResponsePermanentRedirect + + +class NoWWWRedirectMiddleware: + def __init__(self, get_response=None): + self.get_response = get_response + + def __call__(self, request): + response = self.process_request(request) + return response or self.get_response(request) + + def process_request(self, request): + host = request.get_host() + if host.startswith("www."): + if request.method == "GET": + no_www = host[4:] + url = request.build_absolute_uri().replace(host, no_www, 1) + return HttpResponsePermanentRedirect(url) diff --git a/personal_portfolio/settings.py b/personal_portfolio/settings.py index 2685b6f..a994c88 100644 --- a/personal_portfolio/settings.py +++ b/personal_portfolio/settings.py @@ -12,14 +12,15 @@ import os from pathlib import Path -from dotenv import load_dotenv + +from dotenv import load_dotenv load_dotenv() # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent USERNAME = os.environ.get("USERNAME") -production = (os.environ.get("DJANGO_PROD", False) == 'True') +production = os.environ.get("DJANGO_PROD", False) == "True" # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ @@ -57,6 +58,7 @@ ] MIDDLEWARE = [ + "personal_portfolio.non_www_middleware.NoWWWRedirectMiddleware", "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", diff --git a/profile/tests.py b/profile/tests.py index 8f8c93c..74ac671 100644 --- a/profile/tests.py +++ b/profile/tests.py @@ -29,13 +29,13 @@ def setUpTestData(cls): location="Some school1", date_attended="2010 - 2012", study_details="Certificates, exams, degrees", - position=1 + position=1, ) cls.edu2 = ResumeEducation.objects.create( location="Some school2", date_attended="2013 - 2018", study_details="Certificates, exams, degrees", - position=2 + position=2, ) def test_education_created(self): @@ -48,16 +48,8 @@ class ResumeSkillTestCase(TestCase): @classmethod def setUpTestData(cls): - cls.skill1 = ResumeSkill.objects.create( - category="Technical skills", - skill="Python", - position=1 - ) - cls.skill2 = ResumeSkill.objects.create( - category="Languages", - skill="French", - position=2 - ) + cls.skill1 = ResumeSkill.objects.create(category="Technical skills", skill="Python", position=1) + cls.skill2 = ResumeSkill.objects.create(category="Languages", skill="French", position=2) def test_skill_created(self): self.assertEqual(self.skill1.skill, "Python") @@ -74,14 +66,14 @@ def setUpTestData(cls): date_attended="2020 - 2021", location="Some Company Ltd", role_details="Testing, developing software, REST APIs", - position=1 + position=1, ) cls.exp2 = ResumeExperience.objects.create( role="Website Developer", date_attended="2021 - 2024", location="Another Enterprise Ltd", role_details="Testing, developing software, REST APIs", - position=2 + position=2, ) def test_experience_created(self):