<s> import pymongo <EOL> from bson . objectid import ObjectId <EOL> from flask import Flask <EOL> import flask_admin as admin <EOL> from wtforms import form , fields <EOL> from flask_admin . form import Select2Widget <EOL> from flask_admin . contrib . pymongo import ModelView , filters <EOL> from flask_admin . model . fields import InlineFormField , InlineFieldList <EOL> app = Flask ( __name__ ) <EOL> app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> conn = pymongo . Connection ( ) <EOL> db = conn . test <EOL> class InnerForm ( form . Form ) : <EOL> name = fields . TextField ( '<STR_LIT:Name>' ) <EOL> test = fields . TextField ( '<STR_LIT>' ) <EOL> class UserForm ( form . Form ) : <EOL> name = fields . TextField ( '<STR_LIT:Name>' ) <EOL> email = fields . TextField ( '<STR_LIT>' ) <EOL> password = fields . TextField ( '<STR_LIT>' ) <EOL> inner = InlineFormField ( InnerForm ) <EOL> form_list = InlineFieldList ( InlineFormField ( InnerForm ) ) <EOL> class UserView ( ModelView ) : <EOL> column_list = ( '<STR_LIT:name>' , '<STR_LIT:email>' , '<STR_LIT:password>' ) <EOL> column_sortable_list = ( '<STR_LIT:name>' , '<STR_LIT:email>' , '<STR_LIT:password>' ) <EOL> form = UserForm <EOL> class TweetForm ( form . Form ) : <EOL> name = fields . TextField ( '<STR_LIT:Name>' ) <EOL> user_id = fields . SelectField ( '<STR_LIT>' , widget = Select2Widget ( ) ) <EOL> text = fields . TextField ( '<STR_LIT>' ) <EOL> testie = fields . BooleanField ( '<STR_LIT>' ) <EOL> class TweetView ( ModelView ) : <EOL> column_list = ( '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT:text>' ) <EOL> column_sortable_list = ( '<STR_LIT:name>' , '<STR_LIT:text>' ) <EOL> column_filters = ( filters . FilterEqual ( '<STR_LIT:name>' , '<STR_LIT:Name>' ) , <EOL> filters . FilterNotEqual ( '<STR_LIT:name>' , '<STR_LIT:Name>' ) , <EOL> filters . FilterLike ( '<STR_LIT:name>' , '<STR_LIT:Name>' ) , <EOL> filters . FilterNotLike ( '<STR_LIT:name>' , '<STR_LIT:Name>' ) , <EOL> filters . BooleanEqualFilter ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> column_searchable_list = ( '<STR_LIT:name>' , '<STR_LIT:text>' ) <EOL> form = TweetForm <EOL> def get_list ( self , * args , ** kwargs ) : <EOL> count , data = super ( TweetView , self ) . get_list ( * args , ** kwargs ) <EOL> query = { '<STR_LIT>' : { '<STR_LIT>' : [ x [ '<STR_LIT>' ] for x in data ] } } <EOL> users = db . user . find ( query , fields = ( '<STR_LIT:name>' , ) ) <EOL> users_map = dict ( ( x [ '<STR_LIT>' ] , x [ '<STR_LIT:name>' ] ) for x in users ) <EOL> for item in data : <EOL> item [ '<STR_LIT>' ] = users_map . get ( item [ '<STR_LIT>' ] ) <EOL> return count , data <EOL> def _feed_user_choices ( self , form ) : <EOL> users = db . user . find ( fields = ( '<STR_LIT:name>' , ) ) <EOL> form . user_id . choices = [ ( str ( x [ '<STR_LIT>' ] ) , x [ '<STR_LIT:name>' ] ) for x in users ] <EOL> return form <EOL> def create_form ( self ) : <EOL> form = super ( TweetView , self ) . create_form ( ) <EOL> return self . _feed_user_choices ( form ) <EOL> def edit_form ( self , obj ) : <EOL> form = super ( TweetView , self ) . edit_form ( obj ) <EOL> return self . _feed_user_choices ( form ) <EOL> def on_model_change ( self , form , model ) : <EOL> user_id = model . get ( '<STR_LIT>' ) <EOL> model [ '<STR_LIT>' ] = ObjectId ( user_id ) <EOL> return model <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> return '<STR_LIT>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> admin = admin . Admin ( app , name = '<STR_LIT>' ) <EOL> admin . add_view ( UserView ( db . user , '<STR_LIT>' ) ) <EOL> admin . add_view ( TweetView ( db . tweet , '<STR_LIT>' ) ) <EOL> app . run ( debug = True ) </s>
