views.py 2.34 KB
Newer Older
1 2 3
import logging

from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
Florent Chehab's avatar
Florent Chehab committed
4
from django.shortcuts import render
5
from webpack_loader.utils import get_files
Florent Chehab's avatar
Florent Chehab committed
6

7 8
from backend_app.utils import clean_route
from backend_app.viewsets import ALL_VIEWSETS
9 10 11 12
from base_app.forms import UserForm
from base_app.models import User

logger = logging.getLogger("django")
13

Florent Chehab's avatar
Florent Chehab committed
14 15 16 17 18 19 20

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

21 22
    # We give the user object so that we can access its id in JS
    # and fetch userData
23 24
    user = request.user

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

28 29
    # We also retrieve the list of all routes endpoints
    endpoints = list(map(lambda v: clean_route(v.end_point_route), ALL_VIEWSETS))
30 31 32 33 34 35 36
    return render(
        request,
        "index.html",
        dict(user=user, endpoints=endpoints, front_bundle_loc=front_bundle_loc),
    )


37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
def cgu(request):
    if request.method == "POST":
        user = User.objects.get(pk=request.user.pk)
        form = UserForm(request.POST, instance=user)
        if form.is_valid():
            user = form.save(commit=True)
            user.save()
    else:
        user = User.objects.get(pk=request.user.pk)
        form = UserForm(instance=user)

    if "next" in request.GET and user.has_validated_cgu and user.has_validated_rgpd:
        # if the user has just validated everything, we redirect him to the location he requested
        return HttpResponseRedirect(request.GET["next"])
    else:
        return render(request, "cgu.html", dict(user=user, form=form))


def banned(request):
    user = request.user
    if user.is_banned:
        return render(request, "banned.html", dict(user=user))
    else:
        return HttpResponseNotFound()


63 64 65 66 67 68 69 70 71 72 73
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