53 lines
1.5 KiB
Python
53 lines
1.5 KiB
Python
# Create your views here.
|
|
import requests
|
|
from django.conf import settings
|
|
from django.http import JsonResponse, HttpResponse
|
|
from django.shortcuts import render
|
|
from django.views.decorators.csrf import ensure_csrf_cookie
|
|
from ratelimit.decorators import ratelimit
|
|
|
|
from vbv_lernwelt.core.middleware.auth import django_view_authentication_exempt
|
|
|
|
|
|
@django_view_authentication_exempt
|
|
@ensure_csrf_cookie
|
|
def vue_home(request):
|
|
if settings.IT_SERVE_VUE:
|
|
try:
|
|
res = requests.get(f'{settings.IT_SERVE_VUE_URL}{request.get_full_path()}')
|
|
headers = res.headers
|
|
content_type = headers.get('content-type', 'text/html')
|
|
return HttpResponse(res.text, content_type=content_type)
|
|
except Exception as e:
|
|
print(f'Can not connect to vue dev server at {settings.IT_SERVE_VUE_URL}:', e)
|
|
return
|
|
|
|
# render index.html from `npm run build`
|
|
return render(request, 'index.html', {})
|
|
|
|
|
|
def permission_denied_view(request, exception):
|
|
return render(request, "403.html", status=403)
|
|
|
|
|
|
def rate_limit_exceeded_view(request, exception):
|
|
return render(request, "429.html", status=429)
|
|
|
|
|
|
@django_view_authentication_exempt
|
|
def server_json_error(request, *args, **kwargs):
|
|
"""
|
|
Generic 500 error handler.
|
|
"""
|
|
data = {
|
|
"detail": "Server Error (500)",
|
|
"status_code": 500,
|
|
}
|
|
return JsonResponse(data, status=500)
|
|
|
|
|
|
@ratelimit(key="ip", rate="5/m", block=True)
|
|
@django_view_authentication_exempt
|
|
def check_rate_limit(request):
|
|
return HttpResponse(content=b"Hello")
|