Skip to content

Instantly share code, notes, and snippets.

@justinabrahms
Forked from ericflo/admin.py
Created July 5, 2010 22:25
Show Gist options
  • Save justinabrahms/464762 to your computer and use it in GitHub Desktop.
Save justinabrahms/464762 to your computer and use it in GitHub Desktop.
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)
<!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 &copy; 2010, Made with Lasers, LLC.</p>
</div>
{% endblock %}
{% block extra_body %}{% endblock %}
</body>
</html>
{% 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>&nbsp;</th>
<td><input type="submit" value="Edit {{ model_slug.title() }}" /></td>
</tr>
</tbody>
</table>
</form>
{% endblock %}
{% 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 %}
{% 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 %}
{% 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>&nbsp;</th>
<td><input type="submit" value="Create {{ model_slug.title() }}" /></td>
</tr>
</tbody>
</table>
</form>
{% endblock %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment