post creation now saves markdown files

This commit is contained in:
CDaut 2022-06-18 10:13:41 +02:00 committed by CDaut
parent 01ed26a939
commit ead8a3a2ef
3 changed files with 16 additions and 6 deletions

5
.gitignore vendored
View file

@ -281,4 +281,7 @@ fabric.properties
# https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij # https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
.idea/**/azureSettings.xmlnn .idea/**/azureSettings.xmlnn
envvars.env envvars.env
# Test blog posts
/markdownblog/mdfiles/*

View file

@ -1,3 +1,4 @@
POSTGRES_PASSWORD=Pwp9^SXoD9X%R$A$fCX5 POSTGRES_PASSWORD=Pwp9^SXoD9X%R$A$fCX5
POSTGRES_USER=mainuser POSTGRES_USER=mainuser
POSTGRES_DB=django_main POSTGRES_DB=django_main
MD_FILE_PATH=/markdownblog/mdfiles

View file

@ -1,3 +1,4 @@
import os
import random import random
from django.contrib.auth.decorators import login_required 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'] != '': if 'tags' in request.POST and request.POST['tags'] != '':
tags_str = request.POST['tags'].split(" ") tags_str = request.POST['tags'].split(" ")
for tag in tags_str: 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'] topicid = request.POST['topic']
topics = None if topicstr == "No topic" else Topic.objects.filter(name=topicstr)[0] 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) 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()} context = {'alltopics': Topic.objects.all().order_by('name').values()}
return render(request, 'blog/addpost.html', context) return render(request, 'blog/addpost.html', context)