Skip to content

Commit

Permalink
Merge pull request #1608 from bcgov/feat/daniel-add-is-legacy-1550
Browse files Browse the repository at this point in the history
feat: Add Legacy Provisions
  • Loading branch information
dhaselhan authored Jan 6, 2025
2 parents 59e57de + 31ee4a3 commit 71a6856
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 11 deletions.
125 changes: 125 additions & 0 deletions backend/lcfs/db/migrations/versions/2025-01-06-19-01_94306eca5261.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
"""
Add is_legacy to Provisions and insert data
Revision ID: 94306eca5261
Revises: ca7200152130
Create Date: 2025-01-06 19:01:53.418638
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "94306eca5261"
down_revision = "ca7200152130"
branch_labels = None
depends_on = None


def upgrade() -> None:
# 1) Add the new column
op.add_column(
"provision_of_the_act",
sa.Column(
"is_legacy",
sa.Boolean(),
server_default=sa.text("FALSE"),
nullable=False,
comment="Indicates if the provision is legacy and should not be used for new reports",
),
)

# 2) Insert or update data to populate is_legacy, etc.
# For demonstration, we'll use bulk_insert here. If your table already
# has data, you might prefer an UPDATE or a combination of both.
provision_of_the_act = sa.Table(
"provision_of_the_act",
sa.MetaData(),
sa.Column("provision_of_the_act_id", sa.Integer, primary_key=True),
sa.Column("name", sa.String),
sa.Column("description", sa.String),
sa.Column("create_user", sa.String),
sa.Column("update_user", sa.String),
sa.Column("display_order", sa.Integer),
sa.Column("effective_date", sa.Date),
sa.Column("effective_status", sa.Boolean),
sa.Column("expiration_date", sa.Date),
sa.Column("is_legacy", sa.Boolean),
)

op.bulk_insert(
provision_of_the_act,
[
{
"name": "Prescribed carbon intensity - Section 6 (5) (a)",
"description": "Prescribed carbon intensity - Section 6 (5) (a)",
"create_user": "no_user",
"update_user": "no_user",
"display_order": None,
"effective_date": None,
"effective_status": True,
"expiration_date": None,
"is_legacy": True,
},
{
"name": "Prescribed carbon intensity - Section 6 (5) (b)",
"description": "Prescribed carbon intensity - Section 6 (5) (b)",
"create_user": "no_user",
"update_user": "no_user",
"display_order": None,
"effective_date": None,
"effective_status": True,
"expiration_date": None,
"is_legacy": True,
},
{
"name": "Approved fuel code - Section 6 (5) (c)",
"description": "Approved fuel code - Section 6 (5) (c)",
"create_user": "no_user",
"update_user": "no_user",
"display_order": None,
"effective_date": None,
"effective_status": True,
"expiration_date": None,
"is_legacy": True,
},
{
"name": "Default Carbon Intensity Value - Section 6 (5) (d) (i)",
"description": "Default Carbon Intensity Value - Section 6 (5) (d) (i)",
"create_user": "no_user",
"update_user": "no_user",
"display_order": None,
"effective_date": None,
"effective_status": True,
"expiration_date": None,
"is_legacy": True,
},
{
"name": "GHGenius modelled - Section 6 (5) (d) (ii) (A)",
"description": "GHGenius modelled - Section 6 (5) (d) (ii) (A)",
"create_user": "no_user",
"update_user": "no_user",
"display_order": None,
"effective_date": None,
"effective_status": True,
"expiration_date": None,
"is_legacy": True,
},
{
"name": "Alternative Method - Section 6 (5) (d) (ii) (B)",
"description": "Alternative Method - Section 6 (5) (d) (ii) (B)",
"create_user": "no_user",
"update_user": "no_user",
"display_order": None,
"effective_date": None,
"effective_status": True,
"expiration_date": None,
"is_legacy": True,
},
],
)


def downgrade() -> None:
# Remove is_legacy column. (Data removal is optional or up to you.)
op.drop_column("provision_of_the_act", "is_legacy")
8 changes: 7 additions & 1 deletion backend/lcfs/db/models/fuel/ProvisionOfTheAct.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import Column, Integer, String, Boolean
from sqlalchemy import Column, Integer, String, Boolean, text
from sqlalchemy.orm import relationship
from lcfs.db.base import BaseModel, Auditable, DisplayOrder, EffectiveDates

Expand Down Expand Up @@ -28,6 +28,12 @@ class ProvisionOfTheAct(BaseModel, Auditable, DisplayOrder, EffectiveDates):
nullable=False,
comment="Description of the provision. This is the displayed name. e.g. Prescribed Carbon Intensity, Approved Fuel Code.",
)
is_legacy = Column(
Boolean,
server_default=text("FALSE"),
nullable=False,
comment="Indicates if the fuel type is legacy and should not be used for new reports",
)

# relationships
fuel_type_provision_1 = relationship(
Expand Down
14 changes: 11 additions & 3 deletions backend/lcfs/web/api/allocation_agreement/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,25 @@ def __init__(
@repo_handler
async def get_table_options(self, compliance_period: str) -> dict:
"""Get all table options"""
include_legacy = compliance_period < LCFS_Constants.LEGISLATION_TRANSITION_YEAR

fuel_categories = await self.fuel_code_repo.get_fuel_categories()
fuel_types = await self.fuel_code_repo.get_formatted_fuel_types(
include_legacy=compliance_period
< LCFS_Constants.LEGISLATION_TRANSITION_YEAR
include_legacy=include_legacy
)
units_of_measure = [unit.value for unit in QuantityUnitsEnum]
allocation_transaction_types = (
(await self.db.execute(select(AllocationTransactionType))).scalars().all()
)

provisions_select = select(ProvisionOfTheAct)
if include_legacy:
provisions_select = provisions_select.where(
ProvisionOfTheAct.is_legacy == True
)

provisions_of_the_act = (
(await self.db.execute(select(ProvisionOfTheAct))).scalars().all()
(await self.db.execute(provisions_select)).scalars().all()
)
fuel_codes = (await self.db.execute(select(FuelCode))).scalars().all()

Expand Down
4 changes: 3 additions & 1 deletion backend/lcfs/web/api/fuel_export/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ async def get_fuel_export_table_options(self, compliance_period: str):

include_legacy = compliance_period < LCFS_Constants.LEGISLATION_TRANSITION_YEAR
if not include_legacy:
query = query.where(FuelType.is_legacy == False)
query = query.where(
and_(FuelType.is_legacy == False, ProvisionOfTheAct.is_legacy == False)
)

results = (await self.db.execute(query)).all()
return results
Expand Down
4 changes: 3 additions & 1 deletion backend/lcfs/web/api/fuel_supply/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ async def get_fuel_supply_table_options(self, compliance_period: str):

include_legacy = compliance_period < LCFS_Constants.LEGISLATION_TRANSITION_YEAR
if not include_legacy:
query.where(FuelType.is_legacy == False)
query = query.where(
and_(FuelType.is_legacy == False, ProvisionOfTheAct.is_legacy == False)
)

fuel_type_results = (await self.db.execute(query)).all()

Expand Down
15 changes: 10 additions & 5 deletions backend/lcfs/web/api/other_uses/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,21 @@ def __init__(
@repo_handler
async def get_table_options(self, compliance_period: str) -> dict:
"""Get all table options"""
include_legacy = compliance_period < LCFS_Constants.LEGISLATION_TRANSITION_YEAR
fuel_categories = await self.fuel_code_repo.get_fuel_categories()
fuel_types = await self.get_formatted_fuel_types(
include_legacy=compliance_period
< LCFS_Constants.LEGISLATION_TRANSITION_YEAR
)
fuel_types = await self.get_formatted_fuel_types(include_legacy=include_legacy)
expected_uses = await self.fuel_code_repo.get_expected_use_types()
units_of_measure = [unit.value for unit in QuantityUnitsEnum]

provisions_select = select(ProvisionOfTheAct)
if include_legacy:
provisions_select = provisions_select.where(
ProvisionOfTheAct.is_legacy == True
)
provisions_of_the_act = (
(await self.db.execute(select(ProvisionOfTheAct))).scalars().all()
(await self.db.execute(provisions_select)).scalars().all()
)

fuel_codes = (await self.db.execute(select(FuelCode))).scalars().all()

return {
Expand Down

0 comments on commit 71a6856

Please sign in to comment.