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

3
.gitignore vendored
View file

@ -282,3 +282,6 @@ fabric.properties
.idea/**/azureSettings.xmlnn
envvars.env
# Test blog posts
/markdownblog/mdfiles/*

View file

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

View file

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