mirror of
https://github.com/Hestia-Homes/survey-extraction.git
synced 2026-06-08 11:17:29 +00:00
made more progress on tables
This commit is contained in:
parent
13503a7495
commit
cade1e4cc1
3 changed files with 97 additions and 32 deletions
|
|
@ -98,3 +98,5 @@ class HubspotTodb():
|
|||
|
||||
# Creates the a final pre site note table that links all information
|
||||
presitenote = surveyedData.create_pre_site_note_table(db_session, assessor, summary_info)
|
||||
|
||||
surveyedData.load_property_description(db_session)
|
||||
|
|
@ -4,7 +4,7 @@ import math
|
|||
from etl.transform.preSiteNoteTypes import (
|
||||
AssessorInfo, CompanyInfo,
|
||||
PreSiteNotesSummaryInfo, PreSiteNote,
|
||||
PropertyDescription, Dimension,
|
||||
PropertyDescription, Dimension, HeatingType, Heating, HeatingSystemControls
|
||||
)
|
||||
import uuid
|
||||
|
||||
|
|
@ -35,13 +35,55 @@ class surveyedDataProcessor():
|
|||
data_dict=summary_data,
|
||||
lookup_field="reference_number"
|
||||
)
|
||||
|
||||
|
||||
def load_property_description(self, db_session, presitenote):
|
||||
propertydescription = self.pre_site_note.property_description.__dict__
|
||||
print(propertydescription)
|
||||
pass
|
||||
|
||||
def get_attribute_and_load(self, obj, attr_string, pydanticModel, db_session):
|
||||
found = getattr(obj, attr_string, None)
|
||||
if found:
|
||||
db = self.upsert_record(
|
||||
db_session=db_session,
|
||||
model_class=pydanticModel,
|
||||
data_dict=found.__dict__,
|
||||
lookup_field=None
|
||||
)
|
||||
return db
|
||||
return None
|
||||
|
||||
|
||||
def load_property_description(self, db_session):
|
||||
def check_if_attribute_exists(obj, attribute):
|
||||
a = getattr(obj, attribute, None)
|
||||
if a:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
property_des = self.pre_site_note.property_description.__dict__
|
||||
secondary_heating = self.get_attribute_and_load(self.pre_site_note.property_description, "secondaryHeatingType", HeatingType, db_session)
|
||||
|
||||
# main heating 2 and main heating 2 controls
|
||||
if check_if_attribute_exists(self.pre_site_note.property_description, "mainHeating2"):
|
||||
if check_if_attribute_exists(self.pre_site_note.property_description.mainHeating2, "controls"):
|
||||
mainheating2controls = self.get_attribute_and_load(self.pre_site_note.property_description.mainHeating2, "controls", HeatingSystemControls, db_session)
|
||||
data = self.pre_site_note.property_description.mainHeating2.__dict__
|
||||
data.pop("controls")
|
||||
|
||||
mainheating2 = self.upsert_record(
|
||||
db_session=db_session,
|
||||
model_class=Heating,
|
||||
data_dict=data,
|
||||
lookup_field=None,
|
||||
additional_fields= {"main_heating_2_id": mainheating2controls.id},
|
||||
)
|
||||
|
||||
else:
|
||||
mainheating2controls = None
|
||||
else:
|
||||
mainheating2 = None
|
||||
|
||||
|
||||
|
||||
|
||||
print(secondary_heating)
|
||||
|
||||
|
||||
def load_company_table(self, db_session):
|
||||
company_data = self.pre_site_note.company_information.__dict__
|
||||
|
|
@ -51,7 +93,7 @@ class surveyedDataProcessor():
|
|||
data_dict=company_data,
|
||||
lookup_field="trading_name"
|
||||
)
|
||||
|
||||
|
||||
|
||||
def create_pre_site_note_table(
|
||||
self,
|
||||
|
|
@ -75,7 +117,7 @@ class surveyedDataProcessor():
|
|||
db_session,
|
||||
model_class,
|
||||
data_dict,
|
||||
lookup_field: str,
|
||||
lookup_field,
|
||||
update_if_exists: bool = False,
|
||||
additional_fields: dict = None
|
||||
):
|
||||
|
|
@ -103,25 +145,26 @@ class surveyedDataProcessor():
|
|||
if additional_fields:
|
||||
clean_data.update(additional_fields)
|
||||
|
||||
lookup_value = clean_data.get(lookup_field)
|
||||
if not lookup_value:
|
||||
raise ValueError(f"Missing lookup field '{lookup_field}' in data.")
|
||||
if lookup_field is not None:
|
||||
lookup_value = clean_data.get(lookup_field)
|
||||
if not lookup_value:
|
||||
raise ValueError(f"Missing lookup field '{lookup_field}' in data.")
|
||||
|
||||
existing_record = db_session.query(model_class).filter(
|
||||
getattr(model_class, lookup_field) == lookup_value
|
||||
).first()
|
||||
existing_record = db_session.query(model_class).filter(
|
||||
getattr(model_class, lookup_field) == lookup_value
|
||||
).first()
|
||||
|
||||
if existing_record:
|
||||
if update_if_exists:
|
||||
for key, value in clean_data.items():
|
||||
setattr(existing_record, key, value)
|
||||
db_session.commit()
|
||||
return existing_record
|
||||
new_record = model_class(**clean_data)
|
||||
db_session.add(new_record)
|
||||
db_session.commit()
|
||||
return new_record
|
||||
|
||||
if existing_record:
|
||||
if update_if_exists:
|
||||
for key, value in clean_data.items():
|
||||
setattr(existing_record, key, value)
|
||||
db_session.commit()
|
||||
return existing_record
|
||||
else:
|
||||
new_record = model_class(**clean_data)
|
||||
db_session.add(new_record)
|
||||
db_session.commit()
|
||||
return new_record
|
||||
|
||||
def load_assessor_table(self, db_session):
|
||||
company = self.load_company_table(db_session)
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ class Lighting(BaseModel):
|
|||
total_no_of_light_fittings: int
|
||||
total_no_of_lel_fittings: int
|
||||
|
||||
class HeatingSystemControls(BaseModel):
|
||||
class HeatingSystemControls(BaseModel, table=True):
|
||||
control_type: str
|
||||
flue_type: str
|
||||
fan_assisted_flue: bool
|
||||
|
|
@ -117,7 +117,10 @@ class HeatingSystemControls(BaseModel):
|
|||
electricity_meter_type: Optional[str] = ""
|
||||
mains_gas_available: Optional[bool] = False
|
||||
|
||||
class Heating(BaseModel):
|
||||
# Reverse relationship (optional)
|
||||
heating: Optional["Heating"] = Relationship(back_populates="controls", sa_relationship=relationship("Heating", back_populates="controls", uselist=False))
|
||||
|
||||
class Heating(BaseModel, table=True):
|
||||
type: str
|
||||
heating_source: str
|
||||
efficiency_source: str
|
||||
|
|
@ -127,9 +130,13 @@ class Heating(BaseModel):
|
|||
model_qualifer: str
|
||||
sap_2009_table: Optional[str] = ""
|
||||
percentage_of_heated_floor_area_served: Optional[str] = ""
|
||||
controls: HeatingSystemControls
|
||||
|
||||
class HeatingType(BaseModel):
|
||||
# Foregin Key to HeatingSystemControls
|
||||
controls_id: uuid.UUID = Field(foreign_key="heatingsystemcontrols.id")
|
||||
|
||||
controls: HeatingSystemControls = Relationship(back_populates="heating")
|
||||
|
||||
class HeatingType(BaseModel, table=True):
|
||||
heating_type: str
|
||||
fuel_type: str
|
||||
|
||||
|
|
@ -215,8 +222,21 @@ class PropertyDescription(BaseModel):
|
|||
windTurbine: Optional[WindTurbine]
|
||||
otherDetails: Optional[OtherDetails]
|
||||
mainHeating: Optional[Heating]
|
||||
mainHeating2: Optional[Heating]
|
||||
secondaryHeatingType: Optional[HeatingType]
|
||||
|
||||
# Foreign key to Main Heating 2
|
||||
main_heating_2_id: Optional[uuid.UUID] = Field(
|
||||
default=None,
|
||||
foreign_key="heatingtype.id"
|
||||
)
|
||||
mainHeating2: Optional[Heating] = Relationship()
|
||||
|
||||
# Foreign key to HeatingType
|
||||
secondary_heating_type_id: Optional[uuid.UUID] = Field(
|
||||
default=None,
|
||||
foreign_key="heatingtype.id"
|
||||
)
|
||||
|
||||
secondaryHeatingType: Optional[HeatingType] = Relationship()
|
||||
|
||||
class Insulation(BaseModel):
|
||||
type: str
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue