-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
## Summary Fixes #3322 ### Time to review: __10 mins__ ## Changes proposed A lot of file utilities (used in this PR) for handling reading/writing/naming files on s3 Utility for setting up s3 file paths for the attachments Logic to handle inserts/updates/deletes of attachments and the files that need to move around on s3. ## Context for reviewers There are some scenarios I haven't accounted for yet when the opportunity itself is modified (deleted / is no longer a draft), I originally wanted to handle this in a single PR, but I'll split that out as this one already was getting too big. See the ticket for details on the scenarios we need to handle. ## Additional information Testing this is a bit tedious - there is a lot that needs to be setup exactly to test it. I'd recommend nuking anything you already have with `make volume-recreate` Set the env var to enable the job to run (add `TRANSFORM_ORACLE_DATA_ENABLE_OPPORTUNITY_ATTACHMENT=1` to override.env) Run `make console` and in that do `f.StagingTsynopsisAttachmentFactory.create_batch(size=50)` and then `exit()` Finally you can run the job by doing `make cmd args="data-migration load-transform --no-load --transform --no-set-current"`
- Loading branch information
Showing
18 changed files
with
716 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
60 changes: 60 additions & 0 deletions
60
api/src/services/opportunity_attachments/attachment_util.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import re | ||
|
||
from src.adapters.aws import S3Config | ||
from src.db.models.opportunity_models import Opportunity | ||
from src.util import file_util | ||
|
||
|
||
def get_s3_attachment_path( | ||
file_name: str, opportunity_attachment_id: int, opportunity: Opportunity, s3_config: S3Config | ||
) -> str: | ||
"""Construct a path to the attachments on s3 | ||
Will be formatted like: | ||
s3://<bucket>/opportunities/<opportunity_id>/attachments/<attachment_id>/<file_name> | ||
Note that we store the files under a "folder" with the attachment ID as | ||
the legacy system doesn't guarantee file names are unique within an opportunity. | ||
""" | ||
|
||
base_path = ( | ||
s3_config.draft_files_bucket_path | ||
if opportunity.is_draft | ||
else s3_config.public_files_bucket_path | ||
) | ||
|
||
return file_util.join( | ||
base_path, | ||
"opportunities", | ||
str(opportunity.opportunity_id), | ||
"attachments", | ||
str(opportunity_attachment_id), | ||
file_name, | ||
) | ||
|
||
|
||
def adjust_legacy_file_name(existing_file_name: str) -> str: | ||
"""Correct the file names to remove any characters problematic for URL/s3 processing. | ||
We only keep the following characters: | ||
* A-Z | ||
* a-z | ||
* 0-9 | ||
* _ | ||
* - | ||
* ~ | ||
* . | ||
Whitespace will be replaced with underscores. | ||
All other characters will be removed. | ||
""" | ||
|
||
# Replace one-or-more whitespace with a single underscore | ||
file_name = re.sub(r"\s+", "_", existing_file_name) | ||
|
||
# Remove all non-accepted characters | ||
file_name = re.sub(r"[^a-zA-Z0-9_.\-~]", "", file_name) | ||
|
||
return file_name |
Oops, something went wrong.