diff --git a/compose/local/django/Dockerfile b/compose/local/django/Dockerfile index d3e1b32..bfad03a 100644 --- a/compose/local/django/Dockerfile +++ b/compose/local/django/Dockerfile @@ -13,7 +13,10 @@ RUN apk update \ # Translations dependencies && apk add gettext \ # https://docs.djangoproject.com/en/dev/ref/django-admin/#dbshell - && apk add postgresql-client + && apk add postgresql-client \ + # https://weasyprint.readthedocs.io/en/latest/install.html + && apk add gcc musl-dev jpeg-dev zlib-dev libffi-dev cairo-dev pango-dev gdk-pixbuf-dev \ + && apk add ttf-ubuntu-font-family # Requirements are installed here to ensure they will be cached. COPY ./requirements /requirements diff --git a/compose/production/django/Dockerfile b/compose/production/django/Dockerfile index 29e6d2e..6f337e6 100644 --- a/compose/production/django/Dockerfile +++ b/compose/production/django/Dockerfile @@ -10,7 +10,10 @@ RUN apk update \ # Pillow dependencies && apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev \ # CFFI dependencies - && apk add libffi-dev py-cffi + && apk add libffi-dev py-cffi \ + # https://weasyprint.readthedocs.io/en/latest/install.html + && apk add gcc musl-dev jpeg-dev zlib-dev libffi-dev cairo-dev pango-dev gdk-pixbuf-dev \ + && apk add ttf-ubuntu-font-family RUN addgroup -S django \ && adduser -S -G django django diff --git a/requirements/base.txt b/requirements/base.txt index b2cbda8..63f91df 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -20,3 +20,5 @@ djangorestframework==3.10.2 # https://github.com/encode/django-rest-framework coreapi==2.3.3 # https://github.com/core-api/python-client django-markdownx==2.0.28 +WeasyPrint==48 +django-weasyprint==0.5.4 diff --git a/schickmacher/renderer/urls.py b/schickmacher/renderer/urls.py index 62b4517..9f32ba6 100644 --- a/schickmacher/renderer/urls.py +++ b/schickmacher/renderer/urls.py @@ -1,8 +1,9 @@ from django.urls import path -from schickmacher.renderer.views import form_view +from schickmacher.renderer.views import form_view, RenderView app_name = 'renderer' urlpatterns = [ - path("", view=form_view, name="form") + path("", view=form_view, name="form"), + path("render", view=RenderView.as_view(), name="render") ] diff --git a/schickmacher/renderer/views.py b/schickmacher/renderer/views.py index caa40dc..ea861e9 100644 --- a/schickmacher/renderer/views.py +++ b/schickmacher/renderer/views.py @@ -1,5 +1,7 @@ from django.core.handlers.wsgi import WSGIRequest from django.shortcuts import render +from django.views.generic import FormView +from django_weasyprint import WeasyTemplateView from markdownx.utils import markdownify from schickmacher.renderer.forms import RendererForm @@ -18,3 +20,25 @@ def form_view(request: WSGIRequest): form = RendererForm() ctx = {"form": form} return render(request, 'renderer/render_form.html', ctx) + + +class RenderView(WeasyTemplateView): + template_name = 'renderer/rendered.html' + + def get_context_data(self, **kwargs): + form = RendererForm(self.request.POST) + if form.is_valid(): + return { + 'date': form.cleaned_data['date'], + 'content': markdownify(form.cleaned_data['render_content']), + 'title': form.cleaned_data['title'] + } + else: + return { + 'date': 'Error', + 'content': 'Error', + 'title': 'Error' + } + + def post(self, request, *args, **kwargs): + return self.get(request, *args, **kwargs) diff --git a/schickmacher/templates/renderer/render_form.html b/schickmacher/templates/renderer/render_form.html index 192c8c5..3c2214a 100644 --- a/schickmacher/templates/renderer/render_form.html +++ b/schickmacher/templates/renderer/render_form.html @@ -8,7 +8,7 @@ {% block content %}

{% trans "Renderer" %}

-
+ {% csrf_token %} {{ form|crispy }}