mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
80 lines
2.4 KiB
Python
80 lines
2.4 KiB
Python
from datetime import datetime, timezone
|
|
import os
|
|
from typing import cast
|
|
|
|
from backend.app.db.connection import db_session
|
|
from backend.app.db.models.uploaded_file import (
|
|
FileSourceEnum,
|
|
FileTypeEnum,
|
|
UploadedFile,
|
|
)
|
|
from backend.documents_parser.db_writer import save_epc_property_data
|
|
from backend.documents_parser.parser import parse_site_notes_pdf
|
|
from utils.logger import setup_logger
|
|
from utils.s3 import upload_file_to_s3
|
|
from utils.sharepoint.domna_sharepoint_client import DomnaSharepointClient
|
|
|
|
logger = setup_logger()
|
|
|
|
|
|
def upload_file_to_sharepoint(
|
|
client: DomnaSharepointClient,
|
|
file_path: str,
|
|
base_path: str,
|
|
subpath: str,
|
|
) -> None:
|
|
filename = os.path.basename(file_path)
|
|
|
|
full_path = f"{base_path}/{subpath}/1. Retrofit Assessment/A. Assessment"
|
|
|
|
client.upload_file(
|
|
file_path=file_path,
|
|
sharepoint_path=full_path,
|
|
file_name=filename,
|
|
)
|
|
|
|
|
|
def upload_excel_to_sharepoint(
|
|
client: DomnaSharepointClient,
|
|
file_path: str,
|
|
sharepoint_path: str,
|
|
) -> None:
|
|
client.upload_file(
|
|
file_path=file_path,
|
|
sharepoint_path=sharepoint_path,
|
|
file_name=os.path.basename(file_path),
|
|
)
|
|
|
|
|
|
# TODO: this should be moved to somewhere common and called by pashub fetcher
|
|
def upload_file_to_s3_and_update_db(
|
|
bucket: str, file_path: str, hubspot_listing_id: str, file_type: FileTypeEnum
|
|
) -> int:
|
|
filename: str = os.path.basename(file_path)
|
|
key: str = f"documents/hubspot_listing_id/{hubspot_listing_id}/{filename}"
|
|
|
|
upload_file_to_s3(file_path, bucket, key)
|
|
|
|
uploaded_file = UploadedFile(
|
|
s3_file_bucket=bucket,
|
|
s3_file_key=key,
|
|
s3_upload_timestamp=datetime.now(timezone.utc),
|
|
hubspot_listing_id=hubspot_listing_id,
|
|
file_source=FileSourceEnum.ECMK.value,
|
|
file_type=file_type.value,
|
|
)
|
|
|
|
with db_session() as session:
|
|
# TODO: we should do multiple files at once to reduce db trips
|
|
session.add(uploaded_file)
|
|
session.flush()
|
|
uploaded_file_id: int = int(cast(int, uploaded_file.id))
|
|
|
|
if file_type == FileTypeEnum.ECMK_RD_SAP_SITE_NOTE:
|
|
try:
|
|
epc_data = parse_site_notes_pdf(file_path)
|
|
save_epc_property_data(session, epc_data, uploaded_file_id=uploaded_file_id)
|
|
except Exception:
|
|
logger.warning(f"Failed to parse/save site notes {file_path}", exc_info=True)
|
|
|
|
return uploaded_file_id
|