Skip to content

Instantly share code, notes, and snippets.

@shane0
Last active March 24, 2017 19:31
Show Gist options
  • Save shane0/ad29fdeb9c6a6194e9707cb7c29c8c7f to your computer and use it in GitHub Desktop.
Save shane0/ad29fdeb9c6a6194e9707cb7c29c8c7f to your computer and use it in GitHub Desktop.
adding comments to flask-flatpages #flask #flask-flatpages #comments

This is a simple comment system for flask-flatpages, appends a datetime stamp and your comment in the static file.

#todo: cleanup, redirect to same template and clean the template up
class WritePageForm(Form):
"""write content to todays journal file"""
path = TextField('path')
title = TextField('title')
content = TextAreaField('content',
validators=[DataRequired(),
Length(min=3, max=1000),
])
{% extends "layout.html" %}
{% block content %}
<br><br>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Tags</h3>
</div>
<div class="panel-body">
{%for t in page.meta.tags%}
<a href="/flatpages/tag/{{t}}">
{{t}}
</a>
{%endfor%}
</div>
</div>
<p>
{{ page.html | safe }}
</p>
<div class="panel panel-default">
<div class="panel-body">
<h4>Comment</h4>
<form id="WritePageForm" class="form" method="POST" action="{{ url_for('flat.writePage') }}" role="form" >
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
<div class="form-group">
{{form.content(placeholder="enter comment", class_="form-control", rows='5')}}
</div>
page info
<div class="form-group">
{{form.title(value=page.title, class_="form-control", readonly=true)}}
</div>
<div class="form-group">
{{form.path(value=page.path, class_="form-control", readonly=true)}}
</div>
<p><input class="btn btn-default btn-submit" type="submit" value="add"></p>
</form>
</div>
</div>
<br>
<hr>
{% endblock content %}
@blueprint.route('/append/', methods=['GET', 'POST'])
@login_required
def writePage():
form = WritePageForm(request.form, csrf_enabled=False)
now = datetime.datetime.now()
ymd = now.strftime("%Y-%m-%d")
if form.validate_on_submit():
page = pages.get(form.path.data)
ptext = form.content.data.encode('utf-8',errors='ignore') # bugfix for decode errors
ptext = ptext.decode('utf-8',errors='ignore')
path = form.path.data
print(ptext,path)
write_page(path,ptext)
FlatPages.reload(pages)
print(type(page.meta['title']))
flash("comment "+form.content.data+" added to "+str(page.meta['title']))
return render_template('flatpages/writepage.html', form=form, ymd=ymd, page=page)
else:
page = 'fail'
flash_errors(form)
return render_template('flatpages/writepage.html', form=form, ymd=ymd, page=page)
{% extends "layout.html" %}
{% block title %}
{{form.title.data}}
{% endblock %}
{% block name %}
{{form.title.data}}
{% endblock %}
{% block content %}
<!-- Start your project here-->
<h4>Comment</h4>
<p><a href="/flatpages/{{page.path}}">{{page.path}}</a></p>
<h4>output</h4>
<pre class="line-numbers language-powershell">
<code class="line-numbers language-markdown">
comment: {{form.content.data}}
title: {{form.title.data}}
path: {{form.path.data}}
</code>
</pre>
<!-- /Start your project here-->
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment