posts can now be edited
This commit is contained in:
parent
6b0cd847c6
commit
b3c0ac3f38
6 changed files with 206 additions and 5 deletions
|
|
@ -1,10 +1,11 @@
|
|||
from blog.views import viewblog, addpost, order, index, createmocks
|
||||
from blog.views import viewblog, addpost, order, index, createmocks, edit
|
||||
from django.urls import path
|
||||
|
||||
urlpatterns = [
|
||||
path('', index, name="index"),
|
||||
path('manage/addpost', addpost, name='addpost'),
|
||||
path('manage/order', order, name='order'),
|
||||
path('manage/edit/<int:id>/', edit, name='editpost'),
|
||||
path('mock/<str:objtype>/<int:n>/', createmocks, name='mock'),
|
||||
path('read/<str:title>', viewblog, name='readpost'),
|
||||
]
|
||||
|
|
|
|||
|
|
@ -43,6 +43,45 @@ def index(request) -> HttpResponse:
|
|||
return render(request, 'blog/index.html', context)
|
||||
|
||||
|
||||
@login_required
|
||||
def edit(request, id) -> HttpResponse:
|
||||
blogpost = Blogpost.objects.get(pk=id)
|
||||
mdfile_content = open(blogpost.mdfile, "r").read()
|
||||
context = {'alltopics': Topic.objects.all().order_by('name').values(), 'markdown': mdfile_content,
|
||||
'roottopics': Topic.objects.all().filter(rootTopic=None),
|
||||
'allposts': Blogpost.objects.all(), 'post': Blogpost.objects.get(pk=id)}
|
||||
|
||||
if request.method == 'POST':
|
||||
title = request.POST['title']
|
||||
markdown = request.POST['markdown']
|
||||
|
||||
tags = []
|
||||
|
||||
if 'tags' in request.POST and request.POST['tags'] != '':
|
||||
tags_str = request.POST['tags'].split(" ")
|
||||
for tag in tags_str:
|
||||
tagobj = Tag.objects.get_or_create(name=tag)[0]
|
||||
tagobj.save()
|
||||
tags.append(tagobj.id)
|
||||
|
||||
topicname = request.POST['topic']
|
||||
topic = None if topicname == "No topic" else Topic.objects.get(name=topicname)
|
||||
|
||||
filepath = os.path.join(os.environ.get("MD_FILE_PATH"), title + ".md")
|
||||
|
||||
blogpost = Blogpost.objects.get(pk=id)
|
||||
blogpost.topic = topic
|
||||
blogpost.title = title
|
||||
blogpost.tags.set(tags)
|
||||
blogpost.save()
|
||||
|
||||
with open(filepath, "w") as mdfile:
|
||||
mdfile.write(markdown)
|
||||
mdfile.close()
|
||||
context['error'] = "Post \"" + title + "\" edited."
|
||||
return render(request, 'blog/edit.html', context)
|
||||
|
||||
|
||||
@login_required
|
||||
@csrf_exempt
|
||||
def order(request) -> HttpResponse:
|
||||
|
|
@ -78,13 +117,17 @@ 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.get_or_create(name=tag))
|
||||
tagobj = Tag.objects.get_or_create(name=tag)[0]
|
||||
tagobj.save()
|
||||
tags.append(tagobj.id)
|
||||
|
||||
topicid = request.POST['topic']
|
||||
topic = None if topicid == "No topic" else Topic.objects.get(pk=topicid)
|
||||
|
||||
filepath = os.path.join(os.environ.get("MD_FILE_PATH"), title + ".md")
|
||||
|
||||
try:
|
||||
new_post = Blogpost.objects.create(title=title, topic=topic)
|
||||
new_post = Blogpost.objects.create(title=title, topic=topic, mdfile=filepath)
|
||||
new_post.tags.set(tags)
|
||||
except IntegrityError:
|
||||
context = {'alltopics': Topic.objects.all().order_by('name').values(),
|
||||
|
|
@ -92,7 +135,6 @@ def addpost(request) -> HttpResponse:
|
|||
"markdown": markdown}
|
||||
return render(request, 'blog/addpost.html', context)
|
||||
|
||||
filepath = os.path.join(os.environ.get("MD_FILE_PATH"), title + ".md")
|
||||
with open(filepath, "w+") as mdfile:
|
||||
mdfile.write(markdown)
|
||||
mdfile.close()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue