Cleanup/refactor of do_initial_post returns

This commit is contained in:
Liam Steckler 2025-07-10 16:07:40 -07:00
parent ee17ec7a55
commit 11f85b565d

60
scl.py
View file

@ -260,7 +260,7 @@ def do_initial_post(
start_time: datetime, start_time: datetime,
estimated_restoration_time: datetime, estimated_restoration_time: datetime,
outage_geometries: shapely.MultiPolygon, outage_geometries: shapely.MultiPolygon,
) -> dict[str, str | None]: ) -> str:
post_id = None post_id = None
map_media_post = None map_media_post = None
area_text = str() area_text = str()
@ -311,7 +311,7 @@ Cause: {}
) )
post_id = post_result["id"] post_id = post_result["id"]
return {"post_id": post_id, "map_media_post_id": None} return post_id
class Base(DeclarativeBase): class Base(DeclarativeBase):
@ -326,7 +326,6 @@ class SclOutage(Base):
outage_user_id: Mapped[str] = mapped_column() outage_user_id: Mapped[str] = mapped_column()
most_recent_post_id: Mapped[Optional[str]] = mapped_column() most_recent_post_id: Mapped[Optional[str]] = mapped_column()
initial_post_id: Mapped[Optional[str]] = mapped_column() initial_post_id: Mapped[Optional[str]] = mapped_column()
map_media_post_id: Mapped[Optional[str]] = mapped_column()
last_updated_time: Mapped[datetime] = mapped_column() last_updated_time: Mapped[datetime] = mapped_column()
estimated_restoration_time: Mapped[datetime] = mapped_column() estimated_restoration_time: Mapped[datetime] = mapped_column()
cause: Mapped[str] = mapped_column() cause: Mapped[str] = mapped_column()
@ -341,7 +340,7 @@ class SclOutage(Base):
geometries_modified: Mapped[Optional[bool]] = mapped_column() geometries_modified: Mapped[Optional[bool]] = mapped_column()
def __repr__(self) -> str: def __repr__(self) -> str:
return f"SclOutage(scl_outage_id={self.scl_outage_id!r}, most_recent_post_id={self.most_recent_post_id!r}, initial_post_id={self.initial_post_id!r}, map_media_post_id={self.map_media_post_id!r}, last_updated_time={self.last_updated_time!r}, no_longer_in_response_time={self.no_longer_in_response_time!r}, start_time={self.start_time!r}, num_people={self.num_people!r}, max_num_people={self.max_num_people!r}, neighborhood={self.neighborhood!r}, city={self.city!r}, outage_geometries={self.outage_geometries!r}, geometries_modified={self.geometries_modified!r})" return f"SclOutage(scl_outage_id={self.scl_outage_id!r}, most_recent_post_id={self.most_recent_post_id!r}, initial_post_id={self.initial_post_id!r}, last_updated_time={self.last_updated_time!r}, no_longer_in_response_time={self.no_longer_in_response_time!r}, start_time={self.start_time!r}, num_people={self.num_people!r}, max_num_people={self.max_num_people!r}, neighborhood={self.neighborhood!r}, city={self.city!r}, outage_geometries={self.outage_geometries!r}, geometries_modified={self.geometries_modified!r})"
engine = create_engine("sqlite:///scl.db") engine = create_engine("sqlite:///scl.db")
@ -458,30 +457,28 @@ with Session(engine) as session:
print( print(
"Posting an event that grew above the threshold required to post" "Posting an event that grew above the threshold required to post"
) )
initial_post_result = do_initial_post( post_id = do_initial_post(
event, event,
event_class, event_class,
start_time, start_time,
estimated_restoration_time, estimated_restoration_time,
outage_geometries, outage_geometries,
) )
try: # TODO: It doesn't look like these would have been set- where?
existing_record.neighborhood = initial_post_result[ # try:
"neighborhood" # existing_record.neighborhood = initial_post_result[
] # "neighborhood"
except KeyError: # ]
pass # except KeyError:
# pass
try: # try:
existing_record.city = initial_post_result["city"] # existing_record.city = initial_post_result["city"]
except KeyError: # except KeyError:
pass # pass
existing_record.initial_post_id = initial_post_result["post_id"] existing_record.initial_post_id = post_id
existing_record.most_recent_post_id = initial_post_result["post_id"] existing_record.most_recent_post_id = post_id
existing_record.map_media_post_id = initial_post_result[
"map_media_post_id"
]
else: else:
print("Existing record was found, and no properties were updated.") print("Existing record was found, and no properties were updated.")
session.commit() session.commit()
@ -489,7 +486,6 @@ with Session(engine) as session:
except NoResultFound: except NoResultFound:
print("Existing record not found") print("Existing record not found")
post_id = None post_id = None
map_media_post_id = None
neighborhood = None neighborhood = None
city = None city = None
if not event_class["is_postable"]: if not event_class["is_postable"]:
@ -499,32 +495,30 @@ with Session(engine) as session:
) )
) )
else: else:
initial_post_result = do_initial_post( post_id = do_initial_post(
event, event,
event_class, event_class,
start_time, start_time,
estimated_restoration_time, estimated_restoration_time,
outage_geometries, outage_geometries,
) )
post_id = initial_post_result["post_id"]
map_media_post_id = initial_post_result["map_media_post_id"]
try: # TODO: It doesn't look like these would have been set- where?
neighborhood = initial_post_result["neighborhood"] # try:
except KeyError: # neighborhood = initial_post_result["neighborhood"]
pass # except KeyError:
# pass
try: # try:
city = initial_post_result["city"] # city = initial_post_result["city"]
except KeyError: # except KeyError:
pass # pass
new_outage_record = SclOutage( new_outage_record = SclOutage(
scl_outage_id=event["id"], scl_outage_id=event["id"],
outage_user_id=event["identifier"], outage_user_id=event["identifier"],
most_recent_post_id=post_id, most_recent_post_id=post_id,
initial_post_id=post_id, initial_post_id=post_id,
map_media_post_id=map_media_post_id,
last_updated_time=last_updated_time, last_updated_time=last_updated_time,
estimated_restoration_time=estimated_restoration_time, estimated_restoration_time=estimated_restoration_time,
cause=event["cause"], cause=event["cause"],