Skip to content

Commit

Permalink
Issue #233: Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Francois-Werbrouck committed Feb 27, 2025
1 parent b6a57fc commit 96c67ec
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 127 deletions.
88 changes: 46 additions & 42 deletions fertiscan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,63 +600,68 @@ def get_user_analysis_by_verified(cursor: Cursor, user_id: UUID, verified: bool)
if not user.is_a_user_id(cursor=cursor, user_id=user_id):
raise user.UserNotFoundError(f"User not found based on the given id: {user_id}")
return inspection.get_all_user_inspection_filter_verified(cursor, user_id, verified)


def search_inspection(
cursor:Cursor,
fertilizer_name:str,
reg_number:str,
lot_number:str,
inspector_name:str,
date_of_inspection:str,
organization_name:str,
organization_address:str,
organization_phone:str,
cursor: Cursor,
fertilizer_name: str,
reg_number: str,
lot_number: str,
inspector_name: str,
date_of_inspection: str,
organization_name: str,
organization_address: str,
organization_phone: str,
):
"""
This function search all the verified inspection based on the given parameters
Parameters:
- cursor: The cursor object to interact with the database.
- fertilizer_name: The name of the fertilizer.
- registration_number: The registration number of the fertilizer.
- lot_number: The lot number of the fertilizer.
- inspector_name: The name of the inspector.
- date_of_inspection: The date of the inspection.
- organization_name: The name of the organization.
- organization_address: The address of the organization.
- organization_phone: The phone number of the organization.
Returns:
- List of inspection.
[
inspection.id,
inspection.upload_date,
inspection.updated_at,
inspection.sample_id, -- Not used at the moment
inspection.picture_set_id,
label_info.id as label_info_id,
label_info.product_name,
label_info.company_info_id,
label_info.manufacturer_info_id
company_info.id as company_info_id,
company_info.company_name
]
"""
This function search all the verified inspection based on the given parameters
Parameters:
- cursor: The cursor object to interact with the database.
- fertilizer_name: The name of the fertilizer.
- registration_number: The registration number of the fertilizer.
- lot_number: The lot number of the fertilizer.
- inspector_name: The name of the inspector.
- date_of_inspection: The date of the inspection.
- organization_name: The name of the organization.
- organization_address: The address of the organization.
- organization_phone: The phone number of the organization.
Returns:
- List of inspection.
[
inspection.id,
inspection.upload_date,
inspection.updated_at,
inspection.sample_id, -- Not used at the moment
inspection.picture_set_id,
label_info.id as label_info_id,
label_info.product_name,
label_info.company_info_id,
label_info.manufacturer_info_id
company_info.id as company_info_id,
company_info.company_name
]
"""
label_ids = []
# search based on Organization info
if organization_name is not None or organization_address is not None or organization_phone is not None:
if (
organization_name is not None
or organization_address is not None
or organization_phone is not None
):
orgs = organization.search_organization_information(
cursor=cursor,
name=organization_name,
address=organization_address,
phone_number=organization_phone,
website=None
website=None,
)
if org is not None and len(orgs) > 0:
for org in orgs:
label_ids.append(org[1])
# search based on registration number
if reg_number is not None and reg_number.strip() == "":
reg_result = registration_number.search_registration_number(
cursor=cursor,
registration_number=reg_number
cursor=cursor, registration_number=reg_number
)
if reg_result is not None and len(reg_result) > 0:
for reg in reg_result:
Expand All @@ -668,6 +673,5 @@ def search_inspection(
lot_number=lot_number,
inspector_name=inspector_name,
date_of_inspection=date_of_inspection,
organization_info=orgs
organization_info=orgs,
)

67 changes: 39 additions & 28 deletions fertiscan/db/queries/inspection/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@


@handle_query_errors(InspectionCreationError)
def new_inspection(cursor: Cursor, user_id, picture_set_id, label_id,container_id,verified=False):
def new_inspection(
cursor: Cursor, user_id, picture_set_id, label_id, container_id, verified=False
):
"""
This function uploads a new inspection to the database.
Expand All @@ -50,12 +52,13 @@ def new_inspection(cursor: Cursor, user_id, picture_set_id, label_id,container_i
RETURNING
id
"""
cursor.execute(query, (user_id, picture_set_id, verified,label_id,container_id))
cursor.execute(query, (user_id, picture_set_id, verified, label_id, container_id))
if result := cursor.fetchone():
return result[0]
raise InspectionCreationError("Failed to create inspection. No data returned.")

def save_inspection_original_dataset(cursor: Cursor, inspection_id:UUID,og_data):

def save_inspection_original_dataset(cursor: Cursor, inspection_id: UUID, og_data):
query = """
UPDATE
"fertiscan_0.1.1".inspection_factual
Expand All @@ -64,7 +67,8 @@ def save_inspection_original_dataset(cursor: Cursor, inspection_id:UUID,og_data)
WHERE
inspection_factual."inspection_id" = %s;
"""
cursor.execute(query,(og_data,inspection_id))
cursor.execute(query, (og_data, inspection_id))


@handle_query_errors(InspectionCreationError)
def new_inspection_with_label_info(cursor: Cursor, user_id, picture_set_id, label_json):
Expand Down Expand Up @@ -377,11 +381,9 @@ def get_all_organization_inspection(cursor: Cursor, org_id):
cursor.execute(query, (org_id, org_id))
return cursor.fetchall()


def update_inspection(
cursor: Cursor,
inspection_id: str | UUID,
verified: bool,
inspection_comment:str
cursor: Cursor, inspection_id: str | UUID, verified: bool, inspection_comment: str
):
if verified:
query = """
Expand All @@ -396,7 +398,7 @@ def update_inspection(
id = %s;
"""
else:
query = """
query = """
UPDATE
inspection
SET
Expand All @@ -406,15 +408,12 @@ def update_inspection(
WHERE
id = %s;
"""
cursor.execute(query, (verified,inspection_comment,inspection_id))
cursor.execute(query, (verified, inspection_comment, inspection_id))


@handle_query_errors(InspectionUpdateError)
def update_inspection_function(
cursor: Cursor,
inspection_id: str | UUID,
user_id: str | UUID,
updated_data_dict
cursor: Cursor, inspection_id: str | UUID, user_id: str | UUID, updated_data_dict
) -> dict:
"""
Update inspection data in the database.
Expand Down Expand Up @@ -500,7 +499,15 @@ def get_inspection_factual(cursor: Cursor, inspection_id):
cursor.execute(query, (inspection_id,))
return cursor.fetchone()

def search_inspection(cursor: Cursor, fertilizer_name:str, lower_bound_date: Date, upper_bound_date: Date, lot_number:str, label_ids:list[UUID]) -> list:

def search_inspection(
cursor: Cursor,
fertilizer_name: str,
lower_bound_date: Date,
upper_bound_date: Date,
lot_number: str,
label_ids: list[UUID],
) -> list:
"""
Find all inspections where the organization is listed as main contact.
Expand All @@ -511,7 +518,7 @@ def search_inspection(cursor: Cursor, fertilizer_name:str, lower_bound_date: Dat
- upper_bound_date (Date): The upper bound date of the inspection
- lot_number (str): The lot number of the fertilizer
- label_ids (list[UUID]): The list of label IDs to search also search for regarless of other parameters
Returns:
- list: List of tuples containing inspection data
"""
Expand Down Expand Up @@ -548,18 +555,23 @@ def search_inspection(cursor: Cursor, fertilizer_name:str, lower_bound_date: Dat
first = True
params = ()
# check if all parameters are not none
if ((fertilizer_name is None or fertilizer_name.strip() == "") and
(lower_bound_date is None) and
(upper_bound_date is None) and
(lot_number is None or lot_number.strip() == "") and
(label_ids is None or len(label_ids) < 1)
if (
(fertilizer_name is None or fertilizer_name.strip() == "")
and (lower_bound_date is None)
and (upper_bound_date is None)
and (lot_number is None or lot_number.strip() == "")
and (label_ids is None or len(label_ids) < 1)
):
raise InspectionQueryError("No search parameters provided, please provide at least one search parameter.")
raise InspectionQueryError(
"No search parameters provided, please provide at least one search parameter."
)
# Check if the dates are valid
if lower_bound_date is not None and upper_bound_date is not None:
if lower_bound_date > upper_bound_date:
raise InspectionQueryError("The lower bound date is greater than the upper bound date.")

raise InspectionQueryError(
"The lower bound date is greater than the upper bound date."
)

if fertilizer_name is not None and fertilizer_name.strip() != "":
if first:
query += "WHERE "
Expand All @@ -581,7 +593,7 @@ def search_inspection(cursor: Cursor, fertilizer_name:str, lower_bound_date: Dat
query += "WHERE "
else:
query += "AND "
query += "DATE(i.upload_date) <= DATE(%s) "
query += "DATE(i.upload_date) <= DATE(%s) "
first = False
# Make sure upper_bound_date time is 23:59:59 to include the whole day
upper_bound_date = upper_bound_date.replace(hour=23, minute=59, second=59)
Expand All @@ -596,13 +608,13 @@ def search_inspection(cursor: Cursor, fertilizer_name:str, lower_bound_date: Dat
params += (lot_number,)
if label_ids is not None and len(label_ids) > 0:
if first:
query += "WHERE " # This is a list for previous conditions that were met
query += "WHERE " # This is a list for previous conditions that were met
else:
query += "OR "
query += "l.id = ANY(%s) "
first = False
params += (label_ids,)

# Aggregate the Registration Numbers
query += """
GROUP BY
Expand All @@ -628,4 +640,3 @@ def search_inspection(cursor: Cursor, fertilizer_name:str, lower_bound_date: Dat
query += ";"
cursor.execute(query, params)
return cursor.fetchall()

Loading

0 comments on commit 96c67ec

Please sign in to comment.