django-markdownx/markdownx/views.py
Pouria Hadjibagheri 52c9a1e670
Add GH Actions (#218)
* updates URLs to Django 4 pattern

* Checks for Ajax request using `x-requested-with` header

* housekeeping

* housekeeping

* adds zlib to windows runs

* uses specific python version

* updates git URL

* disables testing on Windows

* changes docs path

* changes docs path

* changes docs path

* Update documentations

* removes docs

Co-authored-by: GitHub Action <action@github.com>
2022-01-03 14:25:46 +00:00

81 lines
2.7 KiB
Python
Executable file

from django.http import HttpResponse, JsonResponse
from django.utils.module_loading import import_string
from django.views.generic.edit import View, BaseFormView
from .forms import ImageForm
from .settings import MARKDOWNX_MARKDOWNIFY_FUNCTION
class MarkdownifyView(View):
"""
Conversion of Markdown to HTML.
"""
def post(self, request, *args, **kwargs):
"""
Handling of the conversion from Markdown to HTML using the conversion
function in settings under ``MARKDOWNX_MARKDOWNIFY_FUNCTION``.
:param request: HTTP request.
:param args: Default Django POST arguments.
:param kwargs: Default Django POST keyword arguments.
:return: HTTP response
:rtype: django.http.HttpResponse
"""
markdownify = import_string(MARKDOWNX_MARKDOWNIFY_FUNCTION)
return HttpResponse(markdownify(request.POST['content']))
class ImageUploadView(BaseFormView):
"""
Handling requests for uploading images.
"""
# template_name = "dummy.html"
form_class = ImageForm
success_url = '/'
def form_invalid(self, form):
"""
Handling of invalid form events.
:param form: Django form instance.
:type form: django.forms.Form
:return: JSON response with the HTTP-400 error message for AJAX requests
and the default response for HTTP requests.
:rtype: django.http.JsonResponse, django.http.HttpResponse
"""
if self.request.is_ajax():
return JsonResponse(form.errors, status=400)
response = super(ImageUploadView, self).form_invalid(form)
return response
def form_valid(self, form):
"""
If the form is valid, the contents are saved.
If the **POST** request is AJAX (image uploads), a JSON response will be
produced containing the Markdown encoded image insertion tag with the URL
using which the uploaded image may be accessed.
JSON response would be as follows:
.. code-block:: bash
{ image_code: "![](/media/image_directory/123-4e6-ga3.png)" }
:param form: Django form instance.
:type form: django.forms.Form
:return: JSON encoded Markdown tag for AJAX requests, and an appropriate
response for HTTP requests.
:rtype: django.http.JsonResponse, django.http.HttpResponse
"""
response = super(ImageUploadView, self).form_valid(form)
if self.request.headers.get('x-requested-with') == 'XMLHttpRequest':
image_path = form.save(commit=True)
image_code = '![]({})'.format(image_path)
return JsonResponse({'image_code': image_code})
return response