huisstijl: leesbare uploadfouten + expliciete multipart-parser
This commit is contained in:
parent
e8934c6352
commit
7a5d3b75eb
2 changed files with 19 additions and 10 deletions
|
|
@ -5,7 +5,8 @@ PUT /api/huisstijl/ -> upload logo (alleen beheerders); voert een
|
||||||
kwaliteitscheck uit en geeft waarschuwingen terug.
|
kwaliteitscheck uit en geeft waarschuwingen terug.
|
||||||
DELETE /api/huisstijl/ -> logo verwijderen (alleen beheerders).
|
DELETE /api/huisstijl/ -> logo verwijderen (alleen beheerders).
|
||||||
"""
|
"""
|
||||||
from rest_framework.decorators import api_view
|
from rest_framework.decorators import api_view, parser_classes
|
||||||
|
from rest_framework.parsers import FormParser, JSONParser, MultiPartParser
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
from .models import Huisstijl
|
from .models import Huisstijl
|
||||||
|
|
@ -95,11 +96,12 @@ def analyseer_logo(f):
|
||||||
|
|
||||||
|
|
||||||
@api_view(["GET", "PUT", "POST", "DELETE"])
|
@api_view(["GET", "PUT", "POST", "DELETE"])
|
||||||
|
@parser_classes([MultiPartParser, FormParser, JSONParser])
|
||||||
def huisstijl(request):
|
def huisstijl(request):
|
||||||
obj = Huisstijl.actueel()
|
obj = Huisstijl.actueel()
|
||||||
schrijven = request.method in ("PUT", "POST", "DELETE")
|
schrijven = request.method in ("PUT", "POST", "DELETE")
|
||||||
if schrijven and not (request.user and request.user.is_staff):
|
if schrijven and not (request.user and request.user.is_authenticated and request.user.is_staff):
|
||||||
return Response({"detail": "Alleen beheerders kunnen het logo wijzigen."}, status=403)
|
return Response({"detail": "Alleen beheerders (is_staff) kunnen het logo wijzigen. Vraag een beheerder om het logo in te stellen."}, status=403)
|
||||||
|
|
||||||
if request.method == "DELETE":
|
if request.method == "DELETE":
|
||||||
if obj.logo:
|
if obj.logo:
|
||||||
|
|
@ -110,14 +112,20 @@ def huisstijl(request):
|
||||||
f = request.FILES.get("logo")
|
f = request.FILES.get("logo")
|
||||||
if not f:
|
if not f:
|
||||||
return Response({"detail": "Geen bestand ontvangen (veld 'logo')."}, status=400)
|
return Response({"detail": "Geen bestand ontvangen (veld 'logo')."}, status=400)
|
||||||
analyse = analyseer_logo(f)
|
try:
|
||||||
|
analyse = analyseer_logo(f)
|
||||||
|
except Exception as e: # noqa: BLE001 — geef een leesbare reden i.p.v. 500.
|
||||||
|
return Response({"detail": f"Kon de afbeelding niet verwerken: {e}"}, status=400)
|
||||||
if not analyse["ok"]:
|
if not analyse["ok"]:
|
||||||
return Response({"detail": " ".join(analyse["fouten"]), "kwaliteit": analyse}, status=400)
|
return Response({"detail": " ".join(analyse["fouten"]), "kwaliteit": analyse}, status=400)
|
||||||
f.seek(0)
|
try:
|
||||||
if obj.logo:
|
f.seek(0)
|
||||||
obj.logo.delete(save=False)
|
if obj.logo:
|
||||||
obj.logo = f
|
obj.logo.delete(save=False)
|
||||||
obj.save()
|
obj.logo = f
|
||||||
|
obj.save()
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
return Response({"detail": f"Opslaan van het logo mislukte: {e}"}, status=400)
|
||||||
data = HuisstijlSerializer(obj, context={"request": request}).data
|
data = HuisstijlSerializer(obj, context={"request": request}).data
|
||||||
data["kwaliteit"] = analyse
|
data["kwaliteit"] = analyse
|
||||||
return Response(data)
|
return Response(data)
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,8 @@ export async function uploadLogo(file) {
|
||||||
let data = null;
|
let data = null;
|
||||||
try { data = await res.json(); } catch { data = null; }
|
try { data = await res.json(); } catch { data = null; }
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const err = new Error((data && (data.detail || JSON.stringify(data))) || "Upload mislukt");
|
const reden = (data && (data.detail || JSON.stringify(data))) || `Upload mislukt (HTTP ${res.status} ${res.statusText || ""})`.trim();
|
||||||
|
const err = new Error(reden);
|
||||||
err.status = res.status;
|
err.status = res.status;
|
||||||
err.data = data;
|
err.data = data;
|
||||||
throw err;
|
throw err;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue