Weasy Renderer

This commit is contained in:
Jens Kadenbach 2019-09-11 15:37:45 +02:00
parent d5dcfd5fa2
commit ccdd2c2982
6 changed files with 38 additions and 5 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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")
]

View file

@ -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)

View file

@ -8,7 +8,7 @@
{% block content %}
<h1>{% trans "Renderer" %}</h1>
<form class="form-horizontal" method="post" action="{% url 'renderer:form' %}">
<form class="form-horizontal" method="post" action="{% url 'renderer:render' %}">
{% csrf_token %}
{{ form|crispy }}
<div class="control-group">