Merge branch 'master' into research-mode-implementation

Conflicts:
	tests/app/conftest.py
This commit is contained in:
Martyn Inglis
2016-06-02 11:53:06 +01:00
9 changed files with 219 additions and 49 deletions

View File

@@ -52,7 +52,7 @@ def create_app(app_name=None):
statsd_client.init_app(application)
firetext_client.init_app(application, statsd_client=statsd_client)
loadtest_client.init_app(application, statsd_client=statsd_client)
mmg_client.init_app(application.config, statsd_client=statsd_client)
mmg_client.init_app(application, statsd_client=statsd_client)
aws_ses_client.init_app(application.config['AWS_REGION'], statsd_client=statsd_client)
notify_celery.init_app(application)
encryption.init_app(application)

View File

@@ -1,11 +1,12 @@
import logging
from monotonic import monotonic
from requests import request, RequestException, HTTPError
from app.clients.sms import (
SmsClient,
SmsClientException
)
from flask import current_app
from requests import request, RequestException, HTTPError
from app.clients import STATISTICS_DELIVERED, STATISTICS_FAILURE
logger = logging.getLogger(__name__)
@@ -55,10 +56,11 @@ class FiretextClient(SmsClient):
FireText sms client.
'''
def init_app(self, config, statsd_client, *args, **kwargs):
def init_app(self, current_app, statsd_client, *args, **kwargs):
super(SmsClient, self).__init__(*args, **kwargs)
self.api_key = config.config.get('FIRETEXT_API_KEY')
self.from_number = config.config.get('FIRETEXT_NUMBER')
self.current_app = current_app
self.api_key = current_app.config.get('FIRETEXT_API_KEY')
self.from_number = current_app.config.get('FIRETEXT_NUMBER')
self.name = 'firetext'
self.statsd_client = statsd_client
@@ -86,6 +88,14 @@ class FiretextClient(SmsClient):
if firetext_response['code'] != 0:
raise FiretextClientException(firetext_response)
response.raise_for_status()
self.current_app.logger.info(
"API {} request on {} succeeded with {} '{}'".format(
"POST",
"https://www.firetext.co.uk/api/sendsms",
response.status_code,
firetext_response
)
)
except RequestException as e:
api_error = HTTPError.create(e)
logger.error(
@@ -100,6 +110,6 @@ class FiretextClient(SmsClient):
raise api_error
finally:
elapsed_time = monotonic() - start_time
current_app.logger.info("Firetext request finished in {}".format(elapsed_time))
self.current_app.logger.info("Firetext request finished in {}".format(elapsed_time))
self.statsd_client.timing("notifications.clients.firetext.request-time", elapsed_time)
return response

View File

@@ -1,5 +1,4 @@
import json
from flask import current_app
from monotonic import monotonic
from requests import (request, RequestException, HTTPError)
from app.clients import (STATISTICS_DELIVERED, STATISTICS_FAILURE)
@@ -57,10 +56,11 @@ class MMGClient(SmsClient):
MMG sms client
'''
def init_app(self, config, statsd_client, *args, **kwargs):
def init_app(self, current_app, statsd_client, *args, **kwargs):
super(SmsClient, self).__init__(*args, **kwargs)
self.api_key = config.get('MMG_API_KEY')
self.from_number = config.get('MMG_FROM_NUMBER')
self.current_app = current_app
self.api_key = current_app.config.get('MMG_API_KEY')
self.from_number = current_app.config.get('MMG_FROM_NUMBER')
self.name = 'mmg'
self.statsd_client = statsd_client
@@ -84,12 +84,19 @@ class MMGClient(SmsClient):
headers={'Content-Type': 'application/json',
'Authorization': 'Basic {}'.format(self.api_key)})
if response.status_code != 200:
error = response.text
raise MMGClientException(json.loads(error))
raise MMGClientException(response.json())
response.raise_for_status()
self.current_app.logger.info(
"API {} request on {} succeeded with {} '{}'".format(
"POST",
"https://www.mmgrp.co.uk/API/json/api.php",
response.status_code,
response.json()
)
)
except RequestException as e:
api_error = HTTPError.create(e)
current_app.logger.error(
self.current_app.logger.error(
"API {} request on {} failed with {} '{}'".format(
"POST",
"https://www.mmgrp.co.uk/API/json/api.php",
@@ -102,5 +109,5 @@ class MMGClient(SmsClient):
finally:
elapsed_time = monotonic() - start_time
self.statsd_client.timing("notifications.clients.mmg.request-time", elapsed_time)
current_app.logger.info("MMG request finished in {}".format(elapsed_time))
self.current_app.logger.info("MMG request finished in {}".format(elapsed_time))
return response

View File

@@ -5,7 +5,6 @@ from flask import (
current_app
)
import bleach
from sqlalchemy.exc import IntegrityError
from app.dao.templates_dao import (
dao_update_template,
@@ -59,14 +58,18 @@ def update_template(service_id, template_id):
fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
current_data = dict(template_schema.dump(fetched_template).data.items())
current_data.update(request.get_json())
current_data['content'] = _strip_html(current_data['content'])
updated_template = dict(template_schema.dump(fetched_template).data.items())
updated_template.update(request.get_json())
updated_template['content'] = _strip_html(updated_template['content'])
# Check if there is a change to make.
if _template_has_not_changed(current_data, updated_template):
return jsonify(data=updated_template), 200
update_dict, errors = template_schema.load(current_data)
update_dict, errors = template_schema.load(updated_template)
if errors:
return jsonify(result="error", message=errors), 400
over_limit, json_resp = _content_count_greater_than_limit(
current_data['content'],
updated_template['content'],
fetched_template.template_type)
if over_limit:
return json_resp, 400
@@ -115,3 +118,10 @@ def get_template_versions(service_id, template_id):
def _strip_html(content):
return bleach.clean(content, tags=[], strip=True)
def _template_has_not_changed(current_data, updated_template):
return all(
current_data[key] == updated_template[key]
for key in ('name', 'content', 'subject', 'archived')
)