Skip to content

Instantly share code, notes, and snippets.

@coleifer
Created October 30, 2011 19:41
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coleifer/5449d29f25e0d78bc2e8 to your computer and use it in GitHub Desktop.
Save coleifer/5449d29f25e0d78bc2e8 to your computer and use it in GitHub Desktop.
Wiki
{% extends "base.html" %}
import datetime
import re
from flask import request, redirect, url_for, render_template, Blueprint, flash, Markup
from peewee import *
from wtfpeewee.orm import model_form
from flaskext.rest import RestResource
from flaskext.utils import get_object_or_404, object_list
from api import api
from app import app, db
from auth import auth
wikify_re = re.compile(r'\b(([A-Z]+[a-z]+){2,})\b')
@app.template_filter('wikify')
def wikify(s):
return Markup(wikify_re.sub(r'<a href="/wiki/\1/">\1</a>', s))
class WikiArticle(db.Model):
name = CharField()
content = TextField()
modified_date = DateTimeField()
class Meta:
ordering = (('modified_date', 'desc'),)
def __unicode__(self):
return self.name
def save(self):
self.modified_date = datetime.datetime.now()
return super(WikiArticle, self).save()
wiki = Blueprint('wiki', __name__, template_folder='templates')
@wiki.route('/')
@auth.login_required
def index():
qr = WikiArticle.select()
return object_list('wiki/index.html', qr)
@wiki.route('/<name>/', methods=['GET', 'POST'])
@auth.login_required
def detail(name):
WikiForm = model_form(WikiArticle, only=('name', 'content',))
try:
article = WikiArticle.get(name=name)
except WikiArticle.DoesNotExist:
article = WikiArticle(name=name)
if request.method == 'POST':
form = WikiForm(request.form, obj=article)
if form.validate():
form.populate_obj(article)
article.save()
flash('Your changes have been saved')
return redirect(url_for('wiki.detail', name=article.name))
else:
flash('There were errors with your submission')
else:
form = WikiForm(obj=article)
return render_template('wiki/detail.html', article=article, form=form)
@wiki.route('/<name>/delete/', methods=['GET', 'POST'])
@auth.login_required
def delete(name):
article = get_object_or_404(WikiArticle, name=name)
if request.method == 'POST':
article.delete_instance()
return redirect(url_for('wiki.index'))
return render_template('wiki/delete.html', article=article)
{% extends "wiki/base_wiki.html" %}
{% block title %}Delete {{ article.name }}?{% endblock %}
{% block content_title %}Delete {{ article.name }}?{% endblock %}
{% block content %}
<form method="post" action="">
<p>Are you sure you want to delete this article?</p>
<p><button type="submit">Delete</button> or <a href="{{ url_for('wiki.detail', name=article.name) }}">cancel</a></p>
</form>
{% endblock %}
{% extends "reader/base_reader.html" %}
{% block title %}{{ article.name }}{% endblock %}
{% block content_title %}{{ article.name }}{% endblock %}
{% block content %}
<div class="article">
{% if article.id %}
{{ article.content|wikify|markdown }}
{% else %}
<p>It appears this article does not exist!</p>
{% endif %}
</div>
<div class="edit" style="display: none;">
<form method="post" action="">
{% for field in form %}
<p>{{ field.label }} {{ field }}</p>
{% endfor %}
<p><button type="submit">Save</button> or <a href="{{ url_for('wiki.index') }}">cancel</a></p>
</form>
</div>
{% endblock %}
{% block sidebar %}
<h3>Page</h3>
<ul>
<li><a href="javascript:void(0);" onclick="$('div.edit').toggle(); $('div.article').toggle();">Edit</a></li>
{% if article.id %}
<li><a href="{{ url_for('wiki.delete', name=article.name) }}">Delete</a></li>
{% endif %}
</ul>
{% endblock %}
{% extends "wiki/base_wiki.html" %}
{% block title %}Wiki{% endblock %}
{% block content_title %}Wiki{% endblock %}
{% block content %}
<ul>
{% for article in object_list %}
<li><a href="{{ url_for('wiki.detail', name=article.name) }}">{{ article.name }}</a>
</li>
{% endfor %}
</ul>
{% include "includes/pagination.html" %}
{% endblock %}
<p class="pagination">
{% if page > 1 %}
<a class="previous" href="./?page={{ page - 1 }}">Previous</a>
{% endif %}
{% if pagination.get_pages() > page %}
<a class="next" href="./?page={{ page + 1 }}">Next</a>
{% endif %}
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment