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
|
from django.urls import path
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', index, name="index"),
|
path('', index, name="index"),
|
||||||
path('manage/addpost', addpost, name='addpost'),
|
path('manage/addpost', addpost, name='addpost'),
|
||||||
path('manage/order', order, name='order'),
|
path('manage/order', order, name='order'),
|
||||||
|
path('manage/edit/<int:id>/', edit, name='editpost'),
|
||||||
path('mock/<str:objtype>/<int:n>/', createmocks, name='mock'),
|
path('mock/<str:objtype>/<int:n>/', createmocks, name='mock'),
|
||||||
path('read/<str:title>', viewblog, name='readpost'),
|
path('read/<str:title>', viewblog, name='readpost'),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,45 @@ def index(request) -> HttpResponse:
|
||||||
return render(request, 'blog/index.html', context)
|
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
|
@login_required
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
def order(request) -> HttpResponse:
|
def order(request) -> HttpResponse:
|
||||||
|
|
@ -78,13 +117,17 @@ 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.get_or_create(name=tag))
|
tagobj = Tag.objects.get_or_create(name=tag)[0]
|
||||||
|
tagobj.save()
|
||||||
|
tags.append(tagobj.id)
|
||||||
|
|
||||||
topicid = request.POST['topic']
|
topicid = request.POST['topic']
|
||||||
topic = None if topicid == "No topic" else Topic.objects.get(pk=topicid)
|
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:
|
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)
|
new_post.tags.set(tags)
|
||||||
except IntegrityError:
|
except IntegrityError:
|
||||||
context = {'alltopics': Topic.objects.all().order_by('name').values(),
|
context = {'alltopics': Topic.objects.all().order_by('name').values(),
|
||||||
|
|
@ -92,7 +135,6 @@ def addpost(request) -> HttpResponse:
|
||||||
"markdown": markdown}
|
"markdown": markdown}
|
||||||
return render(request, 'blog/addpost.html', context)
|
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:
|
with open(filepath, "w+") as mdfile:
|
||||||
mdfile.write(markdown)
|
mdfile.write(markdown)
|
||||||
mdfile.close()
|
mdfile.close()
|
||||||
|
|
|
||||||
|
|
@ -69,7 +69,7 @@
|
||||||
spellChecker: false,
|
spellChecker: false,
|
||||||
autosave: {
|
autosave: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
uniqueId: "mainBlogEditor"
|
uniqueId: "add_post"
|
||||||
},
|
},
|
||||||
renderingConfig: {
|
renderingConfig: {
|
||||||
codeSyntaxHighlighting: true,
|
codeSyntaxHighlighting: true,
|
||||||
|
|
|
||||||
148
markdownblog/markdownblog/templates/blog/edit.html
Normal file
148
markdownblog/markdownblog/templates/blog/edit.html
Normal file
|
|
@ -0,0 +1,148 @@
|
||||||
|
{% extends 'base/base.html' %}
|
||||||
|
{% load static %}
|
||||||
|
{% block title %}
|
||||||
|
Edit Post "{{ post.title }}"
|
||||||
|
{% endblock %}
|
||||||
|
{% block includehere %}
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
|
||||||
|
<link rel="stylesheet" href="{% static 'addpost.css' %}">
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/highlight.js/latest/styles/github.min.css">
|
||||||
|
{% endblock %}
|
||||||
|
{% block content %}
|
||||||
|
<div class="col s12">
|
||||||
|
<form action="{% url 'editpost' id=post.id %}" method="post" enctype="multipart/form-data">
|
||||||
|
{% csrf_token %}
|
||||||
|
<input name="title" type="text" class="posttitle" id="input_title" value="{{ post.title }}">
|
||||||
|
<textarea name="markdown" id="mde_edit_{{ post.id }}">{{ markdown }}</textarea>
|
||||||
|
<div class="row">
|
||||||
|
<div class="col s6">
|
||||||
|
<label>
|
||||||
|
Tags
|
||||||
|
<input type="text" id="input_tags" name="tags"
|
||||||
|
value="{% if post.tags == "Blog.Tag.None" %} {% else %}{{ blog.tags }}{% endif %}">
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<div class="input-field col s6">
|
||||||
|
<select name="topic" id="topic_select">
|
||||||
|
{% if post.topic.name == None %}
|
||||||
|
<option selected>No topic</option>
|
||||||
|
{% else %}
|
||||||
|
<option selected>{{ post.topic.name }}</option>
|
||||||
|
{% endif %}
|
||||||
|
{% for topic in alltopics %}
|
||||||
|
<option value="{{ topic.id }}">{{ topic.name }}</option>
|
||||||
|
{% endfor %}
|
||||||
|
</select>
|
||||||
|
<label>Select topic</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="waves-effect waves-light btn">
|
||||||
|
<i class="material-icons left">add</i>
|
||||||
|
Confirm changes
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
{% endblock %}
|
||||||
|
{% block scripts %}
|
||||||
|
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
|
||||||
|
<script>
|
||||||
|
function autoCollapseSideNavFS(editor) {
|
||||||
|
SimpleMDE.toggleFullScreen(editor);
|
||||||
|
const sidenav = M.Sidenav.getInstance(document.getElementById("slide-out"));
|
||||||
|
|
||||||
|
if (editor.isFullscreenActive()) {
|
||||||
|
sidenav.close();
|
||||||
|
} else {
|
||||||
|
let toggle = document.getElementById("open-sidenav-toggle")
|
||||||
|
if (window.getComputedStyle(toggle).display === "none") {
|
||||||
|
sidenav.open();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function autoCollapseSideNavSbS(editor) {
|
||||||
|
SimpleMDE.toggleSideBySide(editor);
|
||||||
|
autoCollapseSideNavFS(editor);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const simplemde = new SimpleMDE({
|
||||||
|
autoDownloadFontAwesome: true,
|
||||||
|
autofocus: true,
|
||||||
|
spellChecker: false,
|
||||||
|
autosave: {
|
||||||
|
enabled: false
|
||||||
|
},
|
||||||
|
renderingConfig: {
|
||||||
|
codeSyntaxHighlighting: true,
|
||||||
|
},
|
||||||
|
toolbar: [
|
||||||
|
"heading-3",
|
||||||
|
"heading-smaller",
|
||||||
|
"|",
|
||||||
|
"bold",
|
||||||
|
"italic",
|
||||||
|
"strikethrough",
|
||||||
|
"|",
|
||||||
|
"code",
|
||||||
|
"horizontal-rule",
|
||||||
|
"quote",
|
||||||
|
"unordered-list",
|
||||||
|
"ordered-list",
|
||||||
|
"link",
|
||||||
|
"image",
|
||||||
|
"table",
|
||||||
|
"|",
|
||||||
|
"preview",
|
||||||
|
{
|
||||||
|
name: "side-by-side",
|
||||||
|
className: "fa fa-columns no-disable no-mobile",
|
||||||
|
title: "Side by side",
|
||||||
|
action: autoCollapseSideNavSbS
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "fullscreen",
|
||||||
|
className: "fa fa-arrows-alt no-disable no-mobile",
|
||||||
|
title: "fullscreen",
|
||||||
|
action: autoCollapseSideNavFS
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'render-tex',
|
||||||
|
className: 'fa fa-refresh no-disable no-mobile',
|
||||||
|
title: 'Re render LaTex',
|
||||||
|
shortcut: 'Ctrl+R',
|
||||||
|
action: () => {
|
||||||
|
if (window.MathJax) {
|
||||||
|
window.MathJax.typeset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
$("#mde_edit_{{ post.id }}").data({editor: simplemde});
|
||||||
|
</script>
|
||||||
|
<script>
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function () {
|
||||||
|
var elems = document.querySelectorAll('select');
|
||||||
|
M.FormSelect.init(elems);
|
||||||
|
if ("{{ error }}" !== "") {
|
||||||
|
M.toast({html: '{{ error }}'});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
<script src="https://cdn.jsdelivr.net/highlight.js/latest/highlight.min.js"></script>
|
||||||
|
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
|
||||||
|
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
|
||||||
|
<script>
|
||||||
|
MathJax = {
|
||||||
|
tex: {
|
||||||
|
inlineMath: [['$', '$'], ['\\(', '\\)']]
|
||||||
|
},
|
||||||
|
svg: {
|
||||||
|
fontCache: 'global'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
{% endblock %}
|
||||||
|
|
@ -24,6 +24,11 @@
|
||||||
<span class="material-icons">delete</span>
|
<span class="material-icons">delete</span>
|
||||||
</button>
|
</button>
|
||||||
</a>
|
</a>
|
||||||
|
<a href="{% url 'editpost' id=post.id %}">
|
||||||
|
<button class="btn waves-effect waves-light btn-edit">
|
||||||
|
<span class="material-icons">edit</span>
|
||||||
|
</button>
|
||||||
|
</a>
|
||||||
</li>
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,11 @@ delete
|
||||||
<span class="material-icons">delete</span>
|
<span class="material-icons">delete</span>
|
||||||
</button>
|
</button>
|
||||||
</a>
|
</a>
|
||||||
|
<a href="{% url 'editpost' id=post.id %}">
|
||||||
|
<button class="btn waves-effect waves-light btn-edit">
|
||||||
|
<span class="material-icons">edit</span>
|
||||||
|
</button>
|
||||||
|
</a>
|
||||||
</li>
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue