def setUp(self): # Bug workarounds: Flask Admin和Flask Restful扩展中, # 它们会为应用生成蓝图对象并在内部保存起来,但在应用销毁时不会主动将其移除。 admin._views = [] rest_api.resources = [] self.app = create_app('test') # 必须push context,否则会报错误 self.app_context = self.app.app_context() self.app_context.push() self.client = self.app.test_client(use_cookies=True) # Bug workaround: 如果不在webapp目录中运行, # 则Flask SQLAlchemy的初始化代码就不能正确地在应用对象中进行初始化 db.app = self.app db.create_all() # create role and user # 由于下面有个test_register_and_login测试,要注册新用户, # 在register路由中会默认添加上'poster'和'default'角色,因此这里要先创建两种角色 poster = Role('poster') poster.description = 'poster role' default = Role('default') default.description = 'default role' db.session.add(poster) db.session.add(default) test_user = User('test') test_user.email = '*****@*****.**' test_user.password = '******' test_user.confirmed = True test_user.roles.append(poster) db.session.add(test_user) db.session.commit()
def populate_default_data(db, app): db.app = app db.create_all() user = User() user.username = "******" user.set_password("jim") db.session.add(user) db.session.commit() tag_one = Tag(title="Python") tag_two = Tag(title="Flask") tag_three = Tag(title="SQLAlchemy") tag_four = Tag(title="Jinja") tag_list = [tag_one, tag_two, tag_three, tag_four] s = "Example text" for i in range(100): new_post = Post(title="Post " + str(i)) new_post.user = user new_post.publish_date = datetime.datetime.now() new_post.text = s new_post.tags = random.sample(tag_list, random.randint(1, 3)) db.session.add(new_post) db.session.commit()
def setUp(self): db.app = test_app db.create_all() user = User() user.username = self.username user.set_password(self.password) db.session.add(user) comment = Comment() comment.name = self.comment_name comment.text = self.comment_text tag = Tag() tag.title = self.tag_title post = Post() post.title = self.post_title post.text = self.post_text post.publish_date = self.post_publish_date # add relationships to other tables post.user = user post.tags = [tag] post.comments = [comment] db.session.add(user) db.session.add(comment) db.session.add(tag) db.session.add(post) db.session.commit()
def setUp(self): admin._views = [] rest_api.resources = [] app = create_app('webapp.config.TestConfig') self.client = app.test_client() db.app = app db.create_all()
def setUp(self): db.app = test_app db.create_all() user = User() user.username = self.username user.set_password(self.password) db.session.add(user)
def createdb(): "Inititalise the database" from webapp.models import db var = input("Drop database [N/y]: ") if var.lower() == "y": db.drop_all() db.create_all() db.session.commit()
def setUp(self): app = create_app('webapp.config.TestConfig') self.client = app.test_client() # Bug workaround db.app = app db.create_all()
def create_app(self): app = create_app('webapp.config.TestConfig') self.client = app.test_client() app.config['WTF_CSRF_ENABLED'] = False # Bug workaround db.app = app db.create_all() return app
def create_app(): print('Running with settings from: {}' .format(os.environ['APP_SETTINGS_FILE'])) flask_app = Flask(__name__) flask_app.config.from_envvar('APP_SETTINGS_FILE') flask_app.secret_key = flask_app.config['SECRET_KEY'] with flask_app.app_context(): db.init_app(flask_app) # Required by Flask-SQLAlchemy if not flask_app.config['TESTING']: db.create_all() return flask_app
def setUp(self): # Bug workarounds: Flask Admin和Flask Restful扩展中, # 它们会为应用生成蓝图对象并在内部保存起来,但在应用销毁时不会主动将其移除。 admin._views = [] rest_api.resources = [] self.app = create_app('test') self.app_context = self.app.app_context() self.app_context.push() # Bug workaround: 如果不在webapp目录中运行, # 则Flask SQLAlchemy的初始化代码就不能正确地在应用对象中进行初始化 db.app = self.app db.create_all()
def setup_db(): db.create_all() # role_list = (('admin', 'admin'), ('default', 'default')) # Role.create_roles(role_list) # tag_list = [Tag.create(tag) for tag in ('Python', 'Flask', 'SQLAlchemy', 'Jinja')] # default_role = Role.create_role('default', 'default') # s = 'Body text' admin_role = Role.create_role('admin', 'admin') User.create(username='******', email='*****@*****.**', password='******', roles=admin_role) Post.generate_fake_posts()
def setUp(self): # # Bug workarounds (?) # admin._views = [] rest_api.resources = [] # # Create the test app # app = create_app('webapp.config.TestConfig') self.client = app.test_client() # # Bug workaround # db.app = app db.create_all()
def create_app(object_name): """ An flask application factory, as explained here: http://flask.pocoo.org/docs/patterns/appfactories/ Arguments: object_name: the python path of the config object, e.g. project.config.ProdConfig """ from webapp.models import db app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://*****:*****@localhost:5432/test5' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config.from_object('webapp.config.ProdConfig') #app.config.from_object('webapp.config.DevConfig') db.init_app(app) try: db.create_all() except Exception as e: print(e) bcrypt.init_app(app) oid.init_app(app) login_manager.init_app(app) principals.init_app(app) # db.create_all() app.register_blueprint(account_blueprint) app.register_blueprint(drivers_blueprint) app.register_blueprint(dvir_blueprint) app.register_blueprint(logs_blueprint) app.register_blueprint(trucks_blueprint) app.register_blueprint(elogstation_blueprint) # Create admin import flask_admin as admin1 admin = admin1.Admin(app, 'Example: Auth', index_view=MyAdminIndexView(), base_template='my_master.html') # Add view admin.add_view(MyModelView(User, db.session)) admin.add_view(MyModelView(Person, db.session)) admin.add_view(MyModelView(companyuser, db.session)) admin.add_view(MyModelView(company1, db.session)) admin.add_view(MyModelView(ELD, db.session)) return app
def testapp(request): app = create_app('webapp.settings.TestConfig', env='dev') client = app.test_client() db.app = app db.create_all() if getattr(request.module, "create_user", True): admin = User('admin', 'supersafepassword') db.session.add(admin) db.session.commit() def teardown(): db.session.remove() db.drop_all() request.addfinalizer(teardown) return client
def testapp(request): app = create_app('webapp.settings.TestConfig') client = app.test_client() db.app = app db.create_all() if getattr(request.module, "create_user", True): admin = User('admin', 'supersafepassword') db.session.add(admin) db.session.commit() def teardown(): db.session.remove() db.drop_all() request.addfinalizer(teardown) return client
def setUp(self): # Bug workarounds: Flask Admin和Flask Restful扩展中, # 它们会为应用生成蓝图对象并在内部保存起来,但在应用销毁时不会主动将其移除。 admin._views = [] rest_api.resources = [] self.app = create_app('test') # 必须push context,否则会报错误 self.app_context = self.app.app_context() self.app_context.push() self.client = self.app.test_client() # Bug workaround: 如果不在webapp目录中运行, # 则Flask SQLAlchemy的初始化代码就不能正确地在应用对象中进行初始化 db.app = self.app db.create_all() # create role poster = Role(name='poster') db.session.add(poster) db.session.commit()
def setup_db(): try: db.create_all() admin_role = Role('admin') admin_role.name = "admin" admin_role.description = "admin" db.session.add(admin_role) default_role = Role('default') default_role.name = "default" default_role.description = "default" db.session.add(default_role) admin = User('admin') admin.username = "******" admin.set_password("password") admin.roles.append(admin_role) admin.roles.append(default_role) db.session.add(admin) db.session.commit() except: print("Already ran migration") pass
def create_app(object_name): """ """ from models import db app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///../databasetest.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config.from_object('config.ProdConfig') db.init_app(app) db.create_all() bcrypt.init_app(app) oid.init_app(app) login_manager.init_app(app) principals.init_app(app) celery.init_app(app) # Create admin import flask_admin as admin1 admin = admin1.Admin(app, 'Elogstation', index_view=MyAdminIndexView()) # Add view admin.add_view(MyModelView(User, db.session)) return app
def setup_db(): db.create_all() admin_role = Role() admin_role.name = "admin" admin_role.description = "admin" db.session.add(admin_role) default_role = Role() default_role.name = "default" default_role.description = "default" db.session.add(default_role) admin = User() admin.username = "******" admin.set_password("password") admin.roles.append(admin_role) admin.roles.append(default_role) db.session.add(admin) tag_one = Tag('Python') tag_two = Tag('Flask') tag_three = Tag('SQLAlechemy') tag_four = Tag('Jinja') tag_list = [tag_one, tag_two, tag_three, tag_four] s = "Body text" for i in xrange(100): new_post = Post("Post " + str(i)) new_post.user = admin new_post.publish_date = datetime.datetime.now() new_post.text = s new_post.tags = random.sample(tag_list, random.randint(1, 3)) db.session.add(new_post) db.session.commit()
def init_db(): import random import datetime db.create_all() default = User('default') default.set_password("password") db.session.add(default) db.session.commit() for i in xrange(5): new_account = Account() new_account.name = "Account {}".format(i + 1) new_account.amount = random.random() * (i + 1) new_account.description = "random account {}".format(i + 1) new_account.user_id = default.id new_account.date = datetime.datetime.now() db.session.add(new_account) db.session.commit() for c in candidate_categories: db.session.add(Category(c)) db.session.commit()
def setup_db(): db.create_all() # 创建管理员角色 admin_role = Role('admin') admin_role.description = 'admin' db.session.add(admin_role) # 创建默认角色 default_role = Role('default') default_role.description = 'default' db.session.add(default_role) # 创建管理员用户信息 admin = User('admin') admin.set_password('password') admin.roles.append(admin_role) admin.roles.append(default_role) db.session.add(admin) tag_one = Tag('Python') tag_two = Tag('Flask') tag_three = Tag('SQLAlchemy') tag_four = Tag('Jinja') tag_list = [tag_one, tag_two, tag_three, tag_four] s = 'Body text' for i in range(100): new_post = Post('Post %s' % i, s) new_post.user = admin new_post.publish_date = datetime.datetime.now() new_post.tags = random.sample(tag_list, random.randint(1, 3)) db.session.add(new_post) db.session.commit()
def setup_db(): 'Code to setup initial db with necessary info' db.create_all() admin_role = Role("admin") admin_role.description = "admin of applicaiton" db.session.add(admin_role) poster_role = Role("poster") poster_role.description = "user with ability to post items" db.session.add(poster_role) admin = User("admin") admin.set_password("password") admin.roles.append(admin_role) admin.roles.append(poster_role) db.session.add(admin) tag_one = Tag('clothes') tag_two = Tag('books') tag_three = Tag('technology') tag_four = Tag('sports') tag_five = Tag('music') tag_list = [tag_one, tag_two, tag_three, tag_four, tag_five] s = "Description text" for i in xrange(100): new_item = Item("Item {}".format(i)) new_item.user = admin new_item.added_date = datetime.datetime.now() new_item.description = s new_item.tags = random.sample(tag_list, random.randint(1, 4)) db.session.add(new_item) db.session.commit()
def setup(): db.create_all() admin_role = Role("admin") admin_role.description = "admin" db.session.add(admin_role) default_role = Role("default") default_role.description = "default" db.session.add(default_role) guest_role = Role("guest") guest_role.description = "guest" db.session.add(guest_role) db.session.commit() admin = User("Admin") admin.nickname = "管理员" admin.email = "*****@*****.**" admin.set_password('admin') admin.roles.append(admin_role) db.session.add(admin) official = User("PublicOfficial") official.nickname = "公共库用户" official.email = "*****@*****.**" official.set_password('official') admin.roles.append(admin_role) db.session.add(official) db.session.commit() # 创建原始公共库目录(共5个) script1 = Script('always_work.sg') script1.content = """Strategy T1: return 1""" script1.description = "永远合作" script1.user = official script1.is_private = False db.session.add(script1) script2 = Script('random_example.sg') script2.content = "Strategy T2:i = RANDOM(1);if(i==0){return 0}else{return 1}" script2.description = "`听天由命`,背叛与合作的几率为 50% (RANDOM 为系统范围内真随机)" script2.user = official script2.is_private = False db.session.add(script2) script3 = Script('slow_work.sg') script3.content = "Strategy T3:if(COUNT == 1){return 1}else{return CUR}" script3.description = "`慢半拍`:开局合作,随后一直跟随对方上一轮的选择" script3.user = official script3.is_private = False db.session.add(script3) script4 = Script('elusive_work.sg') script4.content = "Strategy T4:if (COUNT == 1){return 1}else {i = RANDOM(9);if (i == 9){return 0}else{return CUR}}" script4.description = "改进版`慢半拍`,开局合作,随后随机自我背叛,大几率跟随对方上一轮的选择" script4.user = official script4.is_private = False db.session.add(script4) script5 = Script('never_forgive.sg') script5.content = "Strategy T5:if(FLAG){return 0}else{return 1}" script5.description = "永不原谅,只要对方存在过背叛行为,就一直背叛" script5.user = official script5.is_private = False db.session.add(script5) db.session.commit() # 创建文章评论 comment1 = Comment('comment1') comment1.user = admin comment2 = Comment('comment2') comment2.user = official comment3 = Comment('comment3') comment3.user = admin db.session.add(comment1) db.session.add(comment2) db.session.add(comment3) db.session.commit() post1 = Post('Post1', 'Content1') post1.user = official post1.category = PostCategory.General post1.comments.append(comment1) post1.comments.append(comment2) post2 = Post('Post2', 'Content2') post2.user = admin post2.category = PostCategory.Feedback post2.comments.append(comment3) db.session.add(post1) db.session.add(post2) db.session.commit()
def db(app): _db.create_all() yield _db _db.drop_all()
def main(): db.create_all()
def setUp(self): self.in_string = "Salut GrandPy ! Est-ce que tu connais l'adresse d'Openclassrooms à Paris ?" db.create_all() for key in app.config["DATA_LOAD_CONFIG"].keys(): FiletoDbHandler(db, key)() self.parsing_results = ['Openclassrooms', 'à', 'Paris', 'd', 'adresse'] search_term = 'Openclassrooms' self.google_map_api_url = "https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s" % ( search_term, GOOGLE_MAP_API_KEY) self.wikipedia_api_opensearch_url = "https://fr.wikipedia.org/w/api.php?action=opensearch&search=%s&format=json" % search_term self.wikipedia_api_query_url = "https://fr.wikipedia.org/w/api.php?action=query&titles=%s&prop=extracts&format=json" % search_term self.google_map_api_results = { 'results': [{ 'address_components': [{ 'long_name': '7', 'short_name': '7', 'types': ['street_number'] }, { 'long_name': 'Cité Paradis', 'short_name': 'Cité Paradis', 'types': ['route'] }, { 'long_name': 'Paris', 'short_name': 'Paris', 'types': ['locality', 'political'] }, { 'long_name': 'Paris', 'short_name': 'Paris', 'types': ['administrative_area_level_2', 'political'] }, { 'long_name': 'Île-de-France', 'short_name': 'Île-de-France', 'types': ['administrative_area_level_1', 'political'] }, { 'long_name': 'France', 'short_name': 'FR', 'types': ['country', 'political'] }, { 'long_name': '75010', 'short_name': '75010', 'types': ['postal_code'] }], 'formatted_address': '7 Cité Paradis, 75010 Paris, France', 'geometry': { 'location': { 'lat': 48.8747578, 'lng': 2.350564700000001 }, 'location_type': 'ROOFTOP', 'viewport': { 'northeast': { 'lat': 48.87610678029149, 'lng': 2.351913680291502 }, 'southwest': { 'lat': 48.87340881970849, 'lng': 2.349215719708499 } } }, 'place_id': 'ChIJIZX8lhRu5kcRGwYk8Ce3Vc8', 'types': ['establishment', 'point_of_interest'] }], 'status': 'OK' } self.wikipedia_api_opensearch_results = [ "OpenClassrooms", ["OpenClassrooms"], ["OpenClassrooms est une école en ligne"], ["https://fr.wikipedia.org/wiki/OpenClassrooms"] ] self.wikipedia_api_query_results = { 'query': { 'pages': { '4338589': { 'pageid': 4338589, 'ns': 0, 'title': 'OpenClassrooms', 'extract': '<p><b>OpenClassrooms</b> est une école en ligne...</p>' } } } } self.json_results = { 'google_maps_api_results': { 'formatted_address': '7 Cité Paradis, 75010 Paris, France', 'location': { 'lat': 48.8747578, 'lng': 2.350564700000001 } }, 'wikipedia_api_results': { 'title': 'OpenClassrooms', 'description': '<p><b>OpenClassrooms</b> est une école en ligne...</p>', 'url': "https://fr.wikipedia.org/wiki/OpenClassrooms" } }
def setUp(self): db.create_all()
def createdb(): """ Creates a database with all of the tables defined in your SQLAlchemy models """ db.create_all()
from webapp import create_app from webapp.models import db app = create_app() db.app = app db.init_app(app) db.create_all()
from webapp.resources.posts import PostApi from webapp.resources.categories import CategoryApi from webapp.resources.carousels import CarouselApi from webapp.resources.syssettings import SyssetingsApi from webapp.emails import mail # flask应用实例 app = Flask(__name__) # 读取配置文件 app.config.from_object('webapp.config') # 初始化数据库应用 db.init_app(app) # 如果没有表,创建表 db.create_all(app=app) # 初始化邮件发送实例 mail.init_app(app) # 初始化hash算法实例 bcrypt.init_app(app) # 挂载和初始化restful api restful_api = Api() restful_api.add_resource(UsersApi, '/api/users') restful_api.add_resource(AuthApi, '/api/authenticate') restful_api.add_resource(CaptchaApi, '/api/captchas') restful_api.add_resource(PostApi, '/api/posts', '/api/posts/<string:post_id>') restful_api.add_resource(CategoryApi, '/api/posts/categories') restful_api.add_resource(CarouselApi, '/api/carousels')