From 1e95c4243ce2a585aaef051d3500c42060be7eae Mon Sep 17 00:00:00 2001 From: "Sachin S. Kamath" <sskamath96@gmail.com> Date: Thu, 19 Oct 2017 18:10:51 +0530 Subject: [PATCH] Fixed bug in server run and warn user of running mode Signed-off-by: Sachin S. Kamath <sskamath96@gmail.com> --- hodor/__init__.py | 42 +++++++++++++++++++++------------------ hodor/controllers/user.py | 4 ++-- hodor/models/__init__.py | 1 + hodor/models/user.py | 1 + manage.py | 2 -- requirements.txt | 1 + runserver.py | 5 ++--- 7 files changed, 30 insertions(+), 26 deletions(-) diff --git a/hodor/__init__.py b/hodor/__init__.py index 61b6d17..9b9b1ba 100644 --- a/hodor/__init__.py +++ b/hodor/__init__.py @@ -1,5 +1,7 @@ +# -*- coding: utf-8 -*- # app/__init__.py import time +import copy from flask_api import FlaskAPI from flask_sqlalchemy import SQLAlchemy # local import @@ -8,28 +10,30 @@ from instance.config import app_config # initialize sql-alchemy db = SQLAlchemy() -from hodor.models import * +from hodor.models import * # noqa +# TODO: Don't forget to change when in production +config_name = 'development' -def create_app(config_name): - app = FlaskAPI(__name__, instance_relative_config=True) - app.config.from_object(app_config[config_name]) - app.config.from_pyfile('config.py') - app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False - db.init_app(app) - if config_name != 'production': - for x in range(0,5): - print "*** YOU ARE NOT RUNNING THE APP IN PRODUCTION MODE!! *** CTRL + C TO ABORT. APP WILL RUN IN {} SECONDS".format(5-x) - time.sleep(1) +application = FlaskAPI(__name__, instance_relative_config=True) +application.config.from_object(app_config[config_name]) +application.config.from_pyfile('config.py') +application.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +db.init_app(application) - if config_name == 'production': - app.config['DEFAULT_RENDERERS'] = [ - 'flask.ext.api.renderers.JSONRenderer', - ] +# Delay the application launch to get the user attention if running in testing mode +if config_name != 'production': + for x in range(0, 5): + print("You are not running the server in production mode.\ + App will run in {} seconds".format(5-x)) + time.sleep(1) - return app +# This disables the HTML API renderer for flask_api when called through browsers. +if config_name == 'production': + application.config['DEFAULT_RENDERERS'] = [ + 'flask.ext.api.renderers.JSONRenderer', + ] +app = application -app = create_app('testing') - -from hodor.controllers import * +from hodor.controllers import * # noqa diff --git a/hodor/controllers/user.py b/hodor/controllers/user.py index 82f68d5..e8ddfce 100644 --- a/hodor/controllers/user.py +++ b/hodor/controllers/user.py @@ -5,10 +5,10 @@ from flask import jsonify @app.route('/') def start(): - return jsonify({'hello':'world'}) + return jsonify({'hello': 'world'}) @app.route('/print', methods=['GET', 'POST']) def printer(): - form = {'noob' : 'shit'} + form = {'hello': 'user'} return form diff --git a/hodor/models/__init__.py b/hodor/models/__init__.py index e69de29..9bad579 100644 --- a/hodor/models/__init__.py +++ b/hodor/models/__init__.py @@ -0,0 +1 @@ +# coding=utf-8 diff --git a/hodor/models/user.py b/hodor/models/user.py index c74526c..05ee19c 100644 --- a/hodor/models/user.py +++ b/hodor/models/user.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- + from hodor import db class User(db.Model): diff --git a/manage.py b/manage.py index e3f8d3f..73263fc 100644 --- a/manage.py +++ b/manage.py @@ -1,8 +1,6 @@ -import os from flask_script import Manager # class for handling a set of commands from flask_migrate import Migrate, MigrateCommand from hodor import db, create_app -from hodor.models.user import User app = create_app(config_name='development') diff --git a/requirements.txt b/requirements.txt index 0ce32c8..11e9af0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ Flask Flask-WTF Jinja2 Flask-DebugToolbar +flask_api diff --git a/runserver.py b/runserver.py index 4b3da66..b4fd6db 100755 --- a/runserver.py +++ b/runserver.py @@ -1,6 +1,5 @@ +#!/bin/env python from hodor import app -config_name = 'development' - if __name__ == '__main__': - app.run() \ No newline at end of file + app.run() -- GitLab