-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #736 from alphagov/implement-v2-send-email
Implemented the post email notifications endpoint for v2
- Loading branch information
Showing
9 changed files
with
313 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,45 @@ | ||
import json | ||
|
||
from jsonschema import Draft4Validator, ValidationError | ||
from jsonschema import (Draft4Validator, ValidationError, FormatChecker) | ||
from notifications_utils.recipients import (validate_phone_number, validate_email_address, InvalidPhoneError, | ||
InvalidEmailError) | ||
|
||
|
||
def validate(json_to_validate, schema): | ||
validator = Draft4Validator(schema) | ||
format_checker = FormatChecker() | ||
|
||
@format_checker.checks('phone_number', raises=InvalidPhoneError) | ||
def validate_schema_phone_number(instance): | ||
validate_phone_number(instance) | ||
return True | ||
|
||
@format_checker.checks('email_address', raises=InvalidEmailError) | ||
def validate_schema_email_address(instance): | ||
validate_email_address(instance) | ||
return True | ||
|
||
validator = Draft4Validator(schema, format_checker=format_checker) | ||
errors = list(validator.iter_errors(json_to_validate)) | ||
if errors.__len__() > 0: | ||
raise ValidationError(build_error_message(errors, schema)) | ||
raise ValidationError(build_error_message(errors)) | ||
return json_to_validate | ||
|
||
|
||
def build_error_message(errors, schema): | ||
def build_error_message(errors): | ||
fields = [] | ||
for e in errors: | ||
field = "'{}' {}".format(e.path[0], e.schema.get('validationMessage')) if e.schema.get( | ||
'validationMessage') else e.message | ||
s = field.split("'") | ||
field = {"error": "ValidationError", "message": "{}{}".format(s[1], s[2])} | ||
fields.append(field) | ||
field = "{} {}".format(e.path[0], e.schema.get('validationMessage')) if e.schema.get( | ||
'validationMessage') else __format_message(e) | ||
fields.append({"error": "ValidationError", "message": field}) | ||
message = { | ||
"status_code": 400, | ||
"errors": fields | ||
} | ||
|
||
return json.dumps(message) | ||
|
||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""empty message | ||
Revision ID: 0061_add_client_reference | ||
Revises: 0060_add_letter_template_type | ||
Create Date: 2016-11-17 13:19:25.820617 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '0061_add_client_reference' | ||
down_revision = '0060_add_letter_template_type' | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
def upgrade(): | ||
op.add_column('notifications', sa.Column('client_reference', sa.String(), index=True, nullable=True)) | ||
op.add_column('notification_history', sa.Column('client_reference', sa.String(), nullable=True)) | ||
|
||
|
||
def downgrade(): | ||
op.drop_column('notifications', 'client_reference') | ||
op.drop_column('notification_history', 'client_reference') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.