Merge pull request #1617 from alphagov/remove-flask-script

remove flask-script
This commit is contained in:
Leo Hemsted
2017-11-15 17:47:30 +00:00
committed by GitHub
12 changed files with 68 additions and 89 deletions

View File

@@ -69,6 +69,9 @@ export DANGEROUS_SALT='dev-notify-salt'
export SECRET_KEY='notify-secret-key'
export DESKPRO_API_HOST="some-host"
export DESKPRO_API_KEY="some-key"
export FLASK_APP=application.py
export FLASK_DEBUG=1
export WERKZEUG_DEBUG_PIN=off
"> environment.sh
```

20
app.py
View File

@@ -1,20 +0,0 @@
import os
from flask_script import Manager, Server
from app import create_app
application = create_app()
manager = Manager(application)
port = int(os.environ.get('PORT', 6012))
manager.add_command("runserver", Server(host='0.0.0.0', port=port))
@manager.command
def list_routes():
"""List URLs of all application routes."""
for rule in sorted(application.url_map.iter_rules(), key=lambda r: r.rule):
print("{:10} {}".format(", ".join(rule.methods - set(['OPTIONS', 'HEAD'])), rule.rule))
if __name__ == '__main__':
manager.run()

View File

@@ -7,7 +7,6 @@ import itertools
import ago
from itsdangerous import BadSignature
from flask import (
Flask,
session,
render_template,
make_response,
@@ -37,6 +36,7 @@ from werkzeug.exceptions import abort
from werkzeug.local import LocalProxy
from app import proxy_fix
from app.config import configs
from app.asset_fingerprinter import AssetFingerprinter
from app.its_dangerous_session import ItsdangerousSessionInterface
from app.notify_client.service_api_client import ServiceAPIClient
@@ -54,8 +54,8 @@ from app.notify_client.models import AnonymousUser
from app.notify_client.letter_jobs_client import LetterJobsClient
from app.notify_client.inbound_number_client import InboundNumberClient
from app.notify_client.billing_api_client import BillingAPIClient
from app.commands import setup_commands
from app.utils import get_cdn_domain
from app.utils import gmt_timezones
login_manager = LoginManager()
@@ -82,10 +82,8 @@ billing_api_client = BillingAPIClient()
current_service = LocalProxy(partial(_lookup_req_object, 'service'))
def create_app():
from app.config import configs
application = Flask(__name__)
def create_app(application):
setup_commands(application)
notify_environment = os.environ['NOTIFY_ENVIRONMENT']
@@ -128,40 +126,12 @@ def create_app():
application.session_interface = ItsdangerousSessionInterface()
application.add_template_filter(format_datetime)
application.add_template_filter(format_datetime_24h)
application.add_template_filter(format_datetime_normal)
application.add_template_filter(format_datetime_short)
application.add_template_filter(format_time)
application.add_template_filter(valid_phone_number)
application.add_template_filter(linkable_name)
application.add_template_filter(format_date)
application.add_template_filter(format_date_normal)
application.add_template_filter(format_date_short)
application.add_template_filter(format_datetime_relative)
application.add_template_filter(format_delta)
application.add_template_filter(format_notification_status)
application.add_template_filter(format_notification_status_as_time)
application.add_template_filter(format_notification_status_as_field_status)
application.add_template_filter(format_notification_status_as_url)
application.add_template_filter(formatted_list)
application.add_template_filter(nl2br)
application.add_template_filter(format_phone_number_human_readable)
application.after_request(useful_headers_after_request)
application.after_request(save_service_after_request)
application.before_request(load_service_before_request)
@application.context_processor
def _attach_current_service():
return {'current_service': current_service}
add_template_filters(application)
register_errorhandlers(application)
setup_event_handlers()
return application
def init_csrf(application):
csrf.init_app(application)
@@ -185,6 +155,13 @@ def init_csrf(application):
def init_app(application):
application.after_request(useful_headers_after_request)
application.after_request(save_service_after_request)
application.before_request(load_service_before_request)
@application.context_processor
def _attach_current_service():
return {'current_service': current_service}
@application.before_request
def record_start_time():
@@ -520,3 +497,28 @@ def setup_event_handlers():
from app.event_handlers import on_user_logged_in
user_logged_in.connect(on_user_logged_in)
def add_template_filters(application):
for fn in [
format_datetime,
format_datetime_24h,
format_datetime_normal,
format_datetime_short,
format_time,
valid_phone_number,
linkable_name,
format_date,
format_date_normal,
format_date_short,
format_datetime_relative,
format_delta,
format_notification_status,
format_notification_status_as_time,
format_notification_status_as_field_status,
format_notification_status_as_url,
formatted_list,
nl2br,
format_phone_number_human_readable,
]:
application.add_template_filter(fn)

11
app/commands.py Normal file
View File

@@ -0,0 +1,11 @@
from flask import current_app
def list_routes():
"""List URLs of all application routes."""
for rule in sorted(current_app.url_map.iter_rules(), key=lambda r: r.rule):
print("{:10} {}".format(", ".join(rule.methods - set(['OPTIONS', 'HEAD'])), rule.rule))
def setup_commands(application):
application.cli.command('list-routes')(list_routes)

View File

@@ -4,7 +4,6 @@ from flask import (
jsonify,
request,
url_for,
current_app
)
from flask_login import login_required
@@ -21,27 +20,11 @@ from app.utils import (
get_template,
get_time_left,
get_letter_timings,
REQUESTED_STATUSES,
FAILURE_STATUSES,
SENDING_STATUSES,
DELIVERED_STATUSES,
)
def get_status_arg(filter_args):
if 'status' not in filter_args or not filter_args['status']:
return REQUESTED_STATUSES
elif filter_args['status'] == 'sending':
return SENDING_STATUSES
elif filter_args['status'] == 'delivered':
return DELIVERED_STATUSES
elif filter_args['status'] == 'failed':
return FAILURE_STATUSES
else:
current_app.logger.info('Unrecognised status filter: {}'.format(filter_args['status']))
return REQUESTED_STATUSES
@main.route("/services/<service_id>/notification/<uuid:notification_id>")
@login_required
@user_has_permissions('view_activity', admin_override=True)

View File

@@ -1,13 +1,15 @@
from whitenoise import WhiteNoise
import os
from app import create_app # noqa
from flask import Flask
from whitenoise import WhiteNoise
from app import create_app
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'app', 'static')
STATIC_URL = 'static/'
application = WhiteNoise(create_app(), STATIC_ROOT, STATIC_URL)
app = Flask('app')
if __name__ == "__main__":
application.run()
create_app(app)
application = WhiteNoise(app, STATIC_ROOT, STATIC_URL)

View File

@@ -1,7 +1,7 @@
---
buildpack: python_buildpack
command: scripts/run_app_paas.sh gunicorn -c /home/vcap/app/gunicorn_config.py --error-logfile /home/vcap/logs/gunicorn_error.log -w 5 -b 0.0.0.0:$PORT wsgi
command: scripts/run_app_paas.sh gunicorn -c /home/vcap/app/gunicorn_config.py --error-logfile /home/vcap/logs/gunicorn_error.log -w 5 -b 0.0.0.0:$PORT application
services:
- notify-aws
- notify-config

View File

@@ -1,7 +1,7 @@
---
buildpack: python_buildpack
command: scripts/run_app_paas.sh gunicorn -w 5 -b 0.0.0.0:$PORT wsgi
command: scripts/run_app_paas.sh gunicorn -w 5 -b 0.0.0.0:$PORT application
services:
- notify-aws
- notify-config

View File

@@ -1,6 +1,5 @@
ago==0.0.92
Flask==0.12.2
Flask-Script==2.0.5
Flask-WTF==0.14.2
Flask-Login==0.4.0

View File

@@ -1,4 +1,4 @@
#!/bin/bash
source environment.sh
python3 app.py runserver
flask run -p 6012

View File

@@ -34,7 +34,7 @@ def test_form_contains_next_24h():
@freeze_time("2016-01-01 11:09:00.061258")
def test_form_defaults_to_now():
def test_form_defaults_to_now(client):
assert ChooseTimeForm().scheduled_for.data == ''

View File

@@ -5,7 +5,7 @@ from unittest.mock import Mock
import pytest
from notifications_python_client.errors import HTTPError
from flask import url_for
from flask import url_for, Flask
from bs4 import BeautifulSoup
from app import create_app
@@ -34,17 +34,16 @@ import json
@pytest.fixture(scope='session')
def app_(request):
app = create_app()
app = Flask('app')
create_app(app)
ctx = app.app_context()
ctx.push()
def teardown():
ctx.pop()
request.addfinalizer(teardown)
app.test_client_class = TestClient
return app
yield app
ctx.pop()
@pytest.fixture(scope='function')