views.py 1.34 KB
Newer Older
1
from django.http import HttpResponse
Florent Chehab's avatar
Florent Chehab committed
2
from django.shortcuts import render
3
from webpack_loader.utils import get_files
Florent Chehab's avatar
Florent Chehab committed
4

5
6
7
from backend_app.utils import clean_route
from backend_app.viewsets import ALL_VIEWSETS

Florent Chehab's avatar
Florent Chehab committed
8
9
10
11
12
13
14

def index(request):
    """
    View to to display the index app that contains the JS / CSS
    The "template" displayed is in ./templates/index.html
    """

15
16
    # We give the user object so that we can access its id in JS
    # and fetch userData
17
18
    user = request.user

19
20
21
    # small hack to get the correct location of the frontend bundled files
    front_bundle_loc = "/".join(get_files("main")[0]["url"].split("/")[:-1]) + "/"

22
23
    # We also retrieve the list of all routes endpoints
    endpoints = list(map(lambda v: clean_route(v.end_point_route), ALL_VIEWSETS))
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
    return render(
        request,
        "index.html",
        dict(user=user, endpoints=endpoints, front_bundle_loc=front_bundle_loc),
    )


def media_files_view(request, path):
    """
    Media files are served by nginx only if the user is connected.
    The authentication checked is performed through the middleware
    so here we only need to return a dumb request with the right headers
    that will be read by nginx.
    """
    response = HttpResponse()
    del response["Content-Type"]
    response["X-Accel-Redirect"] = "/protected-assets/media/" + path
    return response