Skip to content

Commit

Permalink
Merge branch 'release-0.2.0' into feat/kevin-1588
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-hashimoto authored Jan 16, 2025
2 parents 17e513a + 7ffaa75 commit 16edad6
Show file tree
Hide file tree
Showing 57 changed files with 854 additions and 305 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cron-cleanup-workflow-runs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ jobs:
with:
token: ${{ github.token }}
repository: ${{ github.repository }}
retain_days: 15
retain_days: 5
keep_minimum_runs: 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""revert org display balance
Revision ID: 5163af6ba4a4
Revises: f78e53370ed2
Create Date: 2025-01-14 14:03:50.975682
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "5163af6ba4a4"
down_revision = "f78e53370ed2"
branch_labels = None
depends_on = None


def upgrade() -> None:
# Reapply the original logic for balances:
op.execute(
"""
CREATE OR REPLACE FUNCTION update_organization_balance()
RETURNS TRIGGER AS $$
DECLARE
new_total_balance BIGINT;
new_reserved_balance BIGINT;
org_id INT := COALESCE(NEW.organization_id, OLD.organization_id);
BEGIN
SELECT COALESCE(SUM(compliance_units), 0)
INTO new_total_balance
FROM "transaction"
WHERE organization_id = org_id
AND transaction_action = 'Adjustment';
SELECT COALESCE(SUM(compliance_units), 0)
INTO new_reserved_balance
FROM "transaction"
WHERE organization_id = org_id
AND transaction_action = 'Reserved'
AND compliance_units < 0;
UPDATE organization
SET total_balance = new_total_balance,
reserved_balance = new_reserved_balance
WHERE organization_id = org_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
"""
)

op.execute(
"""
DROP TRIGGER IF EXISTS update_organization_balance_trigger ON "transaction";
"""
)
op.execute(
"""
CREATE TRIGGER update_organization_balance_trigger
AFTER INSERT OR UPDATE OR DELETE ON "transaction"
FOR EACH ROW EXECUTE FUNCTION update_organization_balance();
"""
)


def downgrade() -> None:
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Update other_uses_fossil_derived in fuel_type
Revision ID: fe03799b4018
Revises: fa98709e7952
Create Date: 2025-01-14 18:12:43.683691
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "fe03799b4018"
down_revision = "5163af6ba4a4"
branch_labels = None
depends_on = None


def upgrade() -> None:
# Set other_uses_fossil_derived to false
op.execute("""
UPDATE fuel_type
SET other_uses_fossil_derived = false
WHERE fuel_type IN (
'CNG', 'Electricity', 'Hydrogen', 'LNG', 'Propane',
'Natural gas-based gasoline', 'Petroleum-based diesel',
'Petroleum-based gasoline'
)
""")

# Set other_uses_fossil_derived to true
op.execute("""
UPDATE fuel_type
SET other_uses_fossil_derived = true
WHERE fuel_type IN (
'Alternative jet fuel', 'Biodiesel', 'Ethanol', 'HDRD',
'Other diesel fuel', 'Renewable gasoline', 'Renewable naphtha'
)
""")

def downgrade() -> None:
# Revert `other_uses_fossil_derived` to original values for false
op.execute("""
UPDATE fuel_type
SET other_uses_fossil_derived = true
WHERE fuel_type IN (
'CNG', 'Electricity', 'Hydrogen', 'LNG', 'Propane',
'Natural gas-based gasoline', 'Petroleum-based diesel',
'Petroleum-based gasoline'
)
""")

# Revert `other_uses_fossil_derived` to original values for true
op.execute("""
UPDATE fuel_type
SET other_uses_fossil_derived = false
WHERE fuel_type IN (
'Alternative jet fuel', 'Biodiesel', 'Ethanol', 'HDRD',
'Other diesel fuel', 'Renewable gasoline', 'Renewable naphtha'
)
""")
14 changes: 10 additions & 4 deletions backend/lcfs/web/api/fuel_code/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,20 @@ async def get_table_options(self) -> TableOptionsSchema:
fp_locations = await self.repo.get_fp_locations()
facility_nameplate_capacity_units = [unit.value for unit in QuantityUnitsEnum]

