From 8c47e9f5ed5178227fd566b7f1bf6175d092ff02 Mon Sep 17 00:00:00 2001 From: yaghoubi Date: Sun, 23 Feb 2025 13:44:49 +0330 Subject: [PATCH] add upload handler to json-editor --- django_daisy/admin.py | 37 +++++++++- .../static/admin/fields/json-editor/init.js | 68 +++++++++++++++++++ .../templates/fields/json_editor_field.html | 1 - 3 files changed, 104 insertions(+), 2 deletions(-) diff --git a/django_daisy/admin.py b/django_daisy/admin.py index 6174101..396b601 100644 --- a/django_daisy/admin.py +++ b/django_daisy/admin.py @@ -1,11 +1,18 @@ +import logging + from django.apps import apps from django.contrib import admin +from django.core.files.storage import FileSystemStorage from django.db import models +from django.http import HttpResponse from django.shortcuts import render -from django.urls import reverse +from django.urls import reverse, path +from django.views.decorators.csrf import csrf_exempt from django_daisy.module_settings import DAISY_SETTINGS +logger = logging.getLogger(__name__) + # Remove default form fields for specific date and time fields admin.options.FORMFIELD_FOR_DBFIELD_DEFAULTS.pop(models.DateTimeField, None) admin.options.FORMFIELD_FOR_DBFIELD_DEFAULTS.pop(models.DateField, None) @@ -20,6 +27,12 @@ class DaisyAdminSite(admin.AdminSite): index_title = DAISY_SETTINGS.get('SITE_HEADER', 'hi, welcome to your dashboard') logo = DAISY_SETTINGS.get('SITE_LOGO', '/static/admin/img/daisyui-logomark.svg') + def get_urls(self): + urls = [ + path('json-editor-upload-handler/', self.admin_view(self.upload_file)) + ] + return urls + super().get_urls() + def get_log_entries(self, request): from django.contrib.admin.models import LogEntry @@ -98,3 +111,25 @@ def each_context(self, request): def get_logo(self, request): return self.logo + + @csrf_exempt + def upload_file(self, request): + if request.method != 'POST': + return HttpResponse('Invalid request method', status=405, content_type='text/plain') + + if 'file' not in request.FILES: + return HttpResponse('No file uploaded', status=400, content_type='text/plain') + + file = request.FILES['file'] + + try: + fs = FileSystemStorage() + filename = fs.save(file.name, file) + url = request.build_absolute_uri(fs.url(filename)) + return HttpResponse(url, content_type='text/plain') + + except Exception as e: + # Log the error for debugging purposes + logger.error('Error saving file: %s', e) + # Return a generic error message to the client + return HttpResponse('Failed to save file', status=500, content_type='text/plain') diff --git a/django_daisy/static/admin/fields/json-editor/init.js b/django_daisy/static/admin/fields/json-editor/init.js index 82cdd4d..cfc12c6 100644 --- a/django_daisy/static/admin/fields/json-editor/init.js +++ b/django_daisy/static/admin/fields/json-editor/init.js @@ -1,4 +1,72 @@ +function getCookie(name) { + // Split document.cookie into individual cookie strings + const cookies = document.cookie.split(';'); + + // Iterate through each cookie + for (let cookie of cookies) { + // Trim whitespace and split into name-value pair + const [cookieName, cookieValue] = cookie.trim().split('='); + + // Check if this is the cookie we're looking for + if (cookieName === name) { + // Return the decoded value + return decodeURIComponent(cookieValue); + } + } + + // Return null if cookie not found + return null; +} + $(document).ready(function () { + + JSONEditor.defaults.callbacks.upload = { + "defaultUploadHandler": function (jseditor, type, file, cbs) { + // Create a new XMLHttpRequest object + var xhr = new XMLHttpRequest(); + let django_lang = getCookie("django_language") + let url = "" + if (django_lang) { + url = `/${django_lang}/admin/json-editor-upload-handler/` + } else { + url = '/admin/json-editor-upload-handler/' + } + + // Configure it to send a POST request to the server endpoint + xhr.open('POST', url, true); + + // Handle upload progress + xhr.upload.addEventListener('progress', function (event) { + if (event.lengthComputable) { + var percent = Math.round((event.loaded / event.total) * 100); + cbs.updateProgress(percent); // Update progress bar with percentage + } + }); + + // Handle successful upload + xhr.addEventListener('load', function () { + if (xhr.status >= 200 && xhr.status < 300) { + var response = xhr.responseText; // Server returns the URL as plain text + cbs.success(response); // Call success callback with the URL + } else { + cbs.failure('Upload failed: ' + xhr.statusText); // Call failure callback with error + } + }); + + // Handle upload errors + xhr.addEventListener('error', function () { + cbs.failure('Upload failed'); // Call failure callback on network error + }); + + // Prepare the file for upload + var formData = new FormData(); + formData.append('file', file); // Append the file to FormData + + // Send the request to the server + xhr.send(formData); + } + }; + function init_editor_json_editor( editor, schema, diff --git a/django_daisy/templates/fields/json_editor_field.html b/django_daisy/templates/fields/json_editor_field.html index a656aed..757fa4d 100755 --- a/django_daisy/templates/fields/json_editor_field.html +++ b/django_daisy/templates/fields/json_editor_field.html @@ -8,4 +8,3 @@
{% endwith %} -