Skip to content

Commit

Permalink
251 fertiscan as a dev i want upload date and updated at to be includ…
Browse files Browse the repository at this point in the history
…ed in inspection for consistency (#259)

* Issue #252: updated_at added to the Inspection model

* Issue #252: fix lint
  • Loading branch information
Francois-Werbrouck authored Feb 27, 2025
1 parent d13a2c3 commit 78792cb
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 226 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
**/__pycache__/**
.vscode/settings.json
.vscode/**
test/**
.env
0.1.3.json
ml_structure.json
.DS_Store
.venv
venv
.vscode/extensions.json
15 changes: 11 additions & 4 deletions fertiscan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,14 @@ def update_inspection(
"The inspection you are trying to update is already verified"
)
else:
inspection.update_inspection(
updated_at = inspection.update_inspection(
cursor=cursor,
inspection_id=updated_data.inspection_id,
verified=updated_data.verified,
inspection_comment=updated_data.inspection_comment,
)
updated_data.updated_at = updated_at
self.model.updated_at = updated_at # just in case
# --------
label_to_update = updated_data.product
label_id = updated_data.product.label_id
Expand Down Expand Up @@ -252,7 +254,7 @@ def update_inspection(
"'Main contact organization information is required and was not found'"
)
else:
organization.upsert_organization(
organization_id = organization.upsert_organization(
cursor=cursor,
name=main_org.name,
website=main_org.website,
Expand All @@ -264,12 +266,13 @@ def update_inspection(
cursor=cursor,
name=fertilizer_name,
reg_number=registration_number_value,
org_owner_id=main_org.id,
org_owner_id=organization_id,
latest_inspection_id=updated_data.inspection_id,
)
else:
updated_data.verified = False
# ------------
updated_data.updated_at = updated_at
return data_inspection.Inspection.model_validate(updated_data)

async def delete_inspection(
Expand Down Expand Up @@ -528,17 +531,21 @@ def new_inspection(
)
if flag == True:
# We do this since we have no way of knowing who is the main contact
record.is_main_contact = True # We assume the first one is the main contact
flag = False

# Inspection
formatted_analysis.inspection_id = inspection.new_inspection(
inspection_id, upload_date = inspection.new_inspection(
cursor=cursor,
user_id=user_id,
picture_set_id=folder_id,
verified=False,
label_id=label_info_id,
container_id=container_id,
)
formatted_analysis.inspection_id = inspection_id
formatted_analysis.upload_date = upload_date
formatted_analysis.updated_at = upload_date
analysis_db = data_inspection.Inspection.model_validate(formatted_analysis)
inspection.save_inspection_original_dataset(
cursor=cursor,
Expand Down
10 changes: 8 additions & 2 deletions fertiscan/db/metadata/inspection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ class Inspection(ValidatedModel):
ingredients: ValuesObjects
folder_id: UUID4
container_id: UUID4
upload_date: Optional[datetime] = None
updated_at: Optional[datetime] = None



Expand Down Expand Up @@ -355,7 +357,9 @@ def build_inspection_import(analysis_form: dict, user_id:UUID,folder_id:UUID,con
ingredients=ingredients,
folder_id=folder_id,
container_id=container_id,
inspection_comment= None
inspection_comment= None,
upload_date=None,
updated_at=None,
)
return inspection_formatted
except MetadataError:
Expand Down Expand Up @@ -441,7 +445,9 @@ def build_inspection_export(cursor, inspection_id) -> Inspection:
verified=db_inspection.verified,
ingredients=ingredients,
folder_id=folder_id,
container_id=container_id
container_id=container_id,
upload_date=db_inspection.upload_date,
updated_at=db_inspection.updated_at,
)
return inspection_formatted
except QueryError as e:
Expand Down
19 changes: 12 additions & 7 deletions fertiscan/db/queries/inspection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import json
from uuid import UUID
from datetime import datetime as Date
from datetime import datetime

from psycopg import Cursor
from psycopg.rows import dict_row
Expand All @@ -25,7 +25,7 @@
@handle_query_errors(InspectionCreationError)
def new_inspection(
cursor: Cursor, user_id, picture_set_id, label_id, container_id, verified=False
):
) -> tuple[UUID, datetime]:
"""
This function uploads a new inspection to the database.
Expand All @@ -50,11 +50,11 @@ def new_inspection(
VALUES
(%s, %s, %s,%s,%s)
RETURNING
id
id, upload_date;
"""
cursor.execute(query, (user_id, picture_set_id, verified, label_id, container_id))
if result := cursor.fetchone():
return result[0]
return result
raise InspectionCreationError("Failed to create inspection. No data returned.")


Expand Down Expand Up @@ -384,7 +384,7 @@ def get_all_organization_inspection(cursor: Cursor, org_id):

def update_inspection(
cursor: Cursor, inspection_id: str | UUID, verified: bool, inspection_comment: str
):
) -> datetime:
if verified:
query = """
UPDATE
Expand All @@ -395,7 +395,9 @@ def update_inspection(
inspection_comment = %s,
verified_date = CURRENT_TIMESTAMP
WHERE
id = %s;
id = %s
RETURNING
updated_at;
"""
else:
query = """
Expand All @@ -406,9 +408,12 @@ def update_inspection(
updated_at = CURRENT_TIMESTAMP,
inspection_comment = %s
WHERE
id = %s;
id = %s
RETURNING
updated_at;
"""
cursor.execute(query, (verified, inspection_comment, inspection_id))
return cursor.fetchone()[0]


@handle_query_errors(InspectionUpdateError)
Expand Down
2 changes: 1 addition & 1 deletion fertiscan/db/queries/organization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def upsert_organization(cursor: Cursor, name:str, website:str, phone_number: str
"""
cursor.execute(query, (name,))
res = cursor.fetchone()
if res[0] is None:
if res is None or res[0] is None:
id =new_organization(
cursor=cursor,
name=name,
Expand Down
Loading

0 comments on commit 78792cb

Please sign in to comment.