Skip to content

Commit

Permalink
Merge branch 'release-0.2.0' into feat/hamed-fix-404-error-1516
Browse files Browse the repository at this point in the history
  • Loading branch information
prv-proton authored Jan 2, 2025
2 parents 1399ddf + 6597cd3 commit 3eadd24
Show file tree
Hide file tree
Showing 66 changed files with 1,208 additions and 389 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Add UQ to TCI
Revision ID: ab04810d4d7c
Revises: 5fbcb508c1be
Create Date: 2024-12-22 23:46:37.505166
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "ab04810d4d7c"
down_revision = "5fbcb508c1be"
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_unique_constraint(
"uq_target_carbon_intensity_compliance_fuel",
"target_carbon_intensity",
["compliance_period_id", "fuel_category_id"],
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint(
"uq_target_carbon_intensity_compliance_fuel",
"target_carbon_intensity",
type_="unique",
)
# ### end Alembic commands ###
3 changes: 1 addition & 2 deletions backend/lcfs/db/models/admin_adjustment/AdminAdjustment.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, DateTime, String
from sqlalchemy.orm import relationship
from sqlalchemy import UniqueConstraint

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


class AdminAdjustment(BaseModel, Auditable, EffectiveDates):
__tablename__ = "admin_adjustment"
__table_args__ = (
UniqueConstraint("admin_adjustment_id"),
{"comment": "Goverment to organization compliance units admin_adjustment"},
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, DateTime
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy import UniqueConstraint

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


class AdminAdjustmentHistory(BaseModel, Auditable, EffectiveDates):
__tablename__ = "admin_adjustment_history"
__table_args__ = (
UniqueConstraint("admin_adjustment_history_id"),
{"comment": "History record for admin_adjustment status change."},
)

Expand Down
14 changes: 9 additions & 5 deletions backend/lcfs/db/models/comment/InternalComment.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
from sqlalchemy import Column, Integer, Text, UniqueConstraint
from sqlalchemy.orm import relationship
from sqlalchemy import Column, Integer, Text
from sqlalchemy.dialects.postgresql import ENUM
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable

# ENUM for audience scope
audience_scope_enum = ENUM(
"Director", "Analyst", "Compliance Manager", name="audience_scope", create_type=False
"Director",
"Analyst",
"Compliance Manager",
name="audience_scope",
create_type=False,
)


class InternalComment(BaseModel, Auditable):
__tablename__ = "internal_comment"
__table_args__ = (
UniqueConstraint("internal_comment_id"),
{"comment": "Stores internal comments with scope and related metadata."},
)

Expand Down Expand Up @@ -42,4 +46,4 @@ class InternalComment(BaseModel, Auditable):
)
compliance_report_internal_comments = relationship(
"ComplianceReportInternalComment", back_populates="internal_comment"
)
)
3 changes: 2 additions & 1 deletion backend/lcfs/db/models/compliance/CompliancePeriod.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sqlalchemy import Column, Integer, String, Date
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, EffectiveDates


Expand Down
6 changes: 3 additions & 3 deletions backend/lcfs/db/models/compliance/ComplianceReport.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import uuid
import enum
import uuid

from pydantic import computed_field
from sqlalchemy import (
Column,
Integer,
String,
ForeignKey,
Enum,
Table,
ForeignKey,
)
from sqlalchemy.orm import relationship, backref
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable


Expand Down
6 changes: 0 additions & 6 deletions backend/lcfs/db/models/compliance/listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,3 @@ def prevent_update_if_locked(mapper, connection, target):
raise InvalidRequestError("Cannot update a locked ComplianceReportSummary")


@event.listens_for(ComplianceReportSummary.is_locked, "set")
def prevent_unlock(target, value, oldvalue, initiator):
if oldvalue and not value:
raise InvalidRequestError(
"Cannot unlock a ComplianceReportSummary once it's locked"
)
4 changes: 1 addition & 3 deletions backend/lcfs/db/models/document/Document.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy import UniqueConstraint
from sqlalchemy import Column, Integer, String
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable
Expand All @@ -11,7 +10,6 @@
class Document(BaseModel, Auditable):
__tablename__ = "document"
__table_args__ = (
UniqueConstraint("document_id"),
{"comment": "Main document table for storing base document information"},
)

