<s> from flask import Flask , url_for , redirect , render_template , request <EOL> from flask_mongoengine import MongoEngine <EOL> from wtforms import form , fields , validators <EOL> import flask_admin as admin <EOL> import flask_login as login <EOL> from flask_admin . contrib . mongoengine import ModelView <EOL> from flask_admin import helpers <EOL> app = Flask ( __name__ ) <EOL> app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> app . config [ '<STR_LIT>' ] = { '<STR_LIT>' : '<STR_LIT:test>' } <EOL> db = MongoEngine ( ) <EOL> db . init_app ( app ) <EOL> class User ( db . Document ) : <EOL> login = db . StringField ( max_length = <NUM_LIT> , unique = True ) <EOL> email = db . StringField ( max_length = <NUM_LIT> ) <EOL> password = db . StringField ( max_length = <NUM_LIT:64> ) <EOL> def is_authenticated ( self ) : <EOL> return True <EOL> def is_active ( self ) : <EOL> return True <EOL> def is_anonymous ( self ) : <EOL> return False <EOL> def get_id ( self ) : <EOL> return str ( self . id ) <EOL> def __unicode__ ( self ) : <EOL> return self . login <EOL> class LoginForm ( form . Form ) : <EOL> login = fields . TextField ( validators = [ validators . required ( ) ] ) <EOL> password = fields . PasswordField ( validators = [ validators . required ( ) ] ) <EOL> def validate_login ( self , field ) : <EOL> user = self . get_user ( ) <EOL> if user is None : <EOL> raise validators . ValidationError ( '<STR_LIT>' ) <EOL> if user . password != self . password . data : <EOL> raise validators . ValidationError ( '<STR_LIT>' ) <EOL> def get_user ( self ) : <EOL> return User . objects ( login = self . login . data ) . first ( ) <EOL> class RegistrationForm ( form . Form ) : <EOL> login = fields . TextField ( validators = [ validators . required ( ) ] ) <EOL> email = fields . TextField ( ) <EOL> password = fields . PasswordField ( validators = [ validators . required ( ) ] ) <EOL> def validate_login ( self , field ) : <EOL> if User . objects ( login = self . login . data ) : <EOL> raise validators . ValidationError ( '<STR_LIT>' ) <EOL> def init_login ( ) : <EOL> login_manager = login . LoginManager ( ) <EOL> login_manager . setup_app ( app ) <EOL> @ login_manager . user_loader <EOL> def load_user ( user_id ) : <EOL> return User . objects ( id = user_id ) . first ( ) <EOL> class MyModelView ( ModelView ) : <EOL> def is_accessible ( self ) : <EOL> return login . current_user . is_authenticated <EOL> class MyAdminIndexView ( admin . AdminIndexView ) : <EOL> def is_accessible ( self ) : <EOL> return login . current_user . is_authenticated <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> return render_template ( '<STR_LIT>' , user = login . current_user ) <EOL> @ app . route ( '<STR_LIT>' , methods = ( '<STR_LIT:GET>' , '<STR_LIT:POST>' ) ) <EOL> def login_view ( ) : <EOL> form = LoginForm ( request . form ) <EOL> if request . method == '<STR_LIT:POST>' and form . validate ( ) : <EOL> user = form . get_user ( ) <EOL> login . login_user ( user ) <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ app . route ( '<STR_LIT>' , methods = ( '<STR_LIT:GET>' , '<STR_LIT:POST>' ) ) <EOL> def register_view ( ) : <EOL> form = RegistrationForm ( request . form ) <EOL> if request . method == '<STR_LIT:POST>' and form . validate ( ) : <EOL> user = User ( ) <EOL> form . populate_obj ( user ) <EOL> user . save ( ) <EOL> login . login_user ( user ) <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def logout_view ( ) : <EOL> login . logout_user ( ) <EOL> return redirect ( url_for ( '<STR_LIT:index>' ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> init_login ( ) <EOL> admin = admin . Admin ( app , '<STR_LIT>' , index_view = MyAdminIndexView ( ) ) <EOL> admin . add_view ( MyModelView ( User ) ) <EOL> app . run ( debug = True ) </s>
