2016-06-28 15:17:36 +01:00
|
|
|
from datetime import date
|
2016-01-14 11:30:45 +00:00
|
|
|
|
2016-02-22 12:55:18 +00:00
|
|
|
from flask import (
|
|
|
|
|
jsonify,
|
2016-06-07 16:31:10 +01:00
|
|
|
request,
|
2016-06-28 15:17:36 +01:00
|
|
|
Blueprint,
|
|
|
|
|
current_app
|
2016-02-22 12:55:18 +00:00
|
|
|
)
|
2016-01-08 17:51:46 +00:00
|
|
|
from sqlalchemy.orm.exc import NoResultFound
|
2016-02-19 15:53:45 +00:00
|
|
|
|
2016-07-18 12:03:44 +01:00
|
|
|
from app.models import EMAIL_TYPE, SMS_TYPE
|
2016-02-25 12:11:51 +00:00
|
|
|
from app.dao.api_key_dao import (
|
|
|
|
|
save_model_api_key,
|
|
|
|
|
get_model_api_keys,
|
2016-06-22 15:27:28 +01:00
|
|
|
get_unsigned_secret,
|
|
|
|
|
expire_api_key)
|
2016-01-12 10:39:49 +00:00
|
|
|
from app.dao.services_dao import (
|
2016-02-19 15:53:45 +00:00
|
|
|
dao_fetch_service_by_id,
|
|
|
|
|
dao_fetch_all_services,
|
|
|
|
|
dao_create_service,
|
|
|
|
|
dao_update_service,
|
2016-03-01 15:36:31 +00:00
|
|
|
dao_fetch_all_services_by_user,
|
2016-03-22 13:14:23 +00:00
|
|
|
dao_add_user_to_service,
|
2016-07-18 12:03:44 +01:00
|
|
|
dao_remove_user_from_service,
|
|
|
|
|
dao_fetch_stats_for_service
|
2016-02-19 15:53:45 +00:00
|
|
|
)
|
2016-06-28 15:17:36 +01:00
|
|
|
from app.dao import notifications_dao
|
2016-04-28 12:01:27 +01:00
|
|
|
from app.dao.provider_statistics_dao import get_fragment_count
|
2016-02-25 12:11:51 +00:00
|
|
|
from app.dao.users_dao import get_model_users
|
2016-01-13 12:14:21 +00:00
|
|
|
from app.schemas import (
|
2016-02-19 15:53:45 +00:00
|
|
|
service_schema,
|
2016-02-26 12:00:16 +00:00
|
|
|
api_key_schema,
|
2016-04-28 12:01:27 +01:00
|
|
|
user_schema,
|
2016-06-06 12:37:06 +01:00
|
|
|
from_to_date_schema,
|
2016-06-28 15:17:36 +01:00
|
|
|
permission_schema,
|
|
|
|
|
notification_status_schema,
|
|
|
|
|
notifications_filter_schema,
|
2016-07-18 12:03:44 +01:00
|
|
|
detailed_service_schema
|
2016-03-03 15:17:14 +00:00
|
|
|
)
|
2016-06-28 15:17:36 +01:00
|
|
|
from app.utils import pagination_links
|
2016-06-14 15:07:23 +01:00
|
|
|
from app.errors import (
|
|
|
|
|
register_errors,
|
|
|
|
|
InvalidRequest
|
|
|
|
|
)
|
2016-02-19 15:53:45 +00:00
|
|
|
|
2016-01-14 16:13:27 +00:00
|
|
|
service = Blueprint('service', __name__)
|
2016-02-17 17:04:50 +00:00
|
|
|
register_errors(service)
|
|
|
|
|
|
2016-01-08 17:51:46 +00:00
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
@service.route('', methods=['GET'])
|
|
|
|
|
def get_services():
|
|
|
|
|
user_id = request.args.get('user_id', None)
|
|
|
|
|
if user_id:
|
|
|
|
|
services = dao_fetch_all_services_by_user(user_id)
|
|
|
|
|
else:
|
|
|
|
|
services = dao_fetch_all_services()
|
2016-06-14 15:07:23 +01:00
|
|
|
data = service_schema.dump(services, many=True).data
|
2016-02-19 15:53:45 +00:00
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
|
|
|
|
2016-03-11 12:39:55 +00:00
|
|
|
@service.route('/<uuid:service_id>', methods=['GET'])
|
2016-02-19 15:53:45 +00:00
|
|
|
def get_service_by_id(service_id):
|
2016-07-18 12:03:44 +01:00
|
|
|
if 'detailed' in request.args:
|
|
|
|
|
return get_detailed_service(service_id)
|
|
|
|
|
else:
|
|
|
|
|
fetched = dao_fetch_service_by_id(service_id)
|
2016-03-11 15:34:20 +00:00
|
|
|
|
2016-07-18 12:03:44 +01:00
|
|
|
data = service_schema.dump(fetched).data
|
|
|
|
|
return jsonify(data=data)
|
2016-02-19 15:53:45 +00:00
|
|
|
|
|
|
|
|
|
2016-01-15 16:44:46 +00:00
|
|
|
@service.route('', methods=['POST'])
|
2016-01-08 17:51:46 +00:00
|
|
|
def create_service():
|
2016-02-19 15:53:45 +00:00
|
|
|
data = request.get_json()
|
|
|
|
|
if not data.get('user_id', None):
|
2016-06-14 15:07:23 +01:00
|
|
|
errors = {'user_id': ['Missing data for required field.']}
|
|
|
|
|
raise InvalidRequest(errors, status_code=400)
|
2016-02-19 15:53:45 +00:00
|
|
|
|
2016-03-11 15:34:20 +00:00
|
|
|
user = get_model_users(data['user_id'])
|
2016-02-19 17:07:59 +00:00
|
|
|
data.pop('user_id', None)
|
2016-06-14 15:07:23 +01:00
|
|
|
valid_service = service_schema.load(request.get_json()).data
|
2016-02-19 15:53:45 +00:00
|
|
|
dao_create_service(valid_service, user)
|
|
|
|
|
return jsonify(data=service_schema.dump(valid_service).data), 201
|
2016-01-08 17:51:46 +00:00
|
|
|
|
2016-02-19 15:53:45 +00:00
|
|
|
|
2016-03-11 12:39:55 +00:00
|
|
|
@service.route('/<uuid:service_id>', methods=['POST'])
|
2016-01-08 17:51:46 +00:00
|
|
|
def update_service(service_id):
|
2016-02-19 15:53:45 +00:00
|
|
|
fetched_service = dao_fetch_service_by_id(service_id)
|
|
|
|
|
current_data = dict(service_schema.dump(fetched_service).data.items())
|
|
|
|
|
current_data.update(request.get_json())
|
2016-06-14 15:07:23 +01:00
|
|
|
update_dict = service_schema.load(current_data).data
|
2016-02-19 15:53:45 +00:00
|
|
|
dao_update_service(update_dict)
|
|
|
|
|
return jsonify(data=service_schema.dump(fetched_service).data), 200
|
2016-01-13 11:04:13 +00:00
|
|
|
|
|
|
|
|
|
2016-03-11 12:39:55 +00:00
|
|
|
@service.route('/<uuid:service_id>/api-key', methods=['POST'])
|
2016-06-23 16:44:57 +01:00
|
|
|
def create_api_key(service_id=None):
|
2016-02-19 15:53:45 +00:00
|
|
|
fetched_service = dao_fetch_service_by_id(service_id=service_id)
|
2016-06-14 15:07:23 +01:00
|
|
|
valid_api_key = api_key_schema.load(request.get_json()).data
|
2016-04-20 17:25:20 +01:00
|
|
|
valid_api_key.service = fetched_service
|
|
|
|
|
save_model_api_key(valid_api_key)
|
|
|
|
|
unsigned_api_key = get_unsigned_secret(valid_api_key.id)
|
2016-01-19 12:07:00 +00:00
|
|
|
return jsonify(data=unsigned_api_key), 201
|
2016-01-13 14:05:49 +00:00
|
|
|
|
|
|
|
|
|
2016-04-08 13:34:46 +01:00
|
|
|
@service.route('/<uuid:service_id>/api-key/revoke/<uuid:api_key_id>', methods=['POST'])
|
2016-01-20 14:48:44 +00:00
|
|
|
def revoke_api_key(service_id, api_key_id):
|
2016-06-22 15:27:28 +01:00
|
|
|
expire_api_key(service_id=service_id, api_key_id=api_key_id)
|
2016-01-14 11:30:45 +00:00
|
|
|
return jsonify(), 202
|
2016-01-13 09:25:46 +00:00
|
|
|
|
|
|
|
|
|
2016-03-11 12:39:55 +00:00
|
|
|
@service.route('/<uuid:service_id>/api-keys', methods=['GET'])
|
2016-04-08 13:34:46 +01:00
|
|
|
@service.route('/<uuid:service_id>/api-keys/<uuid:key_id>', methods=['GET'])
|
2016-01-21 12:13:17 +00:00
|
|
|
def get_api_keys(service_id, key_id=None):
|
2016-03-11 15:34:20 +00:00
|
|
|
dao_fetch_service_by_id(service_id=service_id)
|
|
|
|
|
|
2016-01-20 15:23:32 +00:00
|
|
|
try:
|
2016-01-21 12:13:17 +00:00
|
|
|
if key_id:
|
|
|
|
|
api_keys = [get_model_api_keys(service_id=service_id, id=key_id)]
|
|
|
|
|
else:
|
|
|
|
|
api_keys = get_model_api_keys(service_id=service_id)
|
|
|
|
|
except NoResultFound:
|
2016-06-14 15:07:23 +01:00
|
|
|
error = "API key not found for id: {}".format(service_id)
|
|
|
|
|
raise InvalidRequest(error, status_code=404)
|
2016-01-20 15:23:32 +00:00
|
|
|
|
2016-02-26 12:00:16 +00:00
|
|
|
return jsonify(apiKeys=api_key_schema.dump(api_keys, many=True).data), 200
|
2016-02-23 17:52:55 +00:00
|
|
|
|
|
|
|
|
|
2016-03-11 12:39:55 +00:00
|
|
|
@service.route('/<uuid:service_id>/users', methods=['GET'])
|
2016-02-23 17:52:55 +00:00
|
|
|
def get_users_for_service(service_id):
|
|
|
|
|
fetched = dao_fetch_service_by_id(service_id)
|
2016-02-26 12:00:16 +00:00
|
|
|
result = user_schema.dump(fetched.users, many=True)
|
2016-02-23 17:52:55 +00:00
|
|
|
return jsonify(data=result.data)
|
2016-02-25 12:11:51 +00:00
|
|
|
|
|
|
|
|
|
2016-03-11 12:39:55 +00:00
|
|
|
@service.route('/<uuid:service_id>/users/<user_id>', methods=['POST'])
|
2016-02-29 17:38:02 +00:00
|
|
|
def add_user_to_service(service_id, user_id):
|
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
|
|
|
|
user = get_model_users(user_id=user_id)
|
|
|
|
|
|
|
|
|
|
if user in service.users:
|
2016-06-14 15:07:23 +01:00
|
|
|
error = 'User id: {} already part of service id: {}'.format(user_id, service_id)
|
|
|
|
|
raise InvalidRequest(error, status_code=400)
|
2016-02-29 17:38:02 +00:00
|
|
|
|
2016-06-14 15:07:23 +01:00
|
|
|
permissions = permission_schema.load(request.get_json(), many=True).data
|
2016-06-06 12:37:06 +01:00
|
|
|
dao_add_user_to_service(service, user, permissions)
|
2016-06-14 15:07:23 +01:00
|
|
|
data = service_schema.dump(service).data
|
2016-02-29 17:38:02 +00:00
|
|
|
return jsonify(data=data), 201
|
|
|
|
|
|
|
|
|
|
|
2016-03-22 13:14:23 +00:00
|
|
|
@service.route('/<uuid:service_id>/users/<user_id>', methods=['DELETE'])
|
|
|
|
|
def remove_user_from_service(service_id, user_id):
|
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
|
|
|
|
user = get_model_users(user_id=user_id)
|
|
|
|
|
if user not in service.users:
|
2016-06-14 15:07:23 +01:00
|
|
|
error = 'User not found'
|
|
|
|
|
raise InvalidRequest(error, status_code=404)
|
|
|
|
|
|
2016-03-22 13:14:23 +00:00
|
|
|
elif len(service.users) == 1:
|
2016-06-14 15:07:23 +01:00
|
|
|
error = 'You cannot remove the only user for a service'
|
|
|
|
|
raise InvalidRequest(error, status_code=400)
|
|
|
|
|
|
2016-03-22 13:14:23 +00:00
|
|
|
dao_remove_user_from_service(service, user)
|
|
|
|
|
return jsonify({}), 204
|
|
|
|
|
|
|
|
|
|
|
2016-04-28 12:01:27 +01:00
|
|
|
@service.route('/<uuid:service_id>/fragment/aggregate_statistics')
|
|
|
|
|
def get_service_provider_aggregate_statistics(service_id):
|
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
2016-06-14 15:07:23 +01:00
|
|
|
data = from_to_date_schema.load(request.args).data
|
2016-04-28 12:01:27 +01:00
|
|
|
return jsonify(data=get_fragment_count(
|
|
|
|
|
service,
|
|
|
|
|
date_from=(data.pop('date_from') if 'date_from' in data else date.today()),
|
|
|
|
|
date_to=(data.pop('date_to') if 'date_to' in data else date.today())
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
|
2016-04-21 16:32:20 +01:00
|
|
|
# This is placeholder get method until more thought
|
|
|
|
|
# goes into how we want to fetch and view various items in history
|
|
|
|
|
# tables. This is so product owner can pass stories as done
|
|
|
|
|
@service.route('/<uuid:service_id>/history', methods=['GET'])
|
2016-04-25 10:38:37 +01:00
|
|
|
def get_service_history(service_id):
|
2016-04-27 17:30:15 +01:00
|
|
|
from app.models import (Service, ApiKey, Template, Event)
|
2016-04-25 10:38:37 +01:00
|
|
|
from app.schemas import (
|
|
|
|
|
service_history_schema,
|
|
|
|
|
api_key_history_schema,
|
2016-04-27 17:30:15 +01:00
|
|
|
template_history_schema,
|
|
|
|
|
event_schema
|
2016-04-25 10:38:37 +01:00
|
|
|
)
|
2016-04-21 16:32:20 +01:00
|
|
|
|
|
|
|
|
service_history = Service.get_history_model().query.filter_by(id=service_id).all()
|
2016-06-14 15:07:23 +01:00
|
|
|
service_data = service_history_schema.dump(service_history, many=True).data
|
2016-04-21 16:32:20 +01:00
|
|
|
api_key_history = ApiKey.get_history_model().query.filter_by(service_id=service_id).all()
|
2016-06-14 15:07:23 +01:00
|
|
|
api_keys_data = api_key_history_schema.dump(api_key_history, many=True).data
|
2016-04-21 16:32:20 +01:00
|
|
|
|
2016-04-25 10:38:37 +01:00
|
|
|
template_history = Template.get_history_model().query.filter_by(service_id=service_id).all()
|
|
|
|
|
template_data, errors = template_history_schema.dump(template_history, many=True)
|
|
|
|
|
|
2016-04-27 17:30:15 +01:00
|
|
|
events = Event.query.all()
|
2016-06-14 15:07:23 +01:00
|
|
|
events_data = event_schema.dump(events, many=True).data
|
2016-04-27 17:30:15 +01:00
|
|
|
|
2016-04-25 10:38:37 +01:00
|
|
|
data = {
|
|
|
|
|
'service_history': service_data,
|
|
|
|
|
'api_key_history': api_keys_data,
|
2016-04-27 17:30:15 +01:00
|
|
|
'template_history': template_data,
|
|
|
|
|
'events': events_data}
|
2016-04-21 16:32:20 +01:00
|
|
|
|
|
|
|
|
return jsonify(data=data)
|
2016-06-28 15:17:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@service.route('/<uuid:service_id>/notifications', methods=['GET'])
|
|
|
|
|
def get_all_notifications_for_service(service_id):
|
|
|
|
|
data = notifications_filter_schema.load(request.args).data
|
|
|
|
|
page = data['page'] if 'page' in data else 1
|
|
|
|
|
page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
|
|
|
|
|
limit_days = data.get('limit_days')
|
|
|
|
|
|
|
|
|
|
pagination = notifications_dao.get_notifications_for_service(
|
|
|
|
|
service_id,
|
|
|
|
|
filter_dict=data,
|
|
|
|
|
page=page,
|
|
|
|
|
page_size=page_size,
|
|
|
|
|
limit_days=limit_days)
|
|
|
|
|
kwargs = request.args.to_dict()
|
|
|
|
|
kwargs['service_id'] = service_id
|
|
|
|
|
return jsonify(
|
|
|
|
|
notifications=notification_status_schema.dump(pagination.items, many=True).data,
|
|
|
|
|
page_size=page_size,
|
|
|
|
|
total=pagination.total,
|
|
|
|
|
links=pagination_links(
|
|
|
|
|
pagination,
|
|
|
|
|
'.get_all_notifications_for_service',
|
|
|
|
|
**kwargs
|
|
|
|
|
)
|
|
|
|
|
), 200
|
2016-07-18 12:03:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_detailed_service(service_id):
|
|
|
|
|
service = dao_fetch_service_by_id(service_id)
|
|
|
|
|
statistics = dao_fetch_stats_for_service(service_id)
|
|
|
|
|
service.statistics = format_statistics(statistics)
|
|
|
|
|
data = detailed_service_schema.dump(service).data
|
|
|
|
|
return jsonify(data=data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_statistics(statistics):
|
|
|
|
|
# statistics come in a named tuple with uniqueness from 'notification_type', 'status' - however missing
|
|
|
|
|
# statuses/notification types won't be represented and the status types need to be simplified/summed up
|
|
|
|
|
# so we can return emails/sms * created, sent, and failed
|
|
|
|
|
counts = {
|
|
|
|
|
template_type: {
|
|
|
|
|
status: 0 for status in ('requested', 'delivered', 'failed')
|
|
|
|
|
} for template_type in (EMAIL_TYPE, SMS_TYPE)
|
|
|
|
|
}
|
|
|
|
|
for row in statistics:
|
|
|
|
|
counts[row.notification_type]['requested'] += row.count
|
|
|
|
|
if row.status == 'delivered':
|
|
|
|
|
counts[row.notification_type]['delivered'] += row.count
|
|
|
|
|
elif row.status in ('failed', 'technical-failure', 'temporary-failure', 'permanent-failure'):
|
|
|
|
|
counts[row.notification_type]['failed'] += row.count
|
|
|
|
|
|
|
|
|
|
return counts
|