From ead8a3a2ef8a9a566473ac9dc5af74ee0eddd5a4 Mon Sep 17 00:00:00 2001 From: CDaut Date: Sat, 18 Jun 2022 10:13:41 +0200 Subject: [PATCH] post creation now saves markdown files --- .gitignore | 5 ++++- envvars.env | 3 ++- markdownblog/blog/views.py | 14 ++++++++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 9541438..2b55af4 100644 --- a/.gitignore +++ b/.gitignore @@ -281,4 +281,7 @@ fabric.properties # https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij .idea/**/azureSettings.xmlnn -envvars.env \ No newline at end of file +envvars.env + +# Test blog posts +/markdownblog/mdfiles/* \ No newline at end of file diff --git a/envvars.env b/envvars.env index 657c33c..9f1836a 100644 --- a/envvars.env +++ b/envvars.env @@ -1,3 +1,4 @@ POSTGRES_PASSWORD=Pwp9^SXoD9X%R$A$fCX5 POSTGRES_USER=mainuser -POSTGRES_DB=django_main \ No newline at end of file +POSTGRES_DB=django_main +MD_FILE_PATH=/markdownblog/mdfiles \ No newline at end of file diff --git a/markdownblog/blog/views.py b/markdownblog/blog/views.py index 1e759d5..9e8ba72 100644 --- a/markdownblog/blog/views.py +++ b/markdownblog/blog/views.py @@ -1,3 +1,4 @@ +import os import random from django.contrib.auth.decorators import login_required @@ -40,14 +41,19 @@ def addpost(request) -> HttpResponse: if 'tags' in request.POST and request.POST['tags'] != '': tags_str = request.POST['tags'].split(" ") for tag in tags_str: - tags.append(Tag.objects.filter(name=tag)[0]) + tags.append(Tag.objects.get_or_create(name=tag)) - topicstr = request.POST['topic'] - topics = None if topicstr == "No topic" else Topic.objects.filter(name=topicstr)[0] + topicid = request.POST['topic'] + topic = None if topicid == "No topic" else Topic.objects.get(pk=topicid) - new_post = Blogpost.objects.create(title=title, topics=topics) + new_post = Blogpost.objects.create(title=title, topic=topic) new_post.tags.set(tags) + filepath = os.path.join(os.environ.get("MD_FILE_PATH"), title + ".md") + with open(filepath, "w+") as mdfile: + mdfile.write(markdown) + mdfile.close() + context = {'alltopics': Topic.objects.all().order_by('name').values()} return render(request, 'blog/addpost.html', context)