Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File Upload, Part 2: Update File Delete/Update endpoints #251

Open
wants to merge 2 commits into
base: ryan-jamie/file-upload
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion backend/python/app/rest/intake_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ def delete_intake():
return jsonify({"error": "intake_id query parameter must be an int"}), 400
else:
try:
pdf_file_id = intake_service.get_intake_by_id(intake_id).court_order_file_id
intake_service.delete_intake(intake_id)
file_storage_service.delete_file(pdf_file_id)
return "intake deleted", 200
except Exception as e:
error_message = getattr(e, "message", None)
Expand All @@ -459,11 +461,24 @@ def delete_intake():
400,
)


@blueprint.route("/<int:intake_id>", methods=["PUT"], strict_slashes=False)
def update_intake_route(intake_id):
try:
updated_data = request.json

# if request.files contains a file, then update the file
if "courtInformation[orderReferral]" in request.files:
file = request.files["courtInformation[orderReferral]"]
pdf_file = {
"file_name": file.filename,
"file_data": file.read(),
}
try:
pdf_file = CreatePdfFileDTO(**pdf_file)
file_storage_service.update_file(intake_id, pdf_file)
except Exception as error:
return jsonify(str(error)), 400

updated_intake = intake_service.update_intake(intake_id, updated_data)
return jsonify(updated_intake.__dict__), 200

Expand Down
58 changes: 35 additions & 23 deletions backend/python/app/services/implementations/file_storage_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,42 @@ def create_file(self, file: CreatePdfFileDTO):
db.session.rollback()
raise error

def update_file(self, file_name, file, content_type=None):
current_blob = self.bucket.get_blob(file_name)
if not current_blob:
raise Exception("File name {name} does not exist".format(name=file_name))
blob = self.bucket.blob(file_name)
def update_file(self, pdf_file_id: int, new_file: CreatePdfFileDTO):
try:
blob.upload_from_file(file, content_type=content_type)
except Exception as e:
reason = getattr(e, "message", None)
self.logger.error(
"Failed to update file {name}. Reason = {reason}".format(
name=file_name, reason=(reason if reason else str(e))
if not new_file:
raise Exception(
"Empty file DTO/None passed to update_file function"
)
)
raise e
if not isinstance(new_file, CreatePdfFileDTO):
raise Exception("File passed is not of CreatePdfFileDTO type")
if not pdf_file_id:
raise Exception("Empty PDF file id passed to update_file function")
if not isinstance(pdf_file_id, int):
raise Exception("Intake id passed is not of int type")
# replace file at pdf_file_id with new_file
file = PdfFile.query.filter_by(id=pdf_file_id).first()
if not file:
raise Exception("File with id {} not found".format(pdf_file_id))
file.file_name = new_file.file_name
file.file_data = new_file.file_data
db.session.commit()
return PdfFileDTO(**file.to_dict())
except Exception as error:
db.session.rollback()
raise error

def delete_file(self, file_name):
def delete_file(self, pdf_file_id: int):
try:
self.bucket.delete_blob(file_name)
except Exception as e:
reason = getattr(e, "message", None)
self.logger.error(
"Failed to delete file {name}. Reason = {reason}".format(
name=file_name, reason=(reason if reason else str(e))
)
)
raise e
if not pdf_file_id:
raise Exception("Empty PDF file id passed to delete_file function")
if not isinstance(pdf_file_id, int):
raise Exception("Intake id passed is not of int type")

file = PdfFile.query.filter_by(id=pdf_file_id).first()
if not file:
raise Exception("File with id {} not found".format(pdf_file_id))
db.session.delete(file)
db.session.commit()
except Exception as error:
db.session.rollback()
raise error
14 changes: 14 additions & 0 deletions backend/python/app/services/implementations/intake_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,17 @@ def search_intake_family_name(self, family_name: str):
except Exception as error:
self.logger.error(str(error))
raise error

def get_intake_by_id(self, intake_id: int):
try:
if not intake_id:
raise Exception("Empty intake id passed to get_intake_by_id function")
if not isinstance(intake_id, int):
raise Exception("Intake id passed is not of int type")
intake = Intake.query.filter_by(id=intake_id).first()
if not intake:
raise Exception("Intake with id {} not found".format(intake_id))
return IntakeDTO(**intake.to_dict())
except Exception as error:
self.logger.error(str(error))
raise error