Merge pull request #749 from alphagov/pc-nab-numerous-notifications

Remove marshmallow schema validation + format error messages
This commit is contained in:
Paul Craig
2016-11-28 17:20:27 +00:00
committed by GitHub
6 changed files with 190 additions and 10 deletions

View File

@@ -8,7 +8,7 @@ from itertools import groupby
from flask import current_app
from werkzeug.datastructures import MultiDict
from sqlalchemy import (desc, func, or_, and_, asc, cast, Text)
from sqlalchemy import (desc, func, or_, and_, asc)
from sqlalchemy.orm import joinedload
from app import db, create_uuid

View File

@@ -42,6 +42,4 @@ def build_error_message(errors):
def __format_message(e):
s = e.message.split("'")
msg = "{}{}".format(s[1], s[2])
return msg if not e.cause else "{} {}".format(e.path[0], e.cause.message)
return e.message.replace("'", "") if not e.cause else "{} {}".format(e.path[0], e.cause.message)

View File

@@ -1,9 +1,9 @@
from flask import jsonify, request, url_for
from app import api_user
from app.dao import notifications_dao
from app.schemas import notifications_filter_schema
from app.schema_validation import validate
from app.v2.notifications import notification_blueprint
from app.v2.notifications.notification_schemas import get_notifications_request
@notification_blueprint.route("/<uuid:id>", methods=['GET'])
@@ -17,7 +17,12 @@ def get_notification_by_id(id):
@notification_blueprint.route("", methods=['GET'])
def get_notifications():
data = notifications_filter_schema.load(request.args).data
_data = request.args.to_dict(flat=False)
if 'older_than' in _data:
# flat=False makes everything a list, but we only ever allow one value for "older_than"
_data['older_than'] = _data['older_than'][0]
data = validate(_data, get_notifications_request)
paginated_notifications = notifications_dao.get_notifications_for_service(
str(api_user.service_id),
@@ -29,11 +34,11 @@ def get_notifications():
def _build_links(notifications):
_links = {
'current': url_for(".get_notifications", _external=True, **request.args.to_dict(flat=False)),
'current': url_for(".get_notifications", _external=True, **data),
}
if len(notifications):
next_query_params = dict(request.args.to_dict(flat=False), older_than=notifications[-1].id)
next_query_params = dict(data, older_than=notifications[-1].id)
_links['next'] = url_for(".get_notifications", _external=True, **next_query_params)
return _links

View File

@@ -1,3 +1,4 @@
from app.models import NOTIFICATION_STATUS_TYPES, TEMPLATE_TYPES
from app.schema_validation.definitions import (uuid, personalisation)
# this may belong in a templates module
@@ -72,6 +73,28 @@ get_notification_response = {
]
}
get_notifications_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "schema for query parameters allowed when getting list of notifications",
"type": "object",
"properties": {
"status": {
"type": "array",
"items": {
"enum": NOTIFICATION_STATUS_TYPES
}
},
"template_type": {
"type": "array",
"items": {
"enum": TEMPLATE_TYPES
}
},
"older_than": uuid
},
"additionalProperties": False,
}
get_notifications_response = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "GET list of notifications response schema",