diff --git a/config/settings/base.py b/config/settings/base.py index 8dabdb0..c370030 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -69,6 +69,7 @@ THIRD_PARTY_APPS = [ "allauth.account", "allauth.socialaccount", "rest_framework", + "markdownx", ] LOCAL_APPS = [ diff --git a/config/urls.py b/config/urls.py index d4f14d4..4785bcd 100644 --- a/config/urls.py +++ b/config/urls.py @@ -7,15 +7,13 @@ from django.views import defaults as default_views urlpatterns = [ 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' %} path(settings.ADMIN_URL, admin.site.urls), # User management path("users/", include("schickmacher.users.urls", namespace="users")), path("renderer/", include("schickmacher.renderer.urls", namespace="renderer")), path("accounts/", include("allauth.urls")), + path(r'markdownx/', include('markdownx.urls')), # Your stuff: custom urls includes go here ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/requirements/base.txt b/requirements/base.txt index 3bee6ba..b2cbda8 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -18,3 +18,5 @@ django-redis==4.10.0 # https://github.com/niwinz/django-redis # 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 + +django-markdownx==2.0.28 diff --git a/schickmacher/renderer/forms.py b/schickmacher/renderer/forms.py index 2af7cb8..4817a29 100644 --- a/schickmacher/renderer/forms.py +++ b/schickmacher/renderer/forms.py @@ -1,15 +1,16 @@ from django.utils.translation import ugettext_lazy as _ from django import forms from django.utils import timezone +from markdownx.fields import MarkdownxFormField class RendererForm(forms.Form): + title = forms.CharField(label=_('Title'), required=False) date = forms.DateField(label=_('Date'), initial=lambda: timezone.now().strftime('%Y-%m-%d'), widget=forms.TextInput( attrs={'type': 'date'} )) - render_content = forms.CharField( - label=_('Content'), widget=forms.Textarea, max_length=None, - required=False, initial="" + render_content = MarkdownxFormField( + label=_('Content'), ) diff --git a/schickmacher/renderer/tests/test_forms.py b/schickmacher/renderer/tests/test_forms.py index 199b91a..62591fc 100644 --- a/schickmacher/renderer/tests/test_forms.py +++ b/schickmacher/renderer/tests/test_forms.py @@ -10,7 +10,8 @@ class TestRendererForm: def test_form_accepts_content(self): form = RendererForm({ "date": timezone.now(), - "render_content": "Example **content**" + "render_content": "Example **content**", + "title": "Test" }) assert form.is_valid() diff --git a/schickmacher/renderer/tests/test_views.py b/schickmacher/renderer/tests/test_views.py index d19f335..8abf00a 100644 --- a/schickmacher/renderer/tests/test_views.py +++ b/schickmacher/renderer/tests/test_views.py @@ -13,3 +13,14 @@ class TestRendererFormView: assert response.status_code == 200 assert response.context['form']['date'].value() \ == 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 + diff --git a/schickmacher/renderer/views.py b/schickmacher/renderer/views.py index 0c0c38a..caa40dc 100644 --- a/schickmacher/renderer/views.py +++ b/schickmacher/renderer/views.py @@ -1,9 +1,20 @@ from django.core.handlers.wsgi import WSGIRequest from django.shortcuts import render +from markdownx.utils import markdownify from schickmacher.renderer.forms import RendererForm 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) diff --git a/schickmacher/static/css/render.css b/schickmacher/static/css/render.css new file mode 100644 index 0000000..adc68fa --- /dev/null +++ b/schickmacher/static/css/render.css @@ -0,0 +1,3 @@ +h1 { + color: red; +} diff --git a/schickmacher/templates/base.html b/schickmacher/templates/base.html index 4a893e6..08c8cbf 100644 --- a/schickmacher/templates/base.html +++ b/schickmacher/templates/base.html @@ -49,9 +49,6 @@