views.py 2.61 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
def get_bundle_loc(name):
    return "/".join(get_files(name)[0]["url"].split("/")[:-1]) + "/"


Florent Chehab's avatar
Florent Chehab committed
19 20 21 22 23 24
def index(request):
    """
    View to to display the index app that contains the JS / CSS
    The "template" displayed is in ./templates/index.html
    """

25 26
    # We give the user object so that we can access its id in JS
    # and fetch userData
27 28
    user = request.user

29
    # small hack to get the correct location of the frontend bundled files
30
    front_bundle_loc = get_bundle_loc("main")
31

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


41 42 43 44 45 46 47 48 49 50 51
def rgpd_raw(request):
    """
    Render the view that displays only the RGPD conditions
    """
    return render(request, "rgpd_raw.html")


def cgu_rgpd(request):
    """
    Render the view that handles user accepting CGU and RGPD conditions
    """
52 53 54 55 56 57 58 59 60 61
    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)

62
    if "next" in request.GET and user.has_validated_cgu_rgpd:
63 64 65
        # if the user has just validated everything, we redirect him to the location he requested
        return HttpResponseRedirect(request.GET["next"])
    else:
66
        return render(request, "cgu_rgpd.html", dict(user=user, form=form))
67 68 69 70 71 72 73 74 75 76


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


77 78 79 80 81 82 83 84 85 86 87
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