Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
dj-kaos-react / views.py
Size: Mime:
import re

from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import redirect
from django.views.generic import View


class ReactAppView(View):
    def get(self, request):
        try:
            index_path = str(settings.REACT_APP_DIR.path('index.html'))
            with open(index_path) as file:
                return HttpResponse(file.read())
        except FileNotFoundError:
            return HttpResponse(
                """
                index.html not found! Build your React app!!
                """,
                status=501,
            )


def react_static_redirect(request):
    match = re.match(f"^/{settings.REACT_HOME}(.*)", request.path)
    if match:
        return redirect(match.groups()[0])
    return redirect(request.url)