Expand Down
5 changes: 3 additions & 2 deletions backend/lcfs/db/models/fuel/FuelCategory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from sqlalchemy import Column, Integer, Text, Enum, Float, Numeric
from lcfs.db.base import BaseModel, Auditable, DisplayOrder, EffectiveDates
from sqlalchemy import Column, Integer, Text, Enum, Numeric
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable, DisplayOrder, EffectiveDates


class FuelCategory(BaseModel, Auditable, DisplayOrder, EffectiveDates):

Expand Down
10 changes: 6 additions & 4 deletions backend/lcfs/db/models/fuel/FuelType.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from sqlalchemy import Column, Integer, Text, Boolean, Float, Enum, Numeric
from lcfs.db.base import BaseModel, Auditable, DisplayOrder
from sqlalchemy.orm import relationship
from sqlalchemy import ForeignKey
import enum

from sqlalchemy import Column, Integer, Text, Boolean, Enum, Numeric
from sqlalchemy import ForeignKey
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable, DisplayOrder


# Enum for fuel quantity units
class QuantityUnitsEnum(enum.Enum):
Expand Down
13 changes: 9 additions & 4 deletions backend/lcfs/db/models/fuel/TargetCarbonIntensity.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from sqlalchemy import Column, Integer, Float, ForeignKey, Numeric
from sqlalchemy import Column, Integer, Float, ForeignKey, Numeric, UniqueConstraint
from lcfs.db.base import BaseModel, Auditable, EffectiveDates
from sqlalchemy.orm import relationship


class TargetCarbonIntensity(BaseModel, Auditable, EffectiveDates):
__tablename__ = "target_carbon_intensity"
__table_args__ = {
"comment": "Target carbon intensity values for various fuel categories"
}
__table_args__ = (
UniqueConstraint(
"compliance_period_id",
"fuel_category_id",
name="uq_target_carbon_intensity_compliance_fuel",
),
{"comment": "Target carbon intensity values for various fuel categories"},
)

