# Debug Only Middleware for Ignis Compatibility from django.conf import settings from django.http import HttpResponseRedirect class IgnisRedirectMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): if settings.DEBUG and request.path.startswith("/ignis/"): new_url = f"https://shi.foo{request.path}" return HttpResponseRedirect(new_url) return self.get_response(request) # [DEPRECATED] # # Ignis Middleware # # Scans all 'img' links # # if they start with '/ignis/' > replaces them with env 'ignis.IGNIS_CACHE_ENDPOINT/' # # if they start with '/static/' > replaces them with env 'static.INGIS_STATIC_ENDPOINT/' # import os # import re # from django.utils.deprecation import MiddlewareMixin # from bs4 import BeautifulSoup # from dotenv import load_dotenv # load_dotenv() # IGNIS_CACHE_ENDPOINT = os.getenv("IGNIS_CACHE_ENDPOINT") # IGNIS_CACHE_PROTOCOL = os.getenv("IGNIS_CACHE_PROTOCOL") # DYNAMIC_ENDPOINT = f"{IGNIS_CACHE_PROTOCOL}://ignis.{IGNIS_CACHE_ENDPOINT}" # STATIC_ENDPOINT = f"{IGNIS_CACHE_PROTOCOL}://static.{IGNIS_CACHE_ENDPOINT}" # class IgnisMiddleware(MiddlewareMixin): # def __init__(self, get_response): # self.get_response = get_response # def __call__(self, request): # response = self.get_response(request) # # if request is from localhost or 127.0.0.1, do not process # if re.match(r"^localhost", request.get_host()) or re.match( # r"^127.0.0.1", request.get_host() # ): # return response # # if request is made to /anidata or /admin, do not process # if re.match(r"^/anidata", request.path) or re.match(r"^/admin", request.path): # return response # # Do not process non-HTML responses # if not response["Content-Type"].startswith("text/html"): # return response # response.content = self.process_response(response) # return response # def process_response(self, response): # content = response.content.decode("utf-8") # soup = BeautifulSoup(content, "html.parser") # for image in soup.find_all("img"): # src = image.get("src") # if src.startswith("/ignis/"): # image["src"] = f"{DYNAMIC_ENDPOINT}{src[6:]}" # elif src.startswith("/static/"): # image["src"] = f"{STATIC_ENDPOINT}{src[7:]}" # return str(soup)