<s> import os <EOL> import os . path as op <EOL> from flask import Flask , url_for <EOL> from flask . ext . sqlalchemy import SQLAlchemy <EOL> from sqlalchemy . event import listens_for <EOL> from jinja2 import Markup <EOL> from flask . ext . admin import Admin , form <EOL> from flask . ext . admin . form import rules <EOL> from flask . ext . admin . contrib import sqla <EOL> app = Flask ( __name__ , static_folder = '<STR_LIT>' ) <EOL> app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> app . config [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> app . config [ '<STR_LIT>' ] = '<STR_LIT>' + app . config [ '<STR_LIT>' ] <EOL> app . config [ '<STR_LIT>' ] = True <EOL> db = SQLAlchemy ( app ) <EOL> file_path = op . join ( op . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> try : <EOL> os . mkdir ( file_path ) <EOL> except OSError : <EOL> pass <EOL> class File ( db . Model ) : <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> name = db . Column ( db . Unicode ( <NUM_LIT:64> ) ) <EOL> path = db . Column ( db . Unicode ( <NUM_LIT> ) ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> class Image ( db . Model ) : <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> name = db . Column ( db . Unicode ( <NUM_LIT:64> ) ) <EOL> path = db . Column ( db . Unicode ( <NUM_LIT> ) ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> class User ( db . Model ) : <EOL> id = db . Column ( db . Integer , primary_key = True ) <EOL> first_name = db . Column ( db . Unicode ( <NUM_LIT:64> ) ) <EOL> last_name = db . Column ( db . Unicode ( <NUM_LIT:64> ) ) <EOL> email = db . Column ( db . Unicode ( <NUM_LIT> ) ) <EOL> phone = db . Column ( db . Unicode ( <NUM_LIT:32> ) ) <EOL> city = db . Column ( db . Unicode ( <NUM_LIT> ) ) <EOL> country = db . Column ( db . Unicode ( <NUM_LIT> ) ) <EOL> notes = db . Column ( db . UnicodeText ) <EOL> @ listens_for ( File , '<STR_LIT>' ) <EOL> def del_file ( mapper , connection , target ) : <EOL> if target . path : <EOL> try : <EOL> os . remove ( op . join ( file_path , target . path ) ) <EOL> except OSError : <EOL> pass <EOL> @ listens_for ( Image , '<STR_LIT>' ) <EOL> def del_image ( mapper , connection , target ) : <EOL> if target . path : <EOL> try : <EOL> os . remove ( op . join ( file_path , target . path ) ) <EOL> except OSError : <EOL> pass <EOL> try : <EOL> os . remove ( op . join ( file_path , <EOL> form . thumbgen_filename ( target . path ) ) ) <EOL> except OSError : <EOL> pass <EOL> class FileView ( sqla . ModelView ) : <EOL> form_overrides = { <EOL> '<STR_LIT:path>' : form . FileUploadField <EOL> } <EOL> form_args = { <EOL> '<STR_LIT:path>' : { <EOL> '<STR_LIT:label>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : file_path <EOL> } <EOL> } <EOL> class ImageView ( sqla . ModelView ) : <EOL> def _list_thumbnail ( view , context , model , name ) : <EOL> if not model . path : <EOL> return '<STR_LIT>' <EOL> return Markup ( '<STR_LIT>' % url_for ( '<STR_LIT>' , <EOL> filename = form . thumbgen_filename ( model . path ) ) ) <EOL> column_formatters = { <EOL> '<STR_LIT:path>' : _list_thumbnail <EOL> } <EOL> form_extra_fields = { <EOL> '<STR_LIT:path>' : form . ImageUploadField ( '<STR_LIT>' , <EOL> base_path = file_path , <EOL> thumbnail_size = ( <NUM_LIT:100> , <NUM_LIT:100> , True ) ) <EOL> } <EOL> class UserView ( sqla . ModelView ) : <EOL> """<STR_LIT>""" <EOL> form_create_rules = [ <EOL> rules . FieldSet ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:email>' , '<STR_LIT>' ) , '<STR_LIT>' ) , <EOL> rules . Header ( '<STR_LIT>' ) , <EOL> rules . Field ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> rules . Container ( '<STR_LIT>' , rules . Field ( '<STR_LIT>' ) ) <EOL> ] <EOL> form_edit_rules = form_create_rules <EOL> create_template = '<STR_LIT>' <EOL> edit_template = '<STR_LIT>' <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def index ( ) : <EOL> return '<STR_LIT>' <EOL> admin = Admin ( app , '<STR_LIT>' ) <EOL> admin . add_view ( FileView ( File , db . session ) ) <EOL> admin . add_view ( ImageView ( Image , db . session ) ) <EOL> admin . add_view ( UserView ( User , db . session , name = '<STR_LIT>' ) ) <EOL> def build_sample_db ( ) : <EOL> """<STR_LIT>""" <EOL> import random <EOL> import string <EOL> db . drop_all ( ) <EOL> db . create_all ( ) <EOL> first_names = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> last_names = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> ] <EOL> locations = [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ] <EOL> for i in range ( len ( first_names ) ) : <EOL> user = User ( ) <EOL> user . first_name = first_names [ i ] <EOL> user . last_name = last_names [ i ] <EOL> user . email = user . first_name . lower ( ) + "<STR_LIT>" <EOL> tmp = '<STR_LIT>' . join ( random . choice ( string . digits ) for i in range ( <NUM_LIT:10> ) ) <EOL> user . phone = "<STR_LIT:(>" + tmp [ <NUM_LIT:0> : <NUM_LIT:3> ] + "<STR_LIT>" + tmp [ <NUM_LIT:3> : <NUM_LIT:6> ] + "<STR_LIT:U+0020>" + tmp [ <NUM_LIT:6> : : ] <EOL> user . city = locations [ i ] [ <NUM_LIT:0> ] <EOL> user . country = locations [ i ] [ <NUM_LIT:1> ] <EOL> db . session . add ( user ) <EOL> images = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> for name in images : <EOL> image = Image ( ) <EOL> image . name = name <EOL> image . path = name . lower ( ) + "<STR_LIT>" <EOL> db . session . add ( image ) <EOL> for i in [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] : <EOL> file = File ( ) <EOL> file . name = "<STR_LIT>" + str ( i ) <EOL> file . path = "<STR_LIT>" + str ( i ) + "<STR_LIT>" <EOL> db . session . add ( file ) <EOL> db . session . commit ( ) <EOL> return <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app_dir = op . realpath ( os . path . dirname ( __file__ ) ) <EOL> database_path = op . join ( app_dir , app . config [ '<STR_LIT>' ] ) <EOL> if not os . path . exists ( database_path ) : <EOL> build_sample_db ( ) <EOL> app . run ( debug = True ) </s>
