Skip to content

Commit

Permalink
🎨 remove domain forcing
Browse files Browse the repository at this point in the history
  • Loading branch information
itsthejoker committed Mar 20, 2023
1 parent 529a7f6 commit da7bed8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 71 deletions.
2 changes: 1 addition & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
line_length = 88
multi_line_output = 3
include_trailing_comma = True
known_third_party = better_exceptions,blossom_wrapper,click,decorator_include,django,django_filters,dotenv,dpath,drf_yasg,ipware,markdown,mimesis,opentelemetry,praw,prawcore,psaw,pytest,pytest_django,pytz,pyuwsgi,requests,rest_framework,rest_framework_api_key,revproxy,shiv,slack,social_core,social_django,stripe,toml
known_third_party = better_exceptions,blossom_wrapper,click,django,django_filters,dotenv,dpath,drf_yasg,ipware,markdown,mimesis,opentelemetry,praw,prawcore,psaw,pytest,pytest_django,pytz,pyuwsgi,requests,rest_framework,rest_framework_api_key,revproxy,shiv,slack,social_core,social_django,stripe,toml
skip=venv,.venv,env,migrations
78 changes: 8 additions & 70 deletions blossom/urls.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
from functools import wraps
from typing import Any, Callable

from decorator_include import decorator_include
from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static
from django.contrib import admin
from django.http import HttpRequest
from django.shortcuts import redirect
from django.urls import path
from django.urls import include, path

from blossom.authentication.views import LoginView
from blossom.website.views import user_create
Expand All @@ -19,76 +13,20 @@
handler404 = "blossom.website.views.handler404"
handler500 = "blossom.website.views.handler500"


def force_domain(domain: [str, None]) -> Any:
"""Enforce the appropriate domain name for a given route."""

def decorator(func: Callable) -> Any:
"""Enforce the appropriate domain name for a given route."""

@wraps(func)
def inner_func(request: HttpRequest, *args: list, **kwargs: dict) -> Any:
if settings.DEBUG or request.get_host() == "testserver":
# Patch the request with either the configured hostname for testing
# or the host that's required for this route.
request.get_host = (
lambda: settings.OVERRIDE_HOST if settings.OVERRIDE_HOST else domain
)
return func(request, *args, **kwargs)

if not domain:
# This is so that we can set the request manipulation when in debug mode
# on routes that otherwise don't get forced one way or another.
return func(request, *args, **kwargs)

if request.get_host() != domain:
# The request came in on the wrong domain, so issue a redirect for
# the same route so they come in from the right site.
return redirect(request.scheme + "://" + domain + request.path)
# everything's groovy -- let's roll
return func(request, *args, **kwargs)

return inner_func

return decorator


# domainless urls
urlpatterns = [
path("superadmin/newuser", user_create, name="user_create"),
path("superadmin/", admin.site.urls),
path("", decorator_include(force_domain(None), "blossom.authentication.urls")),
path("api/", decorator_include(force_domain(None), "blossom.api.urls")),
]

# grafeas urls
urlpatterns += [
path(
"payments/",
decorator_include(force_domain("grafeas.org"), "blossom.payments.urls"),
),
path(
"engineering/",
decorator_include(force_domain("grafeas.org"), "blossom.engineeringblog.urls"),
),
path("", decorator_include(force_domain("grafeas.org"), "blossom.website.urls")),
path("", include("blossom.authentication.urls")),
path("api/", include("blossom.api.urls")),
path("payments/", include("blossom.payments.urls")),
path("engineering/", include("blossom.engineeringblog.urls")),
path("", include("blossom.website.urls")),
]

# thetranscription.app urls
if settings.ENABLE_APP:
urlpatterns += [
path(
"app/",
decorator_include(force_domain("thetranscription.app"), "blossom.app.urls"),
),
url(
"",
decorator_include(
force_domain("thetranscription.app"),
"social_django.urls",
namespace="social",
),
),
path("app/", include("blossom.app.urls")),
url("app_login/", include("social_django.urls")),
]

if settings.DEBUG:
Expand Down

0 comments on commit da7bed8

Please sign in to comment.