Created
July 5, 2010 08:09
-
-
Save ericflo/464128 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Module, request, redirect, url_for, render_template, abort | |
from formalchemy import FieldSet | |
from example import db | |
from example import Service, User, Forum, Category, Topic, Post | |
mod = Module(__name__) | |
models = { | |
'service': Service, | |
'user': User, | |
'forum': Forum, | |
'category': Category, | |
'topic': Topic, | |
'post': Post, | |
} | |
@mod.route('/admin') | |
def index(): | |
return render_template('admin/index.html', models=models) | |
@mod.route('/admin/<model_slug>') | |
def object_list(model_slug): | |
if model_slug not in models: | |
abort(404) | |
model = models[model_slug] | |
field_names = model.__table__.columns.keys() | |
primary_key = model.__table__.primary_key.columns.keys()[0] | |
objects = model.query.all() | |
context = { | |
'model_slug': model_slug, | |
'field_names': field_names, | |
'primary_key': primary_key, | |
'objects': objects, | |
} | |
return render_template('admin/list.html', **context) | |
@mod.route('/admin/<model_slug>/edit/<model_key>', methods=['GET', 'POST']) | |
def object_edit(model_slug, model_key): | |
if model_slug not in models: | |
abort(404) | |
model = models[model_slug] | |
field_names = model.__table__.columns.keys() | |
if model_key: | |
obj = model.query.get(model_key) | |
else: | |
obj = model() | |
fields = FieldSet(obj) | |
if request.method == 'POST': | |
data_dict = dict(request.form.items()) | |
fields = FieldSet(obj, data=data_dict) | |
if fields.validate(): | |
fields.sync() | |
db.session.add(obj) | |
db.session.commit() | |
next_url = url_for('admin.object_list', model_slug=model_slug) | |
return redirect(next_url) | |
context = { | |
'model_slug': model_slug, | |
'model': model, | |
'field_names': field_names, | |
'fields': fields, | |
} | |
template_name = 'admin/edit.html' if model_key else 'admin/new.html' | |
return render_template(template_name, **context) | |
@mod.route('/admin/<model_slug>/new', methods=['GET', 'POST']) | |
def object_new(model_slug): | |
return object_edit(model_slug, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<title>{% block title %}Prototype Flask Admin{% endblock %}</title> | |
<link rel="shortcut icon" type="image/ico" href="{{ url_for('.static', filename='images/favicon.ico') }}" /> | |
<link href="{{ url_for('.static', filename='css/admin.css') }}" rel="stylesheet" type="text/css" media="screen" /> | |
{% block extra_head %}{% endblock %} | |
</head> | |
<body id="{% block body_id %}{% endblock %}" class="{% block body_class %}{% endblock %}"> | |
<a href="{{ url_for('admin.index') }}">Admin Home</a> | |
{% block content %}{% endblock %} | |
{% block footer %} | |
<div id="footer"> | |
<p>Copyright © 2010, Made with Lasers, LLC.</p> | |
</div> | |
{% endblock %} | |
{% block extra_body %}{% endblock %} | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "admin/base.html" %} | |
{% block content %} | |
<h2>Edit {{ model_slug.title() }} Object</h2> | |
<form method="POST"> | |
<table> | |
<tbody> | |
{% for field_name in field_names %} | |
<tr> | |
<th>{{ field_name.title() }}</th> | |
<td>{{ fields[field_name].render()|safe }} | |
{% if fields[field_name].errors %} | |
<ul> | |
{% for error_msg in fields[field_name].errors %} | |
<li>{{ error_msg }}</li> | |
{% endfor %} | |
</ul> | |
{% endif %} | |
</td> | |
</tr> | |
{% endfor %} | |
<tr> | |
<th> </th> | |
<td><input type="submit" value="Edit {{ model_slug.title() }}" /></td> | |
</tr> | |
</tbody> | |
</table> | |
</form> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "admin/base.html" %} | |
{% block content %} | |
<h2>Administrative Tasks</h2> | |
<ul> | |
{% for model_slug in models %} | |
<li><a href="{{ url_for('admin.object_list', model_slug=model_slug) }}">{{ model_slug.title() }}</a></li> | |
{% endfor %} | |
</ul> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "admin/base.html" %} | |
{% block content %} | |
<h2>List of {{ model_slug.title() }} Objects</h2> | |
<a href="{{ url_for('admin.object_new', model_slug=model_slug) }}">Create New {{ model_slug.title() }}</a> | |
<table> | |
<thead> | |
<tr> | |
{% for field_name in field_names %} | |
<th>{{ field_name.title() }}</th> | |
{% endfor %} | |
</tr> | |
</thead> | |
<tbody> | |
{% for object in objects %} | |
<tr> | |
{% for field_name in field_names %} | |
{% if loop.first %} | |
<td><a href="{{ url_for('admin.object_edit', model_slug=model_slug, model_key=object[primary_key]) }}">{{ object[field_name] }}</a></td> | |
{% else %} | |
<td>{{ object[field_name] }}</td> | |
{% endif %} | |
{% endfor %} | |
</tr> | |
{% endfor %} | |
</tbody> | |
</table> | |
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "admin/base.html" %} | |
{% block content %} | |
<h2>Create a New {{ model_slug.title() }} Object</h2> | |
<form method="POST"> | |
<table> | |
<tbody> | |
{% for field_name in field_names %} | |
<tr> | |
<th>{{ field_name.title() }}</th> | |
<td>{{ fields[field_name].render()|safe }} | |
{% if fields[field_name].errors %} | |
<ul> | |
{% for error_msg in fields[field_name].errors %} | |
<li>{{ error_msg }}</li> | |
{% endfor %} | |
</ul> | |
{% endif %} | |
</td> | |
</tr> | |
{% endfor %} | |
<tr> | |
<th> </th> | |
<td><input type="submit" value="Create {{ model_slug.title() }}" /></td> | |
</tr> | |
</tbody> | |
</table> | |
</form> | |
{% endblock %} |
Nitpick: The favicon image type should be "image/x-icon".
Nitpick2: You also forgot the CSS file. ;-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also. Have to do a little more nitpicking:
This is valid. method=POST is as far as I know not allowed and action is required, even if it's just empty :D