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.
|
||||
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 .models import Huisstijl
|
||||
|
|
@ -95,11 +96,12 @@ def analyseer_logo(f):
|
|||
|
||||
|
||||
@api_view(["GET", "PUT", "POST", "DELETE"])
|
||||
@parser_classes([MultiPartParser, FormParser, JSONParser])
|
||||
def huisstijl(request):
|
||||
obj = Huisstijl.actueel()
|
||||
schrijven = request.method in ("PUT", "POST", "DELETE")
|
||||
if schrijven and not (request.user and request.user.is_staff):
|
||||
return Response({"detail": "Alleen beheerders kunnen het logo wijzigen."}, status=403)
|
||||
if schrijven and not (request.user and request.user.is_authenticated and request.user.is_staff):
|
||||
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 obj.logo:
|
||||
|
|
@ -110,14 +112,20 @@ def huisstijl(request):
|
|||
f = request.FILES.get("logo")
|
||||
if not f:
|
||||
return Response({"detail": "Geen bestand ontvangen (veld 'logo')."}, status=400)
|
||||
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"]:
|
||||
return Response({"detail": " ".join(analyse["fouten"]), "kwaliteit": analyse}, status=400)
|
||||
try:
|
||||
f.seek(0)
|
||||
if obj.logo:
|
||||
obj.logo.delete(save=False)
|
||||
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["kwaliteit"] = analyse
|
||||
return Response(data)
|
||||
|
|
|
|||
|
|
@ -59,7 +59,8 @@ export async function uploadLogo(file) {
|
|||
let data = null;
|
||||
try { data = await res.json(); } catch { data = null; }
|
||||
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.data = data;
|
||||
throw err;
|
||||
|
|
|
|||
Loading…
Reference in a new issue