Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logging infrastructure #27

Merged
merged 8 commits into from
Apr 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions bikeshare_app/config.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,51 @@
"""Module containing configurations for the flask server"""
from os import getenv
# TODO: Setup a logging environment
import logging


# pylint: disable=too-few-public-methods
class BaseConfig(object):
"""The base configuration that should be used in production"""
DEBUG = False
TESTING = False
MONGODB_SETTINGS = {'db': 'bikeshare'}
LOGGING_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
LOGGING_LOCATION = 'bikeshare.log'
LOGGING_LEVEL = logging.INFO


class TestingConfig(BaseConfig):
"""Configuration for running unit tests"""
DEBUG = False
TESTING = True
MONGODB_SETTINGS = {'db': 'bikeshare-test'}
LOGGING_LEVEL = logging.DEBUG


class DevelopmentConfig(BaseConfig):
"""The configuration that should be run during development"""
DEBUG = True
TESTING = True
MONGODB_SETTINGS = {'db': 'bikeshare-test'}
LOGGING_LEVEL = logging.DEBUG


CONFIG = {
"development": "bikeshare_app.config.DevelopmentConfig",
"testing": "bikeshare_app.config.TestingConfig",
"default": "bikeshare_app.config.BaseConfig"
'development': 'bikeshare_app.config.DevelopmentConfig',
'testing': 'bikeshare_app.config.TestingConfig',
'default': 'bikeshare_app.config.BaseConfig',
}


def configure_app(app):
"""Configure the app based on where it is deployed. First check for the
FLASK_ENVIRONMENT environment variable, fallback to the default
BaseConfig"""
config_name = getenv("FLASK_ENVIRONMENT", "default")
print(config_name)
config_name = getenv('FLASK_ENVIRONMENT', 'default')
app.config.from_object(CONFIG[config_name])
handler = logging.FileHandler(app.config['LOGGING_LOCATION'])
handler.setLevel(app.config['LOGGING_LEVEL'])
logging.getLogger().setLevel(app.config['LOGGING_LEVEL'])
formatter = logging.Formatter(app.config['LOGGING_FORMAT'])
handler.setFormatter(formatter)
app.logger.addHandler(handler)

21 changes: 14 additions & 7 deletions bikeshare_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ def user_loader(email):
@app.route('/')
def index():
"""Return the index page for the app"""
app.logger.debug('Rendering index page')
return render_template('bikes.html',
bikes=ActiveShare.objects(available=True))


@app.route('/login', methods=['GET', 'POST'])
def login():
"""Page to both login and process logins"""
if request.method == 'GET':
# Allow user through if check passed, else log attempt and return home
return render_template('login.html')
Expand All @@ -54,16 +56,21 @@ def logout():
return redirect(url_for('index'))


# Admin page
# Display bike availabilities again. Ability to alter database info
@app.route('/admin')
@flask_login.login_required
def admin():
"""The admin page"""
return render_template('admin.html')
"""The admin page.

Display the bike availabilities again. Also add ability to alter
the DB's
"""
app.logger.debug('Rendering admin page')
# TODO Check user credentials for admin role
# Allow user through if check passed, else log attempt and return home
return render_template('admin.html')

@app.route('/request_bike/<user_email>/<int:bike_id>')
def request_bike():
@app.route("/request_bike/<user_email>/<int:bike_id>")
def request_bike(user_email, bike_id):
"""This endpoint is for requesting a bike"""
return 'Bike request'
app.logger.debug('User {} requesting bike {}'.format(user_email, bike_id))
return render_template('admin.html')