Skip to content

Commit

Permalink
add non-www middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
victoriajury committed Oct 17, 2024
1 parent ef9918a commit 2c4a964
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
18 changes: 18 additions & 0 deletions personal_portfolio/non_www_middleware.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 4 additions & 2 deletions personal_portfolio/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -57,6 +58,7 @@
]

MIDDLEWARE = [
"personal_portfolio.non_www_middleware.NoWWWRedirectMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
Expand Down
20 changes: 6 additions & 14 deletions profile/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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")
Expand All @@ -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):
Expand Down

0 comments on commit 2c4a964

Please sign in to comment.