diff --git a/app/models.py b/app/models.py index bd4a51668..ab8978875 100644 --- a/app/models.py +++ b/app/models.py @@ -179,6 +179,26 @@ class ServicePermissionTypes(db.Model): name = db.Column(db.String(255), primary_key=True) +class AnnualBilling(db.Model): + __tablename__ = "annual_billing" + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=False) + service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), unique=False, index=True, nullable=False) + financial_year_start = db.Column(db.Integer, nullable=False, default=True, unique=False) + free_sms_fragment_limit = db.Column(db.Integer, nullable=False, index=False, unique=False) + updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow) + created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) + + def serialize(self): + return { + 'id': str(self.id), + 'service_id': str(self.service_id), + 'free_sms_fragment_limit': str(self.free_sms_fragment_limit), + 'financial_year_start': str(self.financial_year_start), + 'created_at': self.created_at.strftime(DATETIME_FORMAT), + 'updated_at': self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None + } + + class Service(db.Model, Versioned): __tablename__ = 'services' @@ -213,6 +233,7 @@ class Service(db.Model, Versioned): organisation_id = db.Column(UUID(as_uuid=True), db.ForeignKey('organisation.id'), index=True, nullable=True) free_sms_fragment_limit = db.Column(db.BigInteger, index=False, unique=False, nullable=True) organisation = db.relationship('Organisation') + annual_billing = db.relationship('AnnualBilling') dvla_organisation_id = db.Column( db.String, db.ForeignKey('dvla_organisation.id'), @@ -1462,23 +1483,3 @@ class NotificationEmailReplyTo(db.Model): nullable=False, primary_key=True ) - - -class AnnualBilling(db.Model): - __tablename__ = "annual_billing" - 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) - financial_year_start = db.Column(db.Integer, nullable=False, default=True) - free_sms_fragment_limit = db.Column(db.Integer, nullable=False, index=False, unique=False) - updated_at = db.Column(db.DateTime, nullable=True, onupdate=datetime.datetime.utcnow) - created_at = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) - - def serialize(self): - return { - 'id': str(self.id), - 'service_id': str(self.service_id), - 'free_sms_fragment_limit': str(self.free_sms_fragment_limit), - 'financial_year_start': str(self.financial_year_start), - 'created_at': self.created_at.strftime(DATETIME_FORMAT), - 'updated_at': self.updated_at.strftime(DATETIME_FORMAT) if self.updated_at else None - } diff --git a/migrations/versions/0126_add_annual_billing.py b/migrations/versions/0126_add_annual_billing.py index 7f6dfe5b2..1a20a1c0b 100644 --- a/migrations/versions/0126_add_annual_billing.py +++ b/migrations/versions/0126_add_annual_billing.py @@ -2,7 +2,7 @@ Revision ID: 0126_add_annual_billing Revises: 0125_add_organisation_type -Create Date: 2017-10-18 11:42:54.261575 +Create Date: 2017-10-19 11:38:32.849573 """ from alembic import op @@ -19,11 +19,15 @@ def upgrade(): sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('financial_year_start', sa.Integer(), nullable=False), sa.Column('free_sms_fragment_limit', sa.Integer(), nullable=False), - sa.Column('created_at', sa.DateTime(), nullable=True), sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=False), sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.PrimaryKeyConstraint('id')) + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_annual_billing_service_id'), 'annual_billing', ['service_id'], unique=False) def downgrade(): + op.drop_index(op.f('ix_annual_billing_service_id'), table_name='annual_billing') op.drop_table('annual_billing') +