add created_by to notifications

this is so one-off notifications can be tied to a user
(jobs have a created_by, and api notifications don't make sense
 to have one)
This commit is contained in:
Leo Hemsted
2017-06-13 15:33:33 +01:00
parent 8a49afc06c
commit 9f307fd1c5
4 changed files with 50 additions and 0 deletions

View File

@@ -800,6 +800,9 @@ class Notification(db.Model):
phone_prefix = db.Column(db.String, nullable=True)
rate_multiplier = db.Column(db.Float(asdecimal=False), nullable=True)
created_by = db.relationship('User')
created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), nullable=True)
@hybrid_property
def status(self):
return self._status_enum

View File

@@ -444,6 +444,7 @@ class NotificationWithTemplateSchema(BaseSchema):
dump_only=True
)
job = fields.Nested(JobSchema, only=["id", "original_file_name"], dump_only=True)
created_by = fields.Nested(UserSchema, only=['id', 'name', 'email_address'], dump_only=True)
status = fields.String(required=False)
personalisation = fields.Dict(required=False)
key_type = field_for(models.Notification, 'key_type', required=True)

View File

@@ -0,0 +1,27 @@
"""add created_by col to notification
Revision ID: 0100_notification_created_by
Revises: 0099_tfl_dar
Create Date: 2017-06-13 10:53:25.032202
"""
# revision identifiers, used by Alembic.
revision = '0100_notification_created_by'
down_revision = '0099_tfl_dar'
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
def upgrade():
op.add_column('notifications', sa.Column('created_by_id', postgresql.UUID(as_uuid=True), nullable=True))
op.create_foreign_key(None, 'notifications', 'users', ['created_by_id'], ['id'])
op.add_column('notification_history', sa.Column('created_by_id', postgresql.UUID(as_uuid=True), nullable=True))
op.create_foreign_key(None, 'notification_history', 'users', ['created_by_id'], ['id'])
def downgrade():
op.drop_column('notifications', 'created_by_id')
op.drop_column('notification_history', 'created_by_id')

View File

@@ -1316,6 +1316,25 @@ def test_get_notification_for_service(client, notify_db, notify_db_session):
assert service_2_response == {'message': 'No result found', 'result': 'error'}
def test_get_notification_for_service_includes_created_by(admin_request, sample_notification):
user = sample_notification.created_by = sample_notification.service.created_by
resp = admin_request.get(
'service.get_notification_for_service',
endpoint_kwargs={
'service_id': sample_notification.service_id,
'notification_id': sample_notification.id
}
)
assert resp['id'] == str(sample_notification.id)
assert resp['created_by'] == {
'id': str(user.id),
'name': user.name,
'email_address': user.email_address
}
@pytest.mark.parametrize(
'include_from_test_key, expected_count_of_notifications',
[