Skip to content

Commit b95cb02

Browse files
committed
Refactor comment and flag services for improved readability and error handling
- Removed unused import from `comment_service.py` to streamline the code. - Enhanced readability in `flag_service.py` by formatting conditional statements and comments for better clarity. - Updated error handling in `flag_service.py` to ensure consistent behavior when dealing with DataAPIResponseException. - Improved formatting in `db_helpers.py` for better readability of exception handling logic.
1 parent 8bbb0ed commit b95cb02

3 files changed

Lines changed: 26 additions & 13 deletions

File tree

app/services/comment_service.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from app.services import video_service, user_service
1515
from app.external_services.sentiment_mock import MockSentimentAnalyzer
1616
import inspect # local import to avoid new dependency
17-
from astrapy.exceptions.data_api_exceptions import DataAPIResponseException
1817
from app.utils.db_helpers import safe_count
1918

2019
# testing mocks

app/services/flag_service.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,22 @@ def _to_flag_model(doc: dict) -> Flag:
5353
return Flag(
5454
flagId=UUID(flag_id) if flag_id else uuid4(),
5555
userId=UUID(user_id_raw) if user_id_raw else UUID(int=0),
56-
contentType=ContentTypeEnum(content_type) if content_type else ContentTypeEnum.VIDEO,
56+
contentType=ContentTypeEnum(content_type)
57+
if content_type
58+
else ContentTypeEnum.VIDEO,
5759
contentId=UUID(content_id) if content_id else UUID(int=0),
5860
reasonCode=reason_code or "other",
5961
reasonText=reason_text,
60-
createdAt=norm.get("createdat") or norm.get("review_date") or datetime.now(timezone.utc),
61-
updatedAt=norm.get("updatedat") or norm.get("review_date") or datetime.now(timezone.utc),
62+
createdAt=norm.get("createdat")
63+
or norm.get("review_date")
64+
or datetime.now(timezone.utc),
65+
updatedAt=norm.get("updatedat")
66+
or norm.get("review_date")
67+
or datetime.now(timezone.utc),
6268
status=FlagStatusEnum(norm.get("status", "open")),
63-
moderatorId=UUID(norm["moderatorid"]) if norm.get("moderatorid") else (
64-
UUID(norm["reviewer"]) if norm.get("reviewer") else None
65-
),
69+
moderatorId=UUID(norm["moderatorid"])
70+
if norm.get("moderatorid")
71+
else (UUID(norm["reviewer"]) if norm.get("reviewer") else None),
6672
moderatorNotes=norm.get("moderatornotes"),
6773
resolvedAt=norm.get("resolvedat") or norm.get("review_date"),
6874
)
@@ -142,7 +148,7 @@ async def create_flag(
142148
# Attempt to insert the document. If the **flags** collection has not
143149
# been created yet Astra will return a ``COLLECTION_NOT_EXIST`` error.
144150
# Instead of surfacing a 500 to the caller we create the collection on
145-
#-the-fly and retry once. This mirrors the graceful handling already
151+
# -the-fly and retry once. This mirrors the graceful handling already
146152
# implemented in ``list_flags``.
147153
# ------------------------------------------------------------------
148154

@@ -153,7 +159,9 @@ async def create_flag(
153159
# in environments (e.g. CI) that rely on the stub client.
154160
from astrapy.exceptions.data_api_exceptions import DataAPIResponseException # type: ignore
155161

156-
if isinstance(exc, DataAPIResponseException) and "COLLECTION_NOT_EXIST" in str(exc):
162+
if isinstance(exc, DataAPIResponseException) and "COLLECTION_NOT_EXIST" in str(
163+
exc
164+
):
157165
# Lazily create the collection and retry the insert exactly once.
158166
db = await get_astra_db()
159167

@@ -168,7 +176,9 @@ async def create_flag(
168176

169177
db_table = await get_table(CONTENT_MOD_TABLE_NAME)
170178
await db_table.insert_one(document=doc)
171-
elif isinstance(exc, DataAPIResponseException) and "UNKNOWN_TABLE_COLUMNS" in str(exc):
179+
elif isinstance(
180+
exc, DataAPIResponseException
181+
) and "UNKNOWN_TABLE_COLUMNS" in str(exc):
172182
# Strip any keys not in the table schema and retry once.
173183
allowed_cols = {
174184
"contentid",
@@ -346,7 +356,9 @@ async def action_on_flag(
346356
update_payload_model = {
347357
"status": new_status,
348358
"moderatorId": moderator.userId,
349-
"resolvedAt": now if new_status in {FlagStatusEnum.APPROVED, FlagStatusEnum.REJECTED} else None,
359+
"resolvedAt": now
360+
if new_status in {FlagStatusEnum.APPROVED, FlagStatusEnum.REJECTED}
361+
else None,
350362
"updatedAt": now,
351363
"moderatorNotes": moderator_notes,
352364
}

app/utils/db_helpers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ async def safe_count(
3636
try:
3737
return await db_table.count_documents(filter=query_filter, upper_bound=10**9)
3838
except (TypeError, DataAPIResponseException) as exc: # pragma: no cover – fallback
39-
if isinstance(exc, DataAPIResponseException) and "UNSUPPORTED_TABLE_COMMAND" not in str(exc):
39+
if isinstance(
40+
exc, DataAPIResponseException
41+
) and "UNSUPPORTED_TABLE_COMMAND" not in str(exc):
4042
# An unexpected Data API error – surface to caller.
4143
raise
42-
return fallback_len
44+
return fallback_len

0 commit comments

Comments
 (0)