diff --git a/web/core/views.py b/web/core/views.py index e28c2c2..315baa1 100644 --- a/web/core/views.py +++ b/web/core/views.py @@ -1245,4 +1245,51 @@ def set_theme(request): request.session["theme"] = theme messages.success(request, f"Theme set to {theme.title()}.") - return redirect("settings_home") \ No newline at end of file + return redirect("settings_home") + +# web/core/views.py +import json +import os +from django.conf import settings +from django.contrib.auth.decorators import login_required, user_passes_test +from django.http import JsonResponse, HttpResponseBadRequest +from django.views.decorators.http import require_POST + +@require_POST +@login_required +@user_passes_test(lambda u: u.is_superuser) +def api_update_pub_codes(request): + """ + Accepts a 'json' field (string) that should parse to {"pub_codes": [..]}. + Normalizes, de-duplicates, and writes to web/static/data/wol-pub-codes.v1.json. + """ + payload = request.POST.get("json") or (request.body.decode("utf-8") if request.body else "") + if not payload: + return HttpResponseBadRequest("Missing 'json'.") + + try: + data = json.loads(payload) + except Exception as e: + return HttpResponseBadRequest(f"Invalid JSON: {e}") + + if not isinstance(data, dict) or "pub_codes" not in data or not isinstance(data["pub_codes"], list): + return HttpResponseBadRequest('JSON must be an object with a "pub_codes" array.') + + # Normalize to unique, lowercase, trimmed strings + seen = set() + codes = [] + for c in data["pub_codes"]: + s = str(c or "").strip().lower() + if s and s not in seen: + seen.add(s) + codes.append(s) + + # Write back to static data file + target_path = os.path.join(settings.BASE_DIR, "web", "static", "data", "wol-pub-codes.v1.json") + try: + with open(target_path, "w", encoding="utf-8") as f: + json.dump({"pub_codes": codes}, f, ensure_ascii=False, indent=2) + except Exception as e: + return HttpResponseBadRequest(f"Could not write file: {e}") + + return JsonResponse({"ok": True, "count": len(codes)}) \ No newline at end of file