field_options_results_dict = {}
# Use a set to remove duplicates
field_options_results_dict = {
"company": set(),
"feedstock": set(),
"feedstock_location": set(),
"feedstock_misc": set(),
"former_company": set(),
"contact_name": set(),
"contact_email": set(),
}
for row in field_options_results:
for key, value in row._mapping.items():
if value is None or value == "": # Skip empty strings or null values
continue
if key not in field_options_results_dict:
# Use a set to remove duplicates
field_options_results_dict[key] = set()
field_options_results_dict[key].add(value)

field_options = {
Expand Down
1 change: 1 addition & 0 deletions backend/lcfs/web/api/other_uses/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ async def get_formatted_fuel_types(
joinedload(FuelType.provision_1),
joinedload(FuelType.provision_2),
)
.order_by(FuelType.fuel_type)
)

result = await self.db.execute(query)
Expand Down
36 changes: 13 additions & 23 deletions backend/lcfs/web/api/user/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class UserRepository:
def __init__(self, db: AsyncSession = Depends(get_async_db_session)):
self.db = db

def apply_filters(self, pagination, conditions):
def apply_filters(self, pagination, conditions, full_name):
role_filter_present = False
for filter in pagination.filters:
filter_value = filter.filter
Expand All @@ -83,25 +83,8 @@ def apply_filters(self, pagination, conditions):
)
)
elif filter.field == "first_name":
txt = filter_value.split(" ")
field1 = get_field_for_filter(UserProfile, "first_name")
conditions.append(
apply_filter_conditions(
field1,
txt[0],
filter_option,
filter_type,
)
)
field2 = get_field_for_filter(UserProfile, "last_name")
conditions.append(
apply_filter_conditions(
field2,
txt[1] if len(txt) > 1 else "",
filter_option,
filter_type,
)
)
name_filter = full_name.ilike(f"%{filter_value}%")
conditions.append(name_filter)
else:
field = get_field_for_filter(UserProfile, filter.field)
conditions.append(
Expand Down Expand Up @@ -215,7 +198,7 @@ async def update_bceid_roles(self, user, new_roles, existing_roles_set):
async def get_users_paginated(
self,
pagination: PaginationRequestSchema,
) -> List[UserBaseSchema]:
) -> tuple[list[UserBaseSchema], int]:
"""
Queries users from the database with optional filters for username, surname,
organization, and active status. Supports pagination and sorting.
Expand All @@ -233,17 +216,24 @@ async def get_users_paginated(
# Build the base query statement
conditions = []
role_filter_present = False
full_name = func.concat_ws(" ", UserProfile.first_name, UserProfile.last_name)

if pagination.filters and len(pagination.filters) > 0:
try:
role_filter_present = self.apply_filters(pagination, conditions)
role_filter_present = self.apply_filters(
pagination, conditions, full_name
)
except Exception as e:
raise ValueError(f"Invalid filter provided: {pagination.filters}.")

# Apply pagination and sorting parameters
offset = 0 if (pagination.page < 1) else (pagination.page - 1) * pagination.size
limit = pagination.size
# get distinct profile ids from UserProfile
unique_ids_query = select(UserProfile).where(and_(*conditions))
unique_ids_query = select(
UserProfile,
full_name,
).where(and_(*conditions))
if role_filter_present:
unique_ids_query = unique_ids_query.join(
UserRole, UserProfile.user_profile_id == UserRole.user_profile_id
Expand Down
10 changes: 5 additions & 5 deletions backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ typing-extensions = "^4.12.2"
structlog = "^24.4.0"
python-multipart = "^0.0.18"
aio-pika = "^9.4.3"
jinja2 = "^3.1.4"
jinja2 = "^3.1.5"
requests = "^2.32.3"

[tool.poetry.dev-dependencies]
Expand Down
1 change: 0 additions & 1 deletion etl/nifi_scripts/organization.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def sourceQuery = """
coalesce(o.edrms_record, '') as edrms_record,
(
case
when os.status = 'Archived' then 'Suspended'
when oat.the_type = 'Buy And Sell' or oat.the_type = 'Sell Only' then 'Registered'
else 'Unregistered'
end
Expand Down
Loading

0 comments on commit 16edad6

Please sign in to comment.