target_carbon_intensity_id = Column(Integer, primary_key=True, autoincrement=True)
compliance_period_id = Column(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, DateTime, String
from sqlalchemy.orm import relationship
from sqlalchemy import UniqueConstraint

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


class InitiativeAgreement(BaseModel, Auditable, EffectiveDates):
__tablename__ = "initiative_agreement"
__table_args__ = (
UniqueConstraint("initiative_agreement_id"),
{"comment": "Goverment to organization compliance units initiative agreement"},
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from sqlalchemy import Column, Integer, BigInteger, ForeignKey, DateTime
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy import UniqueConstraint

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


class InitiativeAgreementHistory(BaseModel, Auditable, EffectiveDates):
__tablename__ = "initiative_agreement_history"
__table_args__ = (
UniqueConstraint("initiative_agreement_history_id"),
{"comment": "History record for initiative agreement status change."},
)

Expand Down
2 changes: 0 additions & 2 deletions backend/lcfs/db/models/transaction/Transaction.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import enum

from sqlalchemy import Column, Integer, BigInteger, ForeignKey, Enum
from sqlalchemy import UniqueConstraint
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable, EffectiveDates
Expand All @@ -16,7 +15,6 @@ class TransactionActionEnum(enum.Enum):
class Transaction(BaseModel, Auditable, EffectiveDates):
__tablename__ = "transaction"
__table_args__ = (
UniqueConstraint("transaction_id"),
{
"comment": "Contains a list of all of the government to organization and Organization to Organization transaction."
},
Expand Down
4 changes: 2 additions & 2 deletions backend/lcfs/db/models/transfer/Transfer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import enum

from sqlalchemy import Column, Integer, ForeignKey, DateTime, Enum, String, Numeric
from sqlalchemy.orm import relationship
from sqlalchemy import UniqueConstraint

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


Expand All @@ -13,7 +14,6 @@ class TransferRecommendationEnum(enum.Enum):
class Transfer(BaseModel, Auditable, EffectiveDates):
__tablename__ = "transfer"
__table_args__ = (
UniqueConstraint("transfer_id"),
{"comment": "Records of tranfer from Organization to Organization"},
)

Expand Down
5 changes: 1 addition & 4 deletions backend/lcfs/db/models/transfer/TransferCategory.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ class TransferCategoryEnum(enum.Enum):

class TransferCategory(BaseModel, Auditable, EffectiveDates):
__tablename__ = "transfer_category"
__table_args__ = (
UniqueConstraint("transfer_category_id"),
{"comment": "Transfer Category"},
)
__table_args__ = ({"comment": "Transfer Category"},)

transfer_category_id = Column(
Integer,
Expand Down
3 changes: 2 additions & 1 deletion backend/lcfs/db/models/transfer/TransferHistory.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from sqlalchemy import Column, Integer, ForeignKey, DateTime
from sqlalchemy import Column, Integer, ForeignKey
from sqlalchemy.orm import relationship

from lcfs.db.base import BaseModel, Auditable, EffectiveDates


Expand Down
51 changes: 35 additions & 16 deletions backend/lcfs/services/rabbitmq/report_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ async def process_message(self, body: bytes):
Expected message structure:
{
"tfrs_id": int,
"root_report_id": int,
"organization_id": int,
"compliance_period": str,
"nickname": str,
"action": "Created"|"Submitted"|"Approved",
"credits": int (optional),
"user_id": int
Expand All @@ -96,8 +96,8 @@ async def process_message(self, body: bytes):
action=action,
compliance_period=message.get("compliance_period"),
compliance_units=message.get("credits"),
root_report_id=message["root_report_id"],
legacy_id=message["tfrs_id"],
nickname=message.get("nickname"),
org_id=org_id,
user_id=message["user_id"],
)
Expand Down Expand Up @@ -127,8 +127,8 @@ async def handle_message(
action: str,
compliance_period: str,
compliance_units: Optional[int],
root_report_id: int,
legacy_id: int,
nickname: Optional[str],
org_id: int,
user_id: int,
):
Expand Down Expand Up @@ -157,15 +157,18 @@ async def handle_message(
user = await UserRepository(db=session).get_user_by_id(user_id)

if not user:
logger.error(f"Cannot parse Report {legacy_id} from TFRS, no user with ID {user_id}")
logger.error(
f"Cannot parse Report {legacy_id} from TFRS, no user with ID {user_id}"
)

if action == "Created":
await self._handle_created(
org_id,
root_report_id,
legacy_id,
compliance_period,
nickname,
user,
compliance_report_repo,
compliance_report_service,
)
elif action == "Submitted":
Expand All @@ -190,25 +193,41 @@ async def handle_message(
async def _handle_created(
self,
org_id: int,
root_report_id: int,
legacy_id: int,
compliance_period: str,
nickname: str,
user: UserProfile,
compliance_report_repo: ComplianceReportRepository,
compliance_report_service: ComplianceReportServices,
):
"""
Handle the 'Created' action by creating a new compliance report draft.
"""
lcfs_report = ComplianceReportCreateSchema(
legacy_id=legacy_id,
compliance_period=compliance_period,
organization_id=org_id,
nickname=nickname,
status=ComplianceReportStatusEnum.Draft.value,
)
await compliance_report_service.create_compliance_report(
org_id, lcfs_report, user
)
if root_report_id == legacy_id: # this is a new initial report
lcfs_report = ComplianceReportCreateSchema(
legacy_id=legacy_id,
compliance_period=compliance_period,
organization_id=org_id,
nickname="Original Report",
status=ComplianceReportStatusEnum.Draft.value,
)
await compliance_report_service.create_compliance_report(
org_id, lcfs_report, user
)
else:
# Process a new supplemental report
root_report = (
await compliance_report_repo.get_compliance_report_by_legacy_id(
root_report_id
)
)
if not root_report:
raise ServiceException(
f"No original compliance report found for legacy ID {root_report_id}"
)
await compliance_report_service.create_supplemental_report(
root_report_id, user, legacy_id
)

async def _handle_approved(
self,
Expand Down
Loading

0 comments on commit 3eadd24

Please sign in to comment.