From 6e7383de33591b55651bf6a36264034abe4c8b8c Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Wed, 18 May 2016 10:00:09 +0100 Subject: [PATCH] Removed template subject uniqueness --- app/models.py | 2 +- app/template/rest.py | 11 +---- .../0018_remove_subject_uniqueness.py | 26 +++++++++++ tests/app/template/test_rest.py | 43 ------------------- 4 files changed, 28 insertions(+), 54 deletions(-) create mode 100644 migrations/versions/0018_remove_subject_uniqueness.py diff --git a/app/models.py b/app/models.py index 569125374..33a525b7c 100644 --- a/app/models.py +++ b/app/models.py @@ -178,7 +178,7 @@ class Template(db.Model, Versioned): archived = db.Column(db.Boolean, index=False, nullable=False, default=False) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=False) service = db.relationship('Service', backref=db.backref('templates', lazy='dynamic')) - subject = db.Column(db.Text, index=False, unique=True, nullable=True) + subject = db.Column(db.Text, index=False, unique=False, nullable=True) created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=False) created_by = db.relationship('User') diff --git a/app/template/rest.py b/app/template/rest.py index 8ec5e85de..752417e0f 100644 --- a/app/template/rest.py +++ b/app/template/rest.py @@ -50,16 +50,7 @@ def create_template(service_id): new_template.template_type) if over_limit: return json_resp, 400 - try: - dao_create_template(new_template) - except IntegrityError as ex: - current_app.logger.debug(ex) - message = "Failed to create template" - if "templates_subject_key" in str(ex): - message = 'Duplicate template subject' - return jsonify(result="error", message={'subject': [message]}), 400 - return jsonify(result="error", message=message), 500 - + dao_create_template(new_template) return jsonify(data=template_schema.dump(new_template).data), 201 diff --git a/migrations/versions/0018_remove_subject_uniqueness.py b/migrations/versions/0018_remove_subject_uniqueness.py new file mode 100644 index 000000000..53875d514 --- /dev/null +++ b/migrations/versions/0018_remove_subject_uniqueness.py @@ -0,0 +1,26 @@ +"""empty message + +Revision ID: 0018_remove_subject_uniqueness +Revises: 0017_add_failure_types +Create Date: 2016-05-18 09:39:22.512042 + +""" + +# revision identifiers, used by Alembic. +revision = '0018_remove_subject_uniqueness' +down_revision = '0017_add_failure_types' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint('templates_subject_key', 'templates', type_='unique') + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.create_unique_constraint('templates_subject_key', 'templates', ['subject']) + ### end Alembic commands ### diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index cd6389b10..5495277af 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -155,49 +155,6 @@ def test_must_have_a_subject_on_an_email_template(notify_api, sample_user, sampl assert json_resp['message'] == {'subject': ['Invalid template subject']} -def test_must_have_a_uniqe_subject_on_an_email_template(notify_api, sample_user, sample_service): - with notify_api.test_request_context(): - with notify_api.test_client() as client: - data = { - 'name': 'my template', - 'template_type': 'email', - 'subject': 'subject', - 'content': 'template content', - 'service': str(sample_service.id), - 'created_by': str(sample_user.id) - } - data = json.dumps(data) - auth_header = create_authorization_header() - - response = client.post( - '/service/{}/template'.format(sample_service.id), - headers=[('Content-Type', 'application/json'), auth_header], - data=data - ) - assert response.status_code == 201 - - data = { - 'name': 'my template', - 'template_type': 'email', - 'subject': 'subject', - 'content': 'template content', - 'service': str(sample_service.id), - 'created_by': str(sample_user.id) - } - data = json.dumps(data) - auth_header = create_authorization_header() - - response = client.post( - '/service/{}/template'.format(sample_service.id), - headers=[('Content-Type', 'application/json'), auth_header], - data=data - ) - assert response.status_code == 400 - json_resp = json.loads(response.get_data(as_text=True)) - assert json_resp['result'] == 'error' - assert json_resp['message']['subject'][0] == 'Duplicate template subject' - - def test_should_be_able_to_update_a_template(notify_api, sample_user, sample_service): with notify_api.test_request_context(): with notify_api.test_client() as client: