Render markdown

This commit is contained in:
Jens Kadenbach 2019-09-05 13:12:30 +02:00
parent 8b16fe573c
commit d5dcfd5fa2
13 changed files with 52 additions and 13 deletions

View file

@ -69,6 +69,7 @@ THIRD_PARTY_APPS = [
"allauth.account", "allauth.account",
"allauth.socialaccount", "allauth.socialaccount",
"rest_framework", "rest_framework",
"markdownx",
] ]
LOCAL_APPS = [ LOCAL_APPS = [

View file

@ -7,15 +7,13 @@ from django.views import defaults as default_views
urlpatterns = [ urlpatterns = [
path("", RedirectView.as_view(url=reverse_lazy('renderer:form')), name="home"), path("", RedirectView.as_view(url=reverse_lazy('renderer:form')), name="home"),
path(
"about/", TemplateView.as_view(template_name="pages/about.html"), name="about"
),
# Django Admin, use {% url 'admin:index' %} # Django Admin, use {% url 'admin:index' %}
path(settings.ADMIN_URL, admin.site.urls), path(settings.ADMIN_URL, admin.site.urls),
# User management # User management
path("users/", include("schickmacher.users.urls", namespace="users")), path("users/", include("schickmacher.users.urls", namespace="users")),
path("renderer/", include("schickmacher.renderer.urls", namespace="renderer")), path("renderer/", include("schickmacher.renderer.urls", namespace="renderer")),
path("accounts/", include("allauth.urls")), path("accounts/", include("allauth.urls")),
path(r'markdownx/', include('markdownx.urls')),
# Your stuff: custom urls includes go here # Your stuff: custom urls includes go here
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View file

@ -18,3 +18,5 @@ django-redis==4.10.0 # https://github.com/niwinz/django-redis
# Django REST Framework # Django REST Framework
djangorestframework==3.10.2 # https://github.com/encode/django-rest-framework djangorestframework==3.10.2 # https://github.com/encode/django-rest-framework
coreapi==2.3.3 # https://github.com/core-api/python-client coreapi==2.3.3 # https://github.com/core-api/python-client
django-markdownx==2.0.28

View file

@ -1,15 +1,16 @@
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django import forms from django import forms
from django.utils import timezone from django.utils import timezone
from markdownx.fields import MarkdownxFormField
class RendererForm(forms.Form): class RendererForm(forms.Form):
title = forms.CharField(label=_('Title'), required=False)
date = forms.DateField(label=_('Date'), date = forms.DateField(label=_('Date'),
initial=lambda: timezone.now().strftime('%Y-%m-%d'), initial=lambda: timezone.now().strftime('%Y-%m-%d'),
widget=forms.TextInput( widget=forms.TextInput(
attrs={'type': 'date'} attrs={'type': 'date'}
)) ))
render_content = forms.CharField( render_content = MarkdownxFormField(
label=_('Content'), widget=forms.Textarea, max_length=None, label=_('Content'),
required=False, initial=""
) )

View file

@ -10,7 +10,8 @@ class TestRendererForm:
def test_form_accepts_content(self): def test_form_accepts_content(self):
form = RendererForm({ form = RendererForm({
"date": timezone.now(), "date": timezone.now(),
"render_content": "Example **content**" "render_content": "Example **content**",
"title": "Test"
}) })
assert form.is_valid() assert form.is_valid()

View file

@ -13,3 +13,14 @@ class TestRendererFormView:
assert response.status_code == 200 assert response.status_code == 200
assert response.context['form']['date'].value() \ assert response.context['form']['date'].value() \
== timezone.now().strftime('%Y-%m-%d') == timezone.now().strftime('%Y-%m-%d')
def test_render(self, client: Client):
response = client.post('/renderer/', data={
'title': 'Sample Title',
'date': '2009-11-15',
'render_content': '# Example'
})
assert b'Sample Title' in response.content
assert b'15.11.2009' in response.content
assert b'Example' in response.content

View file

@ -1,9 +1,20 @@
from django.core.handlers.wsgi import WSGIRequest from django.core.handlers.wsgi import WSGIRequest
from django.shortcuts import render from django.shortcuts import render
from markdownx.utils import markdownify
from schickmacher.renderer.forms import RendererForm from schickmacher.renderer.forms import RendererForm
def form_view(request: WSGIRequest): def form_view(request: WSGIRequest):
ctx = {"form": RendererForm()} if request.method == 'POST':
form = RendererForm(request.POST)
if form.is_valid():
return render(request, 'renderer/rendered.html', {
'date': form.cleaned_data['date'],
'content': markdownify(form.cleaned_data['render_content']),
'title': form.cleaned_data['title']
})
else:
form = RendererForm()
ctx = {"form": form}
return render(request, 'renderer/render_form.html', ctx) return render(request, 'renderer/render_form.html', ctx)

View file

@ -0,0 +1,3 @@
h1 {
color: red;
}

View file

@ -49,9 +49,6 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{% url 'renderer:form' %}">Renderer</a> <a class="nav-link" href="{% url 'renderer:form' %}">Renderer</a>
</li> </li>
<li class="nav-item">
<a class="nav-link" href="{% url 'about' %}">About</a>
</li>
{% if request.user.is_authenticated %} {% if request.user.is_authenticated %}
<li class="nav-item"> <li class="nav-item">
{# URL provided by django-allauth/account/urls.py #} {# URL provided by django-allauth/account/urls.py #}

View file

@ -1 +0,0 @@
{% extends "base.html" %}

View file

@ -1 +0,0 @@
{% extends "base.html" %}

View file

@ -1,4 +1,5 @@
{% extends "base.html" %} {% extends "base.html" %}
{% load staticfiles %}
{% load i18n %} {% load i18n %}
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
@ -16,4 +17,5 @@
</div> </div>
</div> </div>
</form> </form>
{{ form.media }}
{% endblock %} {% endblock %}

View file

@ -0,0 +1,14 @@
{% load static compress tz %}<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<link href="{% static 'css/render.css' %}" rel="stylesheet">
</head>
<body>
{{ date | date:'d.m.Y' }}
{{ content | safe }}
</body>
</html>