def setUp(self): self.app = app self.client = self.app.test_client() with self.app.app_context(): # create all tables db.session.close() db.drop_all() db.create_all()
def create_app(): app.config['SECRET_KEY'] = os.urandom(32) app.config[ 'SQLALCHEMY_DATABASE_URI'] = 'postgresql://localhost/durak?user=&password='******'SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) login_manager.init_app(app) socketio.init_app(app) bootstrap.init_app(app) with app.app_context(): db.create_all()
def get(self): remove_file('data.csv') remove_file('data.db') remove_file('svm_model.sav') remove_directory('train') remove_directory('croped_images') remove_directory('images') remove_directory('temp_croped_images') remove_directory('temp_images') db.create_all() return {"msg": "success"}, 200
def create_all(): from database.db import db with app.app_context(): db.init_app(app) db.create_all() db.session.commit() hashed_passwd = generate_password_hash('test12345', method='sha256') new_user = User(username='******', email='*****@*****.**', password=hashed_passwd) db.session.add(new_user) db.session.commit() return True
def create_app(): # Flask 객체 app 생성 및 config 변수 적용 app = Flask(__name__, static_url_path="/static") CORS(app, supports_credentials=True) # app object에 config 적용 app.config.from_object(config) # jwt 적용을 위한 JMTManager 적용 jwt.init_app(app) # auth 객체 blueprint 등록 app.register_blueprint(auth, url_prefix="/auth") # api 설정 및 적용 api = Api(app) set_api_resources(api) # db 적용 및 migrate db.init_app(app) db.create_all(app=app) migrate.init_app(app, db) # JWT 암시적 로그인 연장을 위한 코드 # app에 대한 모든 HTTP request 요청 실행 후 refresh # 여부를 확인하고 refresh 한다. @app.after_request def add_header(response): response.headers["X-Content-Type-Options"] = "nosniff" return response @app.after_request def refresh_expiring_jwts(response): try: # 현재 accessToken의 expire time이 30분 미만 남았을 때 # accessToken을 refresh 시켜준다. exp_timestamp = get_jwt()["exp"] now = datetime.now(timezone.utc) target_timestamp = datetime.timestamp(now + timedelta(minutes=30)) if target_timestamp > exp_timestamp: access_token = create_access_token(identity=get_jwt_identity()) set_access_cookies(response, access_token) return response except (RuntimeError, KeyError): # Case where there is not a valid JWT. Just return the original response # 유효한 Access Token이 아닐 때는 기존 response를 그대로 보낸다. print("error") return response return app
def create_tables(): db.create_all()
now = datetime.now() tweet_entry.created = now db.session.commit() return redirect(url_for('index_get', id=tweet_id)) tweet = get_tweet(tweet_id).AsDict() db.session.add(Tweet(tweet)) db.session.commit() return redirect(url_for('index_get', id=tweet_id)) @app.route('/generate/<tweet_id>', methods=['GET']) def generate_get(tweet_id): tweet = get_tweet(tweet_id) file_io = generate_image(tweet.AsDict()) return send_file(file_io, attachment_filename='image.jpeg') @app.route('/favicon.ico') def favicon(): return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon') if __name__ == '__main__': with app.test_request_context(): db.create_all() app.run(port=port)
def setup_database(application): with application.app_context(): db.create_all()
def db_create(): db.create_all() print('Database created!')
def init_app(): db.create_all()