Skip to content

Commit

Permalink
Merge pull request #221 from hvlads/ssue-218-custom-upload-functionality
Browse files Browse the repository at this point in the history
Implemented custom upload functionality and updated documentation (#218)
  • Loading branch information
hvlads authored May 4, 2024
2 parents 24cbaed + 37e1804 commit 66b24a9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
23 changes: 22 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,29 @@ Quick start
# [ ... ]
urlpatterns += [
path("ckeditor5/", include('django_ckeditor_5.urls'), name="ck_editor_5_upload_file"),
path("ckeditor5/", include('django_ckeditor_5.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Alternatively, you can use your own logic for file uploads. To do this, add the following to your `settings.py` file:

.. code-block:: python
# Define a constant in settings.py to specify the custom upload file view
CK_EDITOR_5_UPLOAD_FILE_VIEW_NAME = "custom_upload_file"
Then, in your `urls.py`, include the custom upload URL pattern:

.. code-block:: python
path("upload/", custom_upload_function, name="custom_upload_file"),
This allows users to customize the upload file logic by specifying their own view function and URL pattern.








4. Add to your `project/models.py`:
Expand Down
4 changes: 3 additions & 1 deletion django_ckeditor_5/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ def render(self, name, value, attrs=None, renderer=None):

context["config"] = self.config
context["script_id"] = "{}{}".format(attrs["id"], "_script")
context["upload_url"] = reverse("ck_editor_5_upload_file")
context["upload_url"] = reverse(
getattr(settings, "CK_EDITOR_5_UPLOAD_FILE_VIEW_NAME", "ck_editor_5_upload_file"),
)
context["upload_file_types"] = json.dumps(getattr(
settings,
"CKEDITOR_5_UPLOAD_FILE_TYPES",
Expand Down
27 changes: 26 additions & 1 deletion example/blog/articles/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from django.http import HttpResponseRedirect
from django.conf import settings
from django.http import Http404, HttpResponseRedirect, JsonResponse
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, FormView, TemplateView
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView

from django_ckeditor_5.forms import UploadFileForm
from django_ckeditor_5.views import NoImageException, handle_uploaded_file, image_verify

from .forms import ArticleForm, CommentForm
from .models import Article

Expand Down Expand Up @@ -52,3 +57,23 @@ def get_success_url(self):
class GetEditorView(TemplateView):
template_name = "articles/dynamic_editor.html"
extra_context = {"form": ArticleForm()}


def custom_upload_file(request):
if request.method == "POST" and request.user.is_staff:
form = UploadFileForm(request.POST, request.FILES)
allow_all_file_types = getattr(
settings,
"CKEDITOR_5_ALLOW_ALL_FILE_TYPES",
False,
)

if not allow_all_file_types:
try:
image_verify(request.FILES["upload"])
except NoImageException as ex:
return JsonResponse({"error": {"message": f"{ex}"}}, status=400)
if form.is_valid():
url = handle_uploaded_file(request.FILES["upload"])
return JsonResponse({"url": url})
raise Http404(_("Page not found."))
2 changes: 1 addition & 1 deletion example/blog/blog/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,5 @@
},
}
CKEDITOR_5_CUSTOM_CSS = "custom.css"

CK_EDITOR_5_UPLOAD_FILE_VIEW_NAME = "custom_upload_file"
CSRF_COOKIE_NAME = "new_csrf_cookie_name"
3 changes: 2 additions & 1 deletion example/blog/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from articles.views import custom_upload_file
from django.conf import settings
from django.conf.urls.static import serve
from django.contrib import admin
from django.urls import include, path, re_path

urlpatterns = [
path("admin/", admin.site.urls),
path("ckeditor5/", include("django_ckeditor_5.urls")),
path("upload/", custom_upload_file, name="custom_upload_file"),
path("", include("articles.urls")),
re_path(r"^media/(?P<path>.*)$", serve, {"document_root": settings.MEDIA_ROOT}),
re_path(r"^static/(?P<path>.*)$", serve, {"document_root": settings.STATIC_ROOT}),
Expand Down
4 changes: 3 additions & 1 deletion example/blog/tests/test_upload_file.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from django.conf import settings
from django.urls import reverse


def test_upload_file(admin_client, file):
with file as upload:
upload_view_name = getattr(settings, "CK_EDITOR_5_UPLOAD_FILE_VIEW_NAME", "")
response = admin_client.post(
reverse("ck_editor_5_upload_file"),
reverse(upload_view_name),
{"upload": upload},
)
assert response.status_code == 200
Expand Down
2 changes: 1 addition & 1 deletion example/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ addopts = """
--cov-report=term-missing:skip-covered
--cov-report=html
--cov-report=xml
--cov-fail-under=80
--cov-fail-under=75
"""

0 comments on commit 66b24a9

Please sign in to comment.