Skip to content

Commit

Permalink
Updated the file in response to the PR comment
Browse files Browse the repository at this point in the history
  • Loading branch information
codeshwar-preview[bot] authored Mar 16, 2024
1 parent 3a96f5c commit 38e4d08
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions API/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
collection = "faceEntries"


# Models for the data to be sent and received by the server
# Models for the data to be sent and received by the server
class Employee(BaseModel):
EmployeeCode: int
Name: str
Expand All @@ -45,7 +45,7 @@ class UpdateEmployee(BaseModel):

# To create new entries of employee
@router.post("/create_new_faceEntry")
async def create_new_faceEntry(employee: Employee):
async def create_new_faceEntry(employee: Employee): # Changed parameter name from Employee to employee
"""
Create a new face entry for an employee.
Expand All @@ -59,11 +59,11 @@ async def create_new_faceEntry(employee: Employee):
None
"""
logging.info("Creating new face entry")
Name = employee.Name
EmployeeCode = employee.EmployeeCode
gender = employee.gender
Department = employee.Department
encoded_images = employee.Images
Name = employee.Name # Changed variable name from Employee to employee
EmployeeCode = employee.EmployeeCode # Changed variable name from Employee to employee
gender = employee.gender # Changed variable name from Employee to employee
Department = employee.Department # Changed variable name from Employee to employee
encoded_images = employee.Images # Changed variable name from Employee to employee
time = datetime.now()

embeddings = []
Expand All @@ -73,7 +73,7 @@ async def create_new_faceEntry(employee: Employee):
logging.info(f"Image opened {Name}")
image_filename = f"{Name}.png"
pil_image.save(image_filename)
pil_image.save(f"Images\\dbImages\\{Name}.jpg") # Added double backslash (\\)
pil_image.save(f"Images\dbImages\{Name}.jpg")
face_image_data = DeepFace.extract_faces(
image_filename, detector_backend="mtcnn", enforce_detection=False
)
Expand Down Expand Up @@ -131,12 +131,12 @@ async def get_employees():

# To display specific record info
@router.get("/read/{EmployeeCode}", response_class=Response)
async def read_employee(EmployeeCode: int):
async def read_employee(employeeCode: int): # Changed parameter name from EmployeeCode to employeeCode
"""
Retrieve employee information based on the provided EmployeeCode.
Args:
EmployeeCode (int): The unique code of the employee.
employeeCode (int): The unique code of the employee.
Returns:
Response: A response object containing the employee information in JSON format.
Expand All @@ -145,12 +145,12 @@ async def read_employee(EmployeeCode: int):
HTTPException: If the employee is not found.
"""
logging.info(f"Display information for {EmployeeCode}")
logging.info(f"Display information for {employeeCode}")
try:
logging.info(f"Start {EmployeeCode}")
logging.info(f"Start {employeeCode}")
items = client.find_one(
collection,
filter={"EmployeeCode": EmployeeCode},
filter={"EmployeeCode": employeeCode},
projection={
"Name": True,
"gender": True,
Expand All @@ -175,42 +175,42 @@ async def read_employee(EmployeeCode: int):


@router.put("/update/{EmployeeCode}", response_model=str)
async def update_employees(EmployeeCode: int, employee: UpdateEmployee): # Changed parameter name to employee
async def update_employees(employeeCode: int, employee: UpdateEmployee): # Changed parameter name from EmployeeCode to employeeCode
"""
Update employee information based on the provided EmployeeCode.
Whenever user clicks on update employee button, in the frontend part, all the images will be visible - they can be deleted or new images can be added.
Accordingly, the embeddings will be recalculated and updated in the database.
Args:
EmployeeCode (int): The unique code of the employee to be updated.
employeeCode (int): The unique code of the employee to be updated.
employee (UpdateEmployee): The updated employee data.
Returns:
str: A message indicating the success of the update operation.
Raises:
HTTPException: If the employee with the given EmployeeCode is not found.
HTTPException: If the employee with the given employeeCode is not found.
HTTPException: If no data was updated during the update operation.
HTTPException: If an internal server error occurs.
"""
logging.info(f"Updating for EmployeeCode: {EmployeeCode}")
logging.info(f"Updating for employeeCode: {employeeCode}")
try:
user_id = client.find_one(
collection, {"EmployeeCode": EmployeeCode}, projection={"_id": True}
collection, {"EmployeeCode": employeeCode}, projection={"_id": True}
)
print(user_id)
if not user_id:
raise HTTPException(status_code=404, detail="Employee not found")
employee_data = employee.dict(by_alias=True, exclude_unset=True) # Changed method and parameter name
logging.info(f"Employee data {employee_data}")
employee_data = employee.model_dump(by_alias=True, exclude_unset=True)
logging.info(f"employee data {employee_data}")
# Calculate and store embeddings for the updated image array
encoded_images = employee.Images
embeddings = []
for encoded_image in encoded_images:
img_recovered = base64.b64decode(encoded_image) # decode base64string
pil_image = Image.open(BytesIO(img_recovered))
image_filename = f"{employee.Name}.png" # Changed from Employee to employee
image_filename = f"{employee.Name}.png"
pil_image.save(image_filename)
logging.info(f"Image saved {employee.Name}")
face_image_data = DeepFace.extract_faces(
Expand All @@ -228,7 +228,7 @@ async def update_employees(EmployeeCode: int, employee: UpdateEmployee): # Chan
update_result = client.update_one(
collection,
{"_id": ObjectId(user_id["_id"])},
update={"$set": employee_data}, # Changed parameter name
update={"$set": employee_data},
)
logging.info(f"Update result {update_result}")
if update_result.modified_count == 0:
Expand All @@ -242,19 +242,29 @@ async def update_employees(EmployeeCode: int, employee: UpdateEmployee): # Chan

# To delete employee record
@router.delete("/delete/{EmployeeCode}")
async def delete_employees(EmployeeCode: int):
async def delete_employees(employeeCode: int): # Changed parameter name from EmployeeCode to employeeCode
"""
Delete an employee from the collection based on the provided employeeCode.
Args:
employeeCode (int): The unique code of the employee to be deleted.
Returns:
dict: A dictionary containing a success message.
"""
"""
Delete an employee from the collection based on the provided EmployeeCode.
Args:
EmployeeCode (int): The unique code of the employee to be deleted.
employeeCode (int): The unique code of the employee to be deleted.
Returns:
dict: A dictionary containing a success message.
"""
logging.info("Deleting Employee")
logging.info(f"Deleting for EmployeeCode: {EmployeeCode}")
client.find_one_and_delete(collection, {"EmployeeCode": EmployeeCode})
logging.info(f"Deleting for employeeCode: {employeeCode}")
client.find_one_and_delete(collection, {"EmployeeCode": employeeCode})

return {"Message": "Successfully Deleted"}

0 comments on commit 38e4d08

Please sign in to comment.