def setUp(self): self.db_fd, app.app.config['DATABASE'] = tempfile.mkstemp() app.app.config['TESTING'] = True app.app.config['WTF_CSRF_ENABLED'] = False self.app = app.app.test_client() with app.app.app_context(): init_db()
def client(): # This fixture accesses main database. # TODO: find a way to use temporary database Base.metadata.drop_all(engine) init_db() app.config["TESTING"] = True client = app.test_client() yield client
def create_app(): """Construct the core app object.""" app = Flask(__name__, instance_relative_config=False) # Application Configuration app.config.from_object('config.Config') # Initialize Plugins login_manager.init_app(app) with app.app_context(): import routes import auth from assets import compile_static_assets # Register Blueprints app.register_blueprint(routes.main_bp) app.register_blueprint(auth.auth_bp) # Create Database Models app = init_db(app) # Compile static assets if app.config['FLASK_ENV'] == 'development': compile_static_assets(app) return app
def before_first_request(): init_db() user_datastore.find_or_create_role(name='admin', description='Administrator') user_datastore.find_or_create_role(name='user', description='User') user_datastore.find_or_create_role(name='operator', description='Operator') encrypted_password = encrypt_password('password') if not user_datastore.get_user('*****@*****.**'): user_datastore.create_user(email='*****@*****.**', username='******', password='******') if not user_datastore.get_user('*****@*****.**'): user_datastore.create_user(email='*****@*****.**', username='******', password='******') db_session.commit() user_datastore.add_role_to_user('*****@*****.**', 'admin') user_datastore.add_role_to_user('*****@*****.**', 'operator') db_session.commit()
from database.database import db_session, init_db from app.models import Chart, Data, DataSource import logging from scrapers.yandex import get_temp_yandex from scrapers.sochicamera import get_temp_sochicamera from scrapers.worldseatemp import get_temp_worldseatemp from tools import create_source, create_chart import requests logging.basicConfig(level=logging.DEBUG) logging.getLogger(__name__) init_db() # Create db if empty. chart_name = "The Black Sea temp" chart_id = Chart.query.filter(Chart.chart_name == chart_name) chart_id = chart_id.all() # Create sea chart if need. if not chart_id: chart_id = create_chart(chart_name) else: chart_id = chart_id[0].chart_id def yandex_temp(): logging.debug("yandex_temp() started") source_name = "yandex" source_id = DataSource.query.filter(DataSource.source_name == source_name)
#-*- coding:utf-8 -*- from flask import Flask, g, request, render_template from bae.core.wsgi import WSGIApplication from bae.api import bcs from admin.admin import admin from oauth.oauth import oauth from intro import intro from database.database import init_db, db_session init_db() app = Flask(__name__) app.debug = True app.secret_key = 'b8Ge798&32hLiPA /*z!~/ogiu921' app.register_blueprint(intro, url_prefix = '/') app.register_blueprint(admin, url_prefix = '/admin') app.register_blueprint(oauth, url_prefix = '/oauth') application = WSGIApplication(app) #@app.teardown_appcontext #def shutdown_session(exception=None): # db_session.remove()
def setup(): CommandUtil.init(Commands) init_db()
from flask import Flask, request from database.database import init_db from database.models import * app = Flask(__name__) init_db(app) @app.route('/users/all', methods=['GET']) def find_all_users(): return app.response_class(response=User.objects().to_json(), status=200, mimetype='application/json') @app.route('/users/<string:id>', methods=['GET']) def find_one_user(id): data = User.objects(id=id) if not data: return app.response_class(status=404, mimetype='application/json') return app.response_class(response=data.to_json(), status=200, mimetype='application/json') @app.route('/users', methods=['POST']) def add_user():
balance = request.json[Abonent.BALANCE] holds = request.json[Abonent.HOLDS] account_status = request.json[Abonent.ACCOUNT_STATUS] abonent = Abonent(account_id, account_name, balance, holds, account_status) if Abonent.query.get(account_id) is None: # TODO: add retry system db_session.add(abonent) db_session.commit() return create_response(200, True, abonent.serialize, "Successfully added new abonent") return create_response(400, False, f"Duplicate key: {account_id}") @app.route('/api/get_abonents', methods=['GET']) def get_abonents(): """Получить всех пользователей""" abonents = Abonent.query.all() return jsonify([i.serialize for i in abonents]) if __name__ == '__main__': init_db() init_default_values() hold_updater = HoldUpdater(app.logger) hold_updater.start() app.run(host='0.0.0.0', debug=True)
""" The model receives commands from the controller, and interacts with the host computer: - reads data from db - writes data - writes to console - logs - etc The db model is built using SQLAlchemy """ import database.database as database from database.data_models import Event, Info, User import time database.init_db() session = database.db_session def teardown(app): @app.teardown_appcontext def shutdown_session(exception=None): session.remove() # dal for user def check_user_login(username, password): users = session.query(User).filter_by(username=username).filter_by( password=password).all() if not users: return False, ""
def initialize_database(): database.create_tables() database.init_db() database.adminInit() return redirect(url_for('site.home_page'))
def init(): init_db()
""" The model receives commands from the controller, and interacts with the host computer: - reads data from db - writes data - writes to console - logs - etc The db model is built using SQLAlchemy """ import database.database as database from database.data_models import Person database.init_db() session = database.db_session def teardown(app): @app.teardown_appcontext def shutdown_session(exception=None): session.remove() def query_all_people(): q = Person.query.all() return q def add_person(firstname, lastname):