mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 23:55:58 -05:00
add new provider_details_history table
* to be used for auditing changes to provider details * not hooked in yet * also made provider_details.active non-nullable. set all existing null provider_details.active to FALSE. none are set false on live so should hopefully be fairly uncontroversial * refactored NotificationHistory.from_notification and update from noti to share with provider_details_history
This commit is contained in:
@@ -130,7 +130,7 @@ def dao_create_notification(notification):
|
||||
if not notification.status:
|
||||
notification.status = 'created'
|
||||
|
||||
notification_history = NotificationHistory.from_notification(notification)
|
||||
notification_history = NotificationHistory.from_original(notification)
|
||||
db.session.add(notification)
|
||||
db.session.add(notification_history)
|
||||
|
||||
@@ -190,7 +190,7 @@ def update_notification_status_by_reference(reference, status):
|
||||
def dao_update_notification(notification):
|
||||
notification.updated_at = datetime.utcnow()
|
||||
notification_history = NotificationHistory.query.get(notification.id)
|
||||
notification_history.update_from_notification(notification)
|
||||
notification_history.update_from_original(notification)
|
||||
db.session.add(notification)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@@ -35,6 +35,18 @@ def filter_null_value_fields(obj):
|
||||
)
|
||||
|
||||
|
||||
class HistoryModel:
|
||||
@classmethod
|
||||
def from_original(cls, original):
|
||||
history = cls()
|
||||
history.update_from_original(original)
|
||||
return history
|
||||
|
||||
def update_from_original(self, original):
|
||||
for c in self.__table__.columns:
|
||||
setattr(self, c.name, getattr(original, c.name))
|
||||
|
||||
|
||||
class User(db.Model):
|
||||
__tablename__ = 'users'
|
||||
|
||||
@@ -354,7 +366,18 @@ class ProviderDetails(db.Model):
|
||||
identifier = db.Column(db.String, nullable=False)
|
||||
priority = db.Column(db.Integer, nullable=False)
|
||||
notification_type = db.Column(notification_types, nullable=False)
|
||||
active = db.Column(db.Boolean, default=False)
|
||||
active = db.Column(db.Boolean, default=False, nullable=False)
|
||||
|
||||
|
||||
class ProviderDetailsHistory(db.Model, HistoryModel):
|
||||
__tablename__ = 'provider_details_history'
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True)
|
||||
display_name = db.Column(db.String, nullable=False)
|
||||
identifier = db.Column(db.String, nullable=False)
|
||||
priority = db.Column(db.Integer, nullable=False)
|
||||
notification_type = db.Column(notification_types, nullable=False)
|
||||
active = db.Column(db.Boolean, nullable=False)
|
||||
|
||||
|
||||
JOB_STATUS_PENDING = 'pending'
|
||||
@@ -654,7 +677,7 @@ class Notification(db.Model):
|
||||
return serialized
|
||||
|
||||
|
||||
class NotificationHistory(db.Model):
|
||||
class NotificationHistory(db.Model, HistoryModel):
|
||||
__tablename__ = 'notification_history'
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True)
|
||||
@@ -680,14 +703,10 @@ class NotificationHistory(db.Model):
|
||||
client_reference = db.Column(db.String, nullable=True)
|
||||
|
||||
@classmethod
|
||||
def from_notification(cls, notification):
|
||||
history = cls(**{c.name: getattr(notification, c.name) for c in cls.__table__.columns})
|
||||
def from_original(cls, notification):
|
||||
history = super().from_original(notification)
|
||||
return history
|
||||
|
||||
def update_from_notification(self, notification):
|
||||
for c in self.__table__.columns:
|
||||
setattr(self, c.name, getattr(notification, c.name))
|
||||
|
||||
|
||||
INVITED_USER_STATUS_TYPES = ['pending', 'accepted', 'cancelled']
|
||||
|
||||
|
||||
38
migrations/versions/0062_provider_details_history.py
Normal file
38
migrations/versions/0062_provider_details_history.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""empty message
|
||||
|
||||
Revision ID: 0062_provider_details_history
|
||||
Revises: 0061_add_client_reference
|
||||
Create Date: 2016-12-14 13:00:24.226990
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '0062_provider_details_history'
|
||||
down_revision = '0061_add_client_reference'
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import postgresql
|
||||
|
||||
def upgrade():
|
||||
op.get_bind()
|
||||
op.execute('UPDATE provider_details SET active = false WHERE active is null')
|
||||
|
||||
op.alter_column('provider_details', 'active', existing_type=sa.BOOLEAN(), nullable=False)
|
||||
|
||||
op.create_table('provider_details_history',
|
||||
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||
sa.Column('display_name', sa.String(), nullable=False),
|
||||
sa.Column('identifier', sa.String(), nullable=False),
|
||||
sa.Column('priority', sa.Integer(), nullable=False),
|
||||
sa.Column('notification_type', postgresql.ENUM('email', 'sms', 'letter', name='notification_type', create_type=False), nullable=False),
|
||||
sa.Column('active', sa.Boolean(), nullable=False),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_table('provider_details_history')
|
||||
|
||||
op.alter_column('provider_details_history', 'active', existing_type=sa.BOOLEAN(), nullable=True)
|
||||
op.alter_column('provider_details', 'active', existing_type=sa.BOOLEAN(), nullable=True)
|
||||
Reference in New Issue
Block a user