mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-23 15:56:45 -04:00
Merge pull request #1326 from alphagov/vb-free-sms-limit-history
[#152067121] Create new table annual_billing
This commit is contained in:
@@ -247,7 +247,7 @@ class PopulateServiceEmailReplyTo(Command):
|
|||||||
result = db.session.execute(services_to_update)
|
result = db.session.execute(services_to_update)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
print("Populated email reply to adderesses for {}".format(result.rowcount))
|
print("Populated email reply to addresses for {}".format(result.rowcount))
|
||||||
|
|
||||||
|
|
||||||
class PopulateServiceSmsSender(Command):
|
class PopulateServiceSmsSender(Command):
|
||||||
@@ -329,3 +329,25 @@ class PopulateServiceAndServiceHistoryFreeSmsFragmentLimit(Command):
|
|||||||
|
|
||||||
print("Populated free sms fragment limits for {} services".format(services_result.rowcount))
|
print("Populated free sms fragment limits for {} services".format(services_result.rowcount))
|
||||||
print("Populated free sms fragment limits for {} services history".format(services_history_result.rowcount))
|
print("Populated free sms fragment limits for {} services history".format(services_history_result.rowcount))
|
||||||
|
|
||||||
|
|
||||||
|
class PopulateAnnualBilling(Command):
|
||||||
|
def run(self):
|
||||||
|
financial_year = [2016, 2017, 2018]
|
||||||
|
|
||||||
|
for fy in financial_year:
|
||||||
|
populate_data = """
|
||||||
|
INSERT INTO annual_billing(id, service_id, free_sms_fragment_limit, financial_year_start,
|
||||||
|
created_at, updated_at)
|
||||||
|
SELECT uuid_in(md5(random()::text || now()::text)::cstring), id, 250000, {}, '{}', '{}'
|
||||||
|
FROM services
|
||||||
|
WHERE id NOT IN(
|
||||||
|
SELECT service_id
|
||||||
|
FROM annual_billing
|
||||||
|
WHERE financial_year_start={})
|
||||||
|
""".format(fy, datetime.utcnow(), datetime.utcnow(), fy)
|
||||||
|
|
||||||
|
services_result1 = db.session.execute(populate_data)
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
print("Populated annual billing {} for {} services".format(fy, services_result1.rowcount))
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ def get_financial_year(year):
|
|||||||
def get_april_fools(year):
|
def get_april_fools(year):
|
||||||
"""
|
"""
|
||||||
This function converts the start of the financial year April 1, 00:00 as BST (British Standard Time) to UTC,
|
This function converts the start of the financial year April 1, 00:00 as BST (British Standard Time) to UTC,
|
||||||
the tzinfo is lastly removed from the datetime becasue the database stores the timestamps without timezone.
|
the tzinfo is lastly removed from the datetime because the database stores the timestamps without timezone.
|
||||||
:param year: the year to calculate the April 1, 00:00 BST for
|
:param year: the year to calculate the April 1, 00:00 BST for
|
||||||
:return: the datetime of April 1 for the given year, for example 2016 = 2016-03-31 23:00:00
|
:return: the datetime of April 1 for the given year, for example 2016 = 2016-03-31 23:00:00
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -179,6 +179,27 @@ class ServicePermissionTypes(db.Model):
|
|||||||
name = db.Column(db.String(255), primary_key=True)
|
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)
|
||||||
|
UniqueConstraint('financial_year_start', 'service_id', name='ix_annual_billing_service_id')
|
||||||
|
|
||||||
|
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):
|
class Service(db.Model, Versioned):
|
||||||
__tablename__ = 'services'
|
__tablename__ = 'services'
|
||||||
|
|
||||||
@@ -213,6 +234,7 @@ class Service(db.Model, Versioned):
|
|||||||
organisation_id = db.Column(UUID(as_uuid=True), db.ForeignKey('organisation.id'), index=True, nullable=True)
|
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)
|
free_sms_fragment_limit = db.Column(db.BigInteger, index=False, unique=False, nullable=True)
|
||||||
organisation = db.relationship('Organisation')
|
organisation = db.relationship('Organisation')
|
||||||
|
annual_billing = db.relationship('AnnualBilling')
|
||||||
dvla_organisation_id = db.Column(
|
dvla_organisation_id = db.Column(
|
||||||
db.String,
|
db.String,
|
||||||
db.ForeignKey('dvla_organisation.id'),
|
db.ForeignKey('dvla_organisation.id'),
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ manager.add_command('populate_service_sms_sender', commands.PopulateServiceSmsSe
|
|||||||
manager.add_command('populate_service_letter_contact', commands.PopulateServiceLetterContact)
|
manager.add_command('populate_service_letter_contact', commands.PopulateServiceLetterContact)
|
||||||
manager.add_command('populate_service_and_service_history_free_sms_fragment_limit',
|
manager.add_command('populate_service_and_service_history_free_sms_fragment_limit',
|
||||||
commands.PopulateServiceAndServiceHistoryFreeSmsFragmentLimit)
|
commands.PopulateServiceAndServiceHistoryFreeSmsFragmentLimit)
|
||||||
|
manager.add_command('populate_annual_billing',
|
||||||
|
commands.PopulateAnnualBilling)
|
||||||
|
|
||||||
|
|
||||||
@manager.command
|
@manager.command
|
||||||
|
|||||||
33
migrations/versions/0126_add_annual_billing.py
Normal file
33
migrations/versions/0126_add_annual_billing.py
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
"""
|
||||||
|
|
||||||
|
Revision ID: 0126_add_annual_billing
|
||||||
|
Revises: 0125_add_organisation_type
|
||||||
|
Create Date: 2017-10-19 11:38:32.849573
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
from sqlalchemy.dialects import postgresql
|
||||||
|
|
||||||
|
revision = '0126_add_annual_billing'
|
||||||
|
down_revision = '0125_add_organisation_type'
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table('annual_billing',
|
||||||
|
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
|
||||||
|
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('updated_at', sa.DateTime(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(), nullable=False),
|
||||||
|
sa.ForeignKeyConstraint(['service_id'], ['services.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')
|
||||||
|
|
||||||
Reference in New Issue
Block a user