Upload
dokenzy
View
3.406
Download
2
Embed Size (px)
DESCRIPTION
Citation preview
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Flask-Admin vs. DIYDokenzy
2013. 7. 30
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
경고
저는 flask-admin를 안써요.
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
준비
$ pythonbrew use 2.7.3$ pythonbrew venv create fladmin$ pythonbrew venv use fladmin
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
설치
▶ Flask▶ Flask-Admin▶ SQLAlchemy▶ Flask-SQLAlchemy▶ WTForms▶ Flask-WTForms
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
시작하기 전에…
▶ http://flask-admin.readthedocs.org/en/latest/▶ I attempted to make it as flexible as possible.▶ Administrative pieces are built as classes with view
methods.
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
시작
1 from flask import Flask2 from flask.ext.admin import Admin34 app = Flask(__name__)56 admin = Admin(app)7 # admin = Admin(app, name=’Admin page Title’)89 # admin = Admin(name=’Admin page Title’)
10 # admin.init_app(app)1112 app.run()
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
View
▶ BaseView▶ ModelView
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
BaseView
1 from flask.ext.admin import Admin, BaseView, expose23 class System(BaseView):4 @expose('/')5 def index(self):6 return self.render(os.path.join('admin', 'system.html'))7 @expose('/settings/')8 def settings(self):9 return self.render(os.path.join('admin', 'settings.html'))
10 @expose('/contacts/')11 def contacts(self):12 return self.render(os.path.join('admin', 'contacts.html'))1314 admin.add_view(System(name='settings', endpoint='settings', category='Setting'))15 admin.add_view(System(name='contacts', endpoint='contacts', category='Setting'))16 admin.add_view(System(name='system', endpoint='sos', category='Setting'))
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
BaseView
▶ @expose('/')
필수
▶ @expose(url, methods)▶ lowercased 클래스 이름이 기본 endpoint▶ 여러 개의 메소드가 있어도 항상 ‘/’만 호출된다.
http://stackoverflow.com/questions/15699303/setting-up-admin-views-with-flask-admin-module.
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
ModelView
1 from flask.ext.admin.contrib.sqlamodel import ModelView2 admin = Admin(app)3 admin.add_view(ModelView(User, db.session))
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
ModelView
▶ SQLAlchemy, MongoEngine, Peewee, PyMongo▶ Override(properties, methods)
▶ column_filters = None▶ column_searchable_list = ('name', 'email')▶ create_view
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Temlates
상속
▶ {% extends 'admin/master.html' %}
Flask Core▶ head_meta, title, head_css, head, …
Models▶ model_menu_bar, model_list_table, …
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Authentication
1 class MyView(BaseView):2 def is_accessible(self):3 return login.current_user.is_authenticated()
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Examples
https://github.com/mrjoes/flask-admin/blob/master/examples/
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Run오류 발생
Failed to create model. __init__() takes exactly 2 arguments (1 given)
Failed to create model. __init__() takes exactly 3 arguments (1 given)
Failed to create model. __init__() takes exactly 4 arguments (1 given)
Failed to create model. __init__() takes exactly 5 arguments (1 given)
Failed to create model. __init__() takes exactly 6 arguments (1 given)
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Run해결방법
1. 모델 클래스의 __init__() 수정2. flask-admin을 패치
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Flask-Admin 패치Source
1 try:2 model = self.model()
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Flask-Admin 패치Patch
/site-packages/flask_admin/contrib/sqlamodel/view.py1 values = {}2 for k in form.data.iterkeys():3 try:4 value = unicode(form.data[k])5 except:6 value = form.data[k]7 values[k] = value8
9 try:10 model = self.model(**values)
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
그래도 저는 안씁니다.
▶ 커스터마이징이 많아지면 extension을 쓰는 이유가 없어진다.▶ 매니저 등급 구분 기능이 없다.
▶ 관리자 메뉴에서 편집한 내용이 원래 레코드에 반영이 안된다.
어쩌라고…?
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
Do it yourself
▶ ORM을 쓰면 할만하다.▶ DB 모델을 싹 갈아치울 일은 거의 없다.▶ 관리자 UI는 유행이나 이벤트로부터 자유롭다.
..........
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
.....
.....
......
.....
......
.....
.....
.
끝
Demo