mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 09:51:11 -05:00
Add ServiceLetterContact data model and script
This commit is contained in:
@@ -257,6 +257,14 @@ class Service(db.Model, Versioned):
|
|||||||
else:
|
else:
|
||||||
return default_reply_to[0].email_address if default_reply_to else None
|
return default_reply_to[0].email_address if default_reply_to else None
|
||||||
|
|
||||||
|
def get_default_letter_contact(self):
|
||||||
|
default_letter_contact = [x for x in self.letter_contacts if x.is_default]
|
||||||
|
if len(default_letter_contact) > 1:
|
||||||
|
raise Exception("There should only ever be one default")
|
||||||
|
else:
|
||||||
|
return default_letter_contact[0].contact_block if default_letter_contact else \
|
||||||
|
self.letter_contact_block # need to update this to None after dropping the letter_contact_block column
|
||||||
|
|
||||||
|
|
||||||
class InboundNumber(db.Model):
|
class InboundNumber(db.Model):
|
||||||
__tablename__ = "inbound_numbers"
|
__tablename__ = "inbound_numbers"
|
||||||
@@ -1381,3 +1389,25 @@ class ServiceEmailReplyTo(db.Model):
|
|||||||
'created_at': self.created_at.strftime(DATETIME_FORMAT),
|
'created_at': self.created_at.strftime(DATETIME_FORMAT),
|
||||||
'updated_at': self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None
|
'updated_at': self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class ServiceLetterContact(db.Model):
|
||||||
|
__tablename__ = "service_letter_contacts"
|
||||||
|
|
||||||
|
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||||
|
|
||||||
|
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), unique=False, index=True, nullable=False)
|
||||||
|
service = db.relationship(Service, backref=db.backref("letter_contacts"))
|
||||||
|
|
||||||
|
contact_block = db.Column(db.Text, nullable=False, index=False, unique=False)
|
||||||
|
is_default = db.Column(db.Boolean, nullable=False, default=True)
|
||||||
|
created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow)
|
||||||
|
updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow)
|
||||||
|
|
||||||
|
def serialize(self):
|
||||||
|
return {
|
||||||
|
'contact_block': self.contact_block,
|
||||||
|
'is_default': self.is_default,
|
||||||
|
'created_at': self.created_at.strftime(DATETIME_FORMAT),
|
||||||
|
'updated_at': self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None
|
||||||
|
}
|
||||||
|
|||||||
32
migrations/versions/0122_add_service_letter_contact.py
Normal file
32
migrations/versions/0122_add_service_letter_contact.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
"""
|
||||||
|
|
||||||
|
Revision ID: 0122_add_service_letter_contact
|
||||||
|
Revises: 0121_nullable_logos
|
||||||
|
Create Date: 2017-09-21 12:16:02.975120
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
revision = '0122_add_service_letter_contact'
|
||||||
|
down_revision = '0121_nullable_logos'
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table('service_letter_contacts',
|
||||||
|
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||||
|
sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||||
|
sa.Column('contact_block', sa.Text(), nullable=False),
|
||||||
|
sa.Column('is_default', sa.Boolean(), nullable=False),
|
||||||
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||||
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['service_id'], ['services.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_service_letter_contact_service_id'), 'service_letter_contacts', ['service_id'], unique=False)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_index(op.f('ix_service_letter_contact_service_id'), table_name='service_letter_contacts')
|
||||||
|
op.drop_table('service_letter_contacts')
|
||||||
@@ -26,7 +26,8 @@ from app.models import (
|
|||||||
INBOUND_SMS_TYPE,
|
INBOUND_SMS_TYPE,
|
||||||
KEY_TYPE_NORMAL,
|
KEY_TYPE_NORMAL,
|
||||||
ServiceInboundApi,
|
ServiceInboundApi,
|
||||||
ServiceEmailReplyTo
|
ServiceEmailReplyTo,
|
||||||
|
ServiceLetterContact
|
||||||
)
|
)
|
||||||
from app.dao.users_dao import save_model_user
|
from app.dao.users_dao import save_model_user
|
||||||
from app.dao.notifications_dao import dao_create_notification, dao_created_scheduled_notification
|
from app.dao.notifications_dao import dao_create_notification, dao_created_scheduled_notification
|
||||||
@@ -347,3 +348,21 @@ def create_reply_to_email(
|
|||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return reply_to
|
return reply_to
|
||||||
|
|
||||||
|
|
||||||
|
def create_letter_contact(
|
||||||
|
service,
|
||||||
|
contact_block,
|
||||||
|
is_default=True
|
||||||
|
):
|
||||||
|
data = {
|
||||||
|
'service': service,
|
||||||
|
'contact_block': contact_block,
|
||||||
|
'is_default': is_default,
|
||||||
|
}
|
||||||
|
letter_content = ServiceLetterContact(**data)
|
||||||
|
|
||||||
|
db.session.add(letter_content)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return letter_content
|
||||||
|
|||||||
@@ -21,7 +21,13 @@ from tests.app.conftest import (
|
|||||||
sample_template as create_sample_template,
|
sample_template as create_sample_template,
|
||||||
sample_notification_with_job as create_sample_notification_with_job
|
sample_notification_with_job as create_sample_notification_with_job
|
||||||
)
|
)
|
||||||
from tests.app.db import create_notification, create_service, create_inbound_number, create_reply_to_email
|
from tests.app.db import (
|
||||||
|
create_notification,
|
||||||
|
create_service,
|
||||||
|
create_inbound_number,
|
||||||
|
create_reply_to_email,
|
||||||
|
create_letter_contact
|
||||||
|
)
|
||||||
from tests.conftest import set_config
|
from tests.conftest import set_config
|
||||||
|
|
||||||
|
|
||||||
@@ -262,3 +268,14 @@ def test_service_get_default_reply_to_email_address(sample_service):
|
|||||||
create_reply_to_email(service=sample_service, email_address="default@email.com")
|
create_reply_to_email(service=sample_service, email_address="default@email.com")
|
||||||
|
|
||||||
assert sample_service.get_default_reply_to_email_address() == 'default@email.com'
|
assert sample_service.get_default_reply_to_email_address() == 'default@email.com'
|
||||||
|
|
||||||
|
|
||||||
|
def test_service_get_default_contact_letter(sample_service):
|
||||||
|
create_letter_contact(service=sample_service, contact_block='London,\nNW1A 1AA')
|
||||||
|
|
||||||
|
assert sample_service.get_default_letter_contact() == 'London,\nNW1A 1AA'
|
||||||
|
|
||||||
|
|
||||||
|
# this test will need to be removed after letter_contact_block is dropped
|
||||||
|
def test_service_get_default_letter_contact_block_from_service(sample_service):
|
||||||
|
assert sample_service.get_default_letter_contact() == sample_service.letter_contact_block
|
||||||
|
|||||||
Reference in New Issue
Block a user