Skip to content

Commit

Permalink
Drop static preview support for redirect app
Browse files Browse the repository at this point in the history
Links have been removed as part of #681.

Closes #680.
  • Loading branch information
jchristgit committed Sep 16, 2023
1 parent 8f9b8ec commit c46f64c
Showing 1 changed file with 8 additions and 70 deletions.
78 changes: 8 additions & 70 deletions pydis_site/apps/redirect/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@

import yaml
from django import conf
from django.http import HttpResponse
from django.urls import URLPattern, path
from django_distill import distill_path

from pydis_site import settings
from pydis_site.apps.content import urls as pages_urls
from pydis_site.apps.redirect.views import CustomRedirectView
from pydis_site.apps.resources import urls as resources_urls

app_name = "redirect"

Expand All @@ -31,72 +26,15 @@ class Redirect:


def map_redirect(name: str, data: Redirect) -> list[URLPattern]:
"""Return a pattern using the Redirects app, or a static HTML redirect for static builds."""
if not settings.STATIC_BUILD:
# Normal dynamic redirect
return [path(
data.original_path,
CustomRedirectView.as_view(
pattern_name=data.redirect_route,
static_args=tuple(data.redirect_arguments),
prefix_redirect=data.prefix_redirect
),
name=name
)]

# Create static HTML redirects for static builds
new_app_name = data.redirect_route.split(":")[0]

if __PARAMETER_REGEX.search(data.original_path):
# Redirects for paths which accept parameters
# We generate an HTML redirect file for all possible entries
paths = []

class RedirectFunc:
def __init__(self, new_url: str, _name: str):
self.result = HttpResponse(REDIRECT_TEMPLATE.format(url=new_url))
self.__qualname__ = _name

def __call__(self, *args, **kwargs):
return self.result

if new_app_name == resources_urls.app_name:
items = resources_urls.get_all_resources()
elif new_app_name == pages_urls.app_name:
items = pages_urls.get_all_pages()
else:
raise ValueError(f"Unknown app in redirect: {new_app_name}")

for item in items:
entry = next(iter(item.values()))

# Replace dynamic redirect with concrete path
concrete_path = __PARAMETER_REGEX.sub(entry, data.original_path)
new_redirect = f"/{new_app_name}/{entry}"
pattern_name = f"{name}_{entry}"

paths.append(distill_path(
concrete_path,
RedirectFunc(new_redirect, pattern_name),
name=pattern_name
))

return paths

redirect_path_name = "pages" if new_app_name == "content" else new_app_name
if len(data.redirect_arguments) > 0:
redirect_arg = data.redirect_arguments[0]
else:
redirect_arg = "resources/"
new_redirect = f"/{redirect_path_name}/{redirect_arg}"

if new_redirect == "/resources/resources/":
new_redirect = "/resources/"

return [distill_path(
"""Return a pattern using the Redirects app."""
return [path(
data.original_path,
lambda *args: HttpResponse(REDIRECT_TEMPLATE.format(url=new_redirect)),
name=name,
CustomRedirectView.as_view(
pattern_name=data.redirect_route,
static_args=tuple(data.redirect_arguments),
prefix_redirect=data.prefix_redirect
),
name=name
)]


Expand Down

0 comments on commit c46f64c

Please sign in to comment.