forked from pallets-eco/flask-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
201 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from flask import Flask | ||
from flask.ext.sqlalchemy import SQLAlchemy | ||
|
||
from flask.ext import admin, wtf | ||
from flask.ext.admin.contrib.sqlamodel import ModelView | ||
|
||
# Create application | ||
app = Flask(__name__) | ||
|
||
# Create dummy secrey key so we can use sessions | ||
app.config['SECRET_KEY'] = '123456790' | ||
|
||
# Create in-memory database | ||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///dummy.sqlite' | ||
app.config['SQLALCHEMY_ECHO'] = True | ||
db = SQLAlchemy(app) | ||
|
||
|
||
# Models | ||
class User(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
name = db.Column(db.Unicode(64)) | ||
email = db.Column(db.Unicode(64)) | ||
|
||
def __unicode__(self): | ||
return self.name | ||
|
||
|
||
class Page(db.Model): | ||
id = db.Column(db.Integer, primary_key=True) | ||
name = db.Column(db.Unicode(64)) | ||
text = db.Column(db.UnicodeText) | ||
|
||
def __unicode__(self): | ||
return self.name | ||
|
||
|
||
# Customized admin interface | ||
class CustomView(ModelView): | ||
list_template = 'list.html' | ||
create_template = 'create.html' | ||
edit_template = 'edit.html' | ||
|
||
|
||
# Flask views | ||
@app.route('/') | ||
def index(): | ||
return '<a href="/admin/">Click me to get to Admin!</a>' | ||
|
||
|
||
if __name__ == '__main__': | ||
# Create admin | ||
admin = admin.Admin(app, base_template='layout.html') | ||
|
||
# Add views | ||
admin.add_view(CustomView(User, db.session)) | ||
admin.add_view(CustomView(Page, db.session)) | ||
|
||
# Create DB | ||
db.create_all() | ||
|
||
# Start app | ||
app.debug = True | ||
app.run('0.0.0.0', 8000) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.btn-title { | ||
float: right; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% extends 'admin/model/create.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{% extends 'admin/model/edit.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{% import 'admin/layout.html' as layout with context -%} | ||
{% extends 'admin/base.html' %} | ||
|
||
{% block head_css %} | ||
{{ super() }} | ||
<link href="{{ url_for('static', filename='layout.css') }}" rel="stylesheet"> | ||
{% endblock %} | ||
|
||
{% block page_body %} | ||
<div class="container"> | ||
<div class="row"> | ||
<div class="span2"> | ||
<ul class="nav nav-pills nav-stacked"> | ||
{{ layout.menu() }} | ||
{{ layout.menu_links() }} | ||
</ul> | ||
</div> | ||
<div class="span10"> | ||
{% block brand %}<h1>{{ admin_view.name|capitalize }}</h1>{% endblock %} | ||
{{ layout.messages() }} | ||
{% block body %}{% endblock %} | ||
</div> | ||
</div> | ||
</div> | ||
{% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
{% extends 'admin/model/list.html' %} | ||
{% import 'admin/model/layout.html' as model_layout with context %} | ||
|
||
{% block brand %} | ||
<h1 style="float: left">{{ admin_view.name|capitalize }} model</h1> | ||
{% if admin_view.can_create %} | ||
<a href="{{ url_for('.create_view', url=return_url) }}" class="btn btn-primary btn-title">{{ _gettext('Create') }}</a> | ||
{% endif %} | ||
|
||
{% if filter_groups %} | ||
{{ model_layout.filter_options() }} | ||
{% endif %} | ||
|
||
{% if actions %} | ||
<div class="btn-group btn-title"> | ||
{{ actionlib.dropdown(actions) }} | ||
</div> | ||
{% endif %} | ||
|
||
{% if search_supported %} | ||
{{ model_layout.search_form() }} | ||
{% endif %} | ||
<div class="clearfix"></div> | ||
<hr> | ||
{% endblock %} | ||
|
||
{% block body %} | ||
{% if filter_groups %} | ||
{{ model_layout.filter_form() }} | ||
{% endif %} | ||
|
||
<table class="table table-striped table-bordered model-list"> | ||
<thead> | ||
<tr> | ||
{% block list_header scoped %} | ||
{% if actions %} | ||
<th class="span1"> | ||
<input type="checkbox" name="rowtoggle" class="action-rowtoggle" /> | ||
</th> | ||
{% endif %} | ||
<th class="span1"> </th> | ||
{% set column = 0 %} | ||
{% for c, name in list_columns %} | ||
<th> | ||
{% if admin_view.is_sortable(c) %} | ||
{% if sort_column == column %} | ||
<a href="{{ sort_url(column, True) }}"> | ||
{{ name }} | ||
{% if sort_desc %} | ||
<i class="icon-chevron-up"></i> | ||
{% else %} | ||
<i class="icon-chevron-down"></i> | ||
{% endif %} | ||
</a> | ||
{% else %} | ||
<a href="{{ sort_url(column) }}">{{ name }}</a> | ||
{% endif %} | ||
{% else %} | ||
{{ name }} | ||
{% endif %} | ||
{% if admin_view.column_descriptions.get(c) %} | ||
<a class="icon-question-sign" | ||
title="{{ admin_view.column_descriptions[c] }}" | ||
href="#" data-role="tooltip" | ||
></a> | ||
{% endif %} | ||
</th> | ||
{% set column = column + 1 %} | ||
{% endfor %} | ||
{% endblock %} | ||
</tr> | ||
</thead> | ||
{% for row in data %} | ||
<tr> | ||
{% block list_row scoped %} | ||
{% if actions %} | ||
<td> | ||
<input type="checkbox" name="rowid" class="action-checkbox" value="{{ get_pk_value(row) }}" /> | ||
</td> | ||
{% endif %} | ||
<td> | ||
{% block list_row_actions scoped %} | ||
{%- if admin_view.can_edit -%} | ||
<a class="icon" href="{{ url_for('.edit_view', id=get_pk_value(row), url=return_url) }}"> | ||
<i class="icon-pencil"></i> | ||
</a> | ||
{%- endif -%} | ||
{%- if admin_view.can_delete -%} | ||
<form class="icon" method="POST" action="{{ url_for('.delete_view', id=get_pk_value(row), url=return_url) }}"> | ||
<button onclick="return confirm('{{ _gettext('You sure you want to delete this item?') }}');"> | ||
<i class="icon-remove"></i> | ||
</button> | ||
</form> | ||
{%- endif -%} | ||
{% endblock %} | ||
</td> | ||
{% for c, name in list_columns %} | ||
<td>{{ get_value(row, c) }}</td> | ||
{% endfor %} | ||
{% endblock %} | ||
</tr> | ||
{% endfor %} | ||
</table> | ||
{{ lib.pager(page, num_pages, pager_url) }} | ||
|
||
{{ actionlib.form(actions, url_for('.action_view')) }} | ||
{% 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