Skip to content

Commit

Permalink
Merge pull request #2064 from bcgov/fix/daniel-ff-import-1927
Browse files Browse the repository at this point in the history
fix: Import Fixes and add feature flag
  • Loading branch information
dhaselhan authored Feb 25, 2025
2 parents 3fd2a6b + 480c017 commit 92c6c92
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 43 deletions.
43 changes: 28 additions & 15 deletions backend/lcfs/web/api/final_supply_equipment/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,14 @@ async def import_async(
socket_connect_timeout=5,
)

await _update_progress(redis_client, job_id, 5, "Initializing services...")
await _update_progress(
redis_client, job_id, 5, "Initializing services..."
)

if overwrite:
await _update_progress(redis_client, job_id, 10, "Deleting old data...")
await _update_progress(
redis_client, job_id, 10, "Deleting old data..."
)
await fse_service.delete_all(compliance_report_id)
org_code = user.organization.organization_code
await fse_repo.reset_seq_by_org(org_code)
Expand All @@ -165,7 +169,9 @@ async def import_async(
)
clamav_service.scan_file(file)

await _update_progress(redis_client, job_id, 20, "Loading Excel sheet...")
await _update_progress(
redis_client, job_id, 20, "Loading Excel sheet..."
)

try:
sheet = _load_sheet(file)
Expand All @@ -188,7 +194,9 @@ async def import_async(
valid_intended_users = await fse_repo.get_intended_user_types()
valid_use_types = await fse_repo.get_intended_use_types()
valid_use_type_names = {obj.type for obj in valid_use_types}
valid_user_type_names = {obj.type_name for obj in valid_intended_users}
valid_user_type_names = {
obj.type_name for obj in valid_intended_users
}

# Iterate through all data rows, skipping the header
for row_idx, row in enumerate(
Expand All @@ -210,6 +218,10 @@ async def import_async(
errors=errors,
)

# Check if the entire row is empty
if all(cell is None for cell in row):
continue

# Validate row
error = _validate_row(
row, row_idx, valid_use_type_names, valid_user_type_names
Expand All @@ -222,7 +234,9 @@ async def import_async(
# Parse row data and insert into DB
try:
fse_data = _parse_row(row, compliance_report_id)
await fse_service.create_final_supply_equipment(fse_data, user)
await fse_service.create_final_supply_equipment(
fse_data, user
)
created += 1
except Exception as ex:
logger.error(str(ex))
Expand All @@ -239,7 +253,9 @@ async def import_async(
rejected=rejected,
errors=errors,
)
logger.debug(f"Completed importing FSE data, {created} rows created")
logger.debug(
f"Completed importing FSE data, {created} rows created"
)

return {
"success": True,
Expand All @@ -266,6 +282,7 @@ async def import_async(
finally:
await engine.dispose()


def _load_sheet(file: UploadFile) -> Worksheet:
"""
Loads and returns the 'FSE' worksheet from the provided Excel file.
Expand Down Expand Up @@ -307,10 +324,6 @@ def _validate_row(
notes,
) = row

# Check if the entire row is empty
if all(cell is None for cell in row):
return f"Row {row_idx}: Row is empty"

missing_fields = []
if supply_from_date is None:
missing_fields.append("Supply from date")
Expand Down Expand Up @@ -404,15 +417,15 @@ def _parse_row(
supply_from_date=supply_from_date,
supply_to_date=supply_to_date,
kwh_usage=kwh_usage,
serial_nbr=serial_number or "",
manufacturer=manufacturer or "",
model=model or "",
serial_nbr=str(serial_number) or "",
manufacturer=str(manufacturer) or "",
model=str(model) or "",
level_of_equipment=level_of_equipment or "",
ports=PortsEnum(ports) if ports else None,
intended_uses=intended_uses,
intended_users=intended_users,
street_address=street_address or "",
city=city or "",
street_address=str(street_address) or "",
city=str(city) or "",
postal_code=postal_code or "",
latitude=latitude,
longitude=longitude,
Expand Down
3 changes: 2 additions & 1 deletion frontend/public/config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export const config = {
},
feature_flags: {
supplementalReporting: true,
fullLegacyReports: true
fullLegacyReports: true,
fseImportExport: true
}
}

Expand Down
8 changes: 5 additions & 3 deletions frontend/src/constants/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const isFeatureEnabled = (featureFlag) => {

export const FEATURE_FLAGS = {
SUPPLEMENTAL_REPORTING: 'supplementalReporting',
LEGACY_REPORT_DETAILS: 'fullLegacyReports'
LEGACY_REPORT_DETAILS: 'fullLegacyReports',
FSE_IMPORT_EXPORT: 'fseImportExport'
}

export const CONFIG = {
Expand All @@ -56,8 +57,9 @@ export const CONFIG = {
},
feature_flags: {
supplementalReporting:
window.lcfs_config.feature_flags.supplementalReporting ?? true,
window.lcfs_config.feature_flags.supplementalReporting ?? false,
fullLegacyReports:
window.lcfs_config.feature_flags.fullLegacyReports ?? true
window.lcfs_config.feature_flags.fullLegacyReports ?? false,
fseImportExport: window.lcfs_config.feature_flags.fseImportExport ?? false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Menu, MenuItem } from '@mui/material'
import { faCaretDown } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import ImportFuelSupplyEquipmentDialog from '@/views/FinalSupplyEquipments/ImportFuelSupplyEquipmentDialog.jsx'
import { FEATURE_FLAGS, isFeatureEnabled } from '@/constants/config.js'

export const AddEditFinalSupplyEquipments = () => {
const [rowData, setRowData] = useState([])
Expand Down Expand Up @@ -317,18 +318,20 @@ export const AddEditFinalSupplyEquipments = () => {
</BCBox>
</div>
<BCBox>
<BCButton
color="primary"
variant="outlined"
aria-controls={isDownloadOpen ? 'download-menu' : undefined}
aria-haspopup="true"
aria-expanded={isDownloadOpen ? 'true' : undefined}
onClick={handleDownloadClick}
endIcon={<FontAwesomeIcon icon={faCaretDown} />}
isLoading={isDownloading}
>
{t('finalSupplyEquipment:downloadBtn')}
</BCButton>
{isFeatureEnabled(FEATURE_FLAGS.FSE_IMPORT_EXPORT) && (
<BCButton
color="primary"
variant="outlined"
aria-controls={isDownloadOpen ? 'download-menu' : undefined}
aria-haspopup="true"
aria-expanded={isDownloadOpen ? 'true' : undefined}
onClick={handleDownloadClick}
endIcon={<FontAwesomeIcon icon={faCaretDown} />}
isLoading={isDownloading}
>
{t('finalSupplyEquipment:downloadBtn')}
</BCButton>
)}
<Menu
id="download-menu"
anchorEl={downloadAnchorEl}
Expand Down Expand Up @@ -360,18 +363,20 @@ export const AddEditFinalSupplyEquipments = () => {
{t('finalSupplyEquipment:downloadWithoutDataBtn')}
</MenuItem>
</Menu>
<BCButton
style={{ marginLeft: '12px' }}
color="primary"
variant="outlined"
aria-controls={isImportOpen ? 'import-menu' : undefined}
aria-haspopup="true"
aria-expanded={isImportOpen ? 'true' : undefined}
onClick={handleImportClick}
endIcon={<FontAwesomeIcon icon={faCaretDown} />}
>
{t('finalSupplyEquipment:importBtn')}
</BCButton>
{isFeatureEnabled(FEATURE_FLAGS.FSE_IMPORT_EXPORT) && (
<BCButton
style={{ marginLeft: '12px' }}
color="primary"
variant="outlined"
aria-controls={isImportOpen ? 'import-menu' : undefined}
aria-haspopup="true"
aria-expanded={isImportOpen ? 'true' : undefined}
onClick={handleImportClick}
endIcon={<FontAwesomeIcon icon={faCaretDown} />}
>
{t('finalSupplyEquipment:importBtn')}
</BCButton>
)}
<Menu
id="import-menu"
slotProps={{
Expand Down

0 comments on commit 92c6c92

Please sign in to comment.