Skip to content

Commit 7dd0a39

Browse files
committed
Added example working with rochacbruno/tinyMongo
1 parent 23dddb0 commit 7dd0a39

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

examples/tinymongo/README.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
PyMongo model backend integration example.
2+
3+
To run this example:
4+
5+
1. Clone the repository::
6+
7+
git clone https://github.com/flask-admin/flask-admin.git
8+
cd flask-admin
9+
10+
2. Create and activate a virtual environment::
11+
12+
virtualenv env
13+
source env/bin/activate
14+
15+
3. Install requirements::
16+
17+
pip install -r 'examples/pymongo/requirements.txt'
18+
19+
4. Run the application::
20+
21+
python examples/pymongo/app.py
22+

examples/tinymongo/app.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
from tinymongo import TinyMongoClient
2+
from bson.objectid import ObjectId
3+
4+
from flask import Flask
5+
import flask_admin as admin
6+
7+
from wtforms import form, fields
8+
9+
from flask_admin.form import Select2Widget
10+
from flask_admin.contrib.pymongo import ModelView, filters
11+
from flask_admin.model.fields import InlineFormField, InlineFieldList
12+
13+
# Create application
14+
app = Flask(__name__)
15+
16+
# Create dummy secrey key so we can use sessions
17+
app.config['SECRET_KEY'] = '123456790'
18+
19+
# Create models
20+
conn = TinyMongoClient('/tmp/flask_admin_test')
21+
db = conn.test
22+
23+
24+
# User admin
25+
class InnerForm(form.Form):
26+
name = fields.StringField('Name')
27+
test = fields.StringField('Test')
28+
29+
30+
class UserForm(form.Form):
31+
foo = fields.StringField('foo')
32+
name = fields.StringField('Name')
33+
email = fields.StringField('Email')
34+
password = fields.StringField('Password')
35+
36+
# Inner form
37+
inner = InlineFormField(InnerForm)
38+
39+
# Form list
40+
form_list = InlineFieldList(InlineFormField(InnerForm))
41+
42+
43+
class UserView(ModelView):
44+
column_list = ('name', 'email', 'password', 'foo')
45+
column_sortable_list = ('name', 'email', 'password')
46+
47+
form = UserForm
48+
49+
50+
# Tweet view
51+
class TweetForm(form.Form):
52+
name = fields.StringField('Name')
53+
user_id = fields.SelectField('User', widget=Select2Widget())
54+
text = fields.StringField('Text')
55+
56+
testie = fields.BooleanField('Test')
57+
58+
59+
class TweetView(ModelView):
60+
column_list = ('name', 'user_name', 'text')
61+
column_sortable_list = ('name', 'text')
62+
63+
column_filters = (filters.FilterEqual('name', 'Name'),
64+
filters.FilterNotEqual('name', 'Name'),
65+
filters.FilterLike('name', 'Name'),
66+
filters.FilterNotLike('name', 'Name'),
67+
filters.BooleanEqualFilter('testie', 'Testie'))
68+
69+
column_searchable_list = ('name', 'text')
70+
71+
form = TweetForm
72+
73+
def get_list(self, *args, **kwargs):
74+
count, data = super(TweetView, self).get_list(*args, **kwargs)
75+
76+
# Grab user names
77+
query = {'_id': {'$in': [x['user_id'] for x in data]}}
78+
users = db.user.find(query, fields=('name',))
79+
80+
# Contribute user names to the models
81+
users_map = dict((x['_id'], x['name']) for x in users)
82+
83+
for item in data:
84+
item['user_name'] = users_map.get(item['user_id'])
85+
86+
return count, data
87+
88+
# Contribute list of user choices to the forms
89+
def _feed_user_choices(self, form):
90+
users = db.user.find(fields=('name',))
91+
form.user_id.choices = [(str(x['_id']), x['name']) for x in users]
92+
return form
93+
94+
def create_form(self):
95+
form = super(TweetView, self).create_form()
96+
return self._feed_user_choices(form)
97+
98+
def edit_form(self, obj):
99+
form = super(TweetView, self).edit_form(obj)
100+
return self._feed_user_choices(form)
101+
102+
# Correct user_id reference before saving
103+
def on_model_change(self, form, model):
104+
user_id = model.get('user_id')
105+
model['user_id'] = user_id
106+
107+
return model
108+
109+
110+
# Flask views
111+
@app.route('/')
112+
def index():
113+
return '<a href="/admin/">Click me to get to Admin!</a>'
114+
115+
116+
if __name__ == '__main__':
117+
# Create admin
118+
admin = admin.Admin(app, name='Example: PyMongo')
119+
120+
# Add views
121+
admin.add_view(UserView(db.user, 'User'))
122+
admin.add_view(TweetView(db.tweet, 'Tweets'))
123+
124+
# Start app
125+
app.run(debug=True)

examples/tinymongo/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Flask
2+
Flask-Admin
3+
pymongo==2.4.1

0 commit comments

Comments
 (0)