Add endpoint for creating local contacts with access control
Introduced a new `local_contact_router` to handle creation of local contacts. The endpoint enforces role-based access control and ensures no duplication of email addresses. Updated the router exports for consistency and cleaned up a large test file to improve readability.
This commit is contained in:
@ -89,19 +89,19 @@ async def create_sample_event(
|
||||
return sample # Return the sample, now including `mount_count`
|
||||
|
||||
|
||||
@router.post("/samples/{sample_id}/upload-images")
|
||||
async def upload_sample_images(
|
||||
@router.post("/{sample_id}/upload-images")
|
||||
async def upload_sample_image(
|
||||
sample_id: int,
|
||||
uploaded_files: list[UploadFile] = File(...),
|
||||
uploaded_file: UploadFile = File(...),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
logging.info(f"Received files: {[file.filename for file in uploaded_files]}")
|
||||
logging.info(f"Received file: {uploaded_file.filename}")
|
||||
|
||||
"""
|
||||
Uploads images for a given sample and saves them to a directory structure.
|
||||
Uploads an image for a given sample and saves it to a directory structure.
|
||||
Args:
|
||||
sample_id (int): ID of the sample.
|
||||
uploaded_files (list[UploadFile]): A list of files uploaded with the request.
|
||||
uploaded_file (UploadFile): The file uploaded with the request.
|
||||
db (Session): Database session.
|
||||
"""
|
||||
|
||||
@ -123,35 +123,32 @@ async def upload_sample_images(
|
||||
base_dir = Path(f"images/{pgroup}/{today}/{dewar_name}/{puck_name}/{position}")
|
||||
base_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 3. Process and Save Each File
|
||||
saved_files = []
|
||||
for file in uploaded_files:
|
||||
# Validate MIME type
|
||||
if not file.content_type.startswith("image/"):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Invalid file type: {file.filename}. Only images are accepted.",
|
||||
)
|
||||
# 3. Validate MIME type and Save the File
|
||||
if not uploaded_file.content_type.startswith("image/"):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=f"Invalid file type: {uploaded_file.filename}."
|
||||
f" Only images are accepted.",
|
||||
)
|
||||
|
||||
# Save file to the base directory
|
||||
file_path = base_dir / file.filename
|
||||
file_path = base_dir / uploaded_file.filename
|
||||
logging.debug(f"Saving file {uploaded_file.filename} to {file_path}")
|
||||
|
||||
# Save the file from the file stream
|
||||
try:
|
||||
with file_path.open("wb") as buffer:
|
||||
shutil.copyfileobj(file.file, buffer)
|
||||
saved_files.append(str(file_path)) # Track saved file paths
|
||||
except Exception as e:
|
||||
logging.error(f"Error saving file {file.filename}: {str(e)}")
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=f"Could not save file {file.filename}."
|
||||
f" Ensure the server has correct permissions.",
|
||||
)
|
||||
try:
|
||||
with file_path.open("wb") as buffer:
|
||||
shutil.copyfileobj(uploaded_file.file, buffer)
|
||||
logging.info(f"File saved: {file_path}")
|
||||
except Exception as e:
|
||||
logging.error(f"Error saving file {uploaded_file.filename}: {str(e)}")
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail=f"Could not save file {uploaded_file.filename}."
|
||||
f" Ensure the server has correct permissions.",
|
||||
)
|
||||
|
||||
# 4. Return Saved Files Information
|
||||
logging.info(f"Uploaded {len(saved_files)} files for sample {sample_id}.")
|
||||
# 4. Return Saved File Information
|
||||
logging.info(f"Uploaded 1 file for sample {sample_id}.")
|
||||
return {
|
||||
"message": f"{len(saved_files)} images uploaded successfully.",
|
||||
"files": saved_files,
|
||||
"message": "1 image uploaded successfully.",
|
||||
"file": str(file_path),
|
||||
}
|
||||
|
Reference in New Issue
Block a user