Removed template subject uniqueness

This commit is contained in:
Nicholas Staples
2016-05-18 10:00:09 +01:00
parent 9407273381
commit 6e7383de33
4 changed files with 28 additions and 54 deletions

View File

@@ -178,7 +178,7 @@ class Template(db.Model, Versioned):
archived = db.Column(db.Boolean, index=False, nullable=False, default=False) 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_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')) 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_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=False)
created_by = db.relationship('User') created_by = db.relationship('User')

View File

@@ -50,16 +50,7 @@ def create_template(service_id):
new_template.template_type) new_template.template_type)
if over_limit: if over_limit:
return json_resp, 400 return json_resp, 400
try: dao_create_template(new_template)
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
return jsonify(data=template_schema.dump(new_template).data), 201 return jsonify(data=template_schema.dump(new_template).data), 201

View File

@@ -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 ###

View File

@@ -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']} 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): 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_request_context():
with notify_api.test_client() as client: with notify_api.test_client() as client: