-
Notifications
You must be signed in to change notification settings - Fork 11
fix: Robust Recursion Handling & Enhanced Migration Tools #109
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
Open
zerodiscount
wants to merge
1
commit into
agritheory:version-15
Choose a base branch
from
zerodiscount:pr-contribution
base: version-15
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -56,9 +56,11 @@ def validate(self) -> None: | |
| PATH: frappe/core/doctype/file/file.py | ||
| METHOD: validate | ||
| """ | ||
| self.associate_files() | ||
| if self.flags.cloud_storage or self.flags.ignore_file_validate: | ||
| return | ||
|
|
||
| self.associate_files() | ||
|
|
||
| if not self.is_remote_file: | ||
| self.custom_validate() | ||
| else: | ||
|
|
@@ -211,7 +213,13 @@ def associate_files( | |
| "file_association", | ||
| add_child_file_association(attached_to_doctype, attached_to_name), | ||
| ) | ||
| existing_file.save() | ||
| existing_file.flags.ignore_file_validate = True | ||
|
|
||
| if self.flags.ignore_links: existing_file.flags.ignore_links = True | ||
| if self.flags.ignore_permissions: existing_file.flags.ignore_permissions = True | ||
| if self.flags.ignore_validate: existing_file.flags.ignore_validate = True | ||
|
|
||
| existing_file.save(ignore_permissions=True) | ||
| else: | ||
| if self.file_association: | ||
| already_linked = any( | ||
|
|
@@ -395,10 +403,15 @@ def has_permission(doc, ptype: str | None = None, user: str | None = None) -> bo | |
| if doc.owner == user: | ||
| has_access = True | ||
| elif doc.attached_to_doctype and doc.attached_to_name: # type: ignore | ||
| reference_doc = frappe.get_doc(doc.attached_to_doctype, doc.attached_to_name) # type: ignore | ||
| has_access = reference_doc.has_permission() | ||
| if not has_access: | ||
| has_access = has_user_permission(doc, user) | ||
| try: | ||
| reference_doc = frappe.get_doc(doc.attached_to_doctype, doc.attached_to_name) # type: ignore | ||
| has_access = reference_doc.has_permission() | ||
| if not has_access: | ||
| has_access = has_user_permission(doc, user) | ||
| except frappe.DoesNotExistError: | ||
| # If attached document doesn't exist, check permission on the file itself | ||
| has_access = bool(frappe.has_permission(doc.doctype, ptype, user=user)) | ||
|
|
||
| # elif True: | ||
| # Check "shared with" including parent 'folder' to allow access | ||
| # ... | ||
|
|
@@ -439,11 +452,31 @@ def strip_special_chars(file_name: str) -> str: | |
| return regex.sub("", file_name) | ||
|
|
||
|
|
||
| def get_cloud_storage_config() -> dict: | ||
| config = frappe.conf.get("cloud_storage_settings", {}) | ||
|
|
||
| # If nested config is found and seems populated with at least access_key, use it. | ||
| if config and config.get("access_key"): | ||
| return config | ||
|
|
||
| # Otherwise, build from top-level standard keys | ||
| return { | ||
| "access_key": frappe.conf.get("s3_access_key"), | ||
| "secret": frappe.conf.get("s3_secret_key"), | ||
| "bucket": frappe.conf.get("s3_bucket"), | ||
| "region": frappe.conf.get("region"), | ||
| "endpoint_url": frappe.conf.get("endpoint_url"), | ||
| "folder": frappe.conf.get("s3_folder"), | ||
| "use_local": frappe.conf.get("use_local"), | ||
| "use_legacy_paths": frappe.conf.get("use_legacy_paths", True) | ||
| } | ||
|
|
||
|
|
||
| @frappe.whitelist() | ||
| def get_cloud_storage_client(): | ||
| validate_config() | ||
|
|
||
| config: dict = frappe.conf.cloud_storage_settings | ||
| config: dict = get_cloud_storage_config() | ||
| session = Session( | ||
| aws_access_key_id=config.get("access_key"), | ||
| aws_secret_access_key=config.get("secret"), | ||
|
|
@@ -462,7 +495,7 @@ def get_cloud_storage_client(): | |
|
|
||
|
|
||
| def validate_config() -> None: | ||
| config: dict = frappe.conf.cloud_storage_settings | ||
| config: dict = get_cloud_storage_config() | ||
|
|
||
| if not config: | ||
| frappe.throw( | ||
|
|
@@ -560,14 +593,23 @@ def get_file_path(file: File, folder: str | None = None) -> str: | |
| except Exception as e: | ||
| frappe.log_error(f"Custom path generator failed: {str(e)}", "Cloud Storage Path Error") | ||
|
|
||
| config = frappe.conf.get("cloud_storage_settings", {}) | ||
| config = get_cloud_storage_config() | ||
| if config.get("use_legacy_paths", True): | ||
| return _legacy_get_file_path(file, folder) | ||
| # Verify if this is a fresh install or if we want to enforce new paths even with legacy flag | ||
| # For Hygient/Zerodiscount: We enforce strict site segregation. | ||
| pass | ||
|
|
||
| # Standard Logic | ||
| path = file.file_name | ||
| if folder: | ||
| return f"{folder}/{file.file_name}" | ||
| path = f"{folder}/{file.file_name}" | ||
|
|
||
| return file.file_name | ||
| # Enforce Site Segregation | ||
| # e.g. "site1.local/folder/filename.jpg" | ||
| if hasattr(frappe.local, "site") and frappe.local.site: | ||
| return f"{frappe.local.site}/{path}" | ||
|
Comment on lines
+609
to
+610
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is not necessary. If you need a custom path, you can configure it in {
"cloud_storage_settings": {
"folder": "site1.local/folder",
...
}
}This approach is more flexible and doesn't force a specific path structure on all installations. |
||
|
|
||
| return path | ||
|
|
||
|
|
||
| def _legacy_get_file_path(file: File, folder: str | None = None) -> str: | ||
|
|
@@ -599,9 +641,8 @@ def get_file_content_hash(content, content_type): | |
|
|
||
| @frappe.whitelist() | ||
| def write_file(file: File, remove_spaces_in_file_name: bool = True) -> File: | ||
| if not frappe.conf.cloud_storage_settings or frappe.conf.cloud_storage_settings.get( | ||
| "use_local", False | ||
| ): | ||
| config = get_cloud_storage_config() | ||
| if not config or config.get("use_local", False): | ||
| file.save_file_on_filesystem() | ||
| return file | ||
|
|
||
|
|
@@ -618,8 +659,14 @@ def write_file(file: File, remove_spaces_in_file_name: bool = True) -> File: | |
|
|
||
| if existing_file_hashes: | ||
| file_doc: File = frappe.get_doc("File", existing_file_hashes[0]) | ||
|
|
||
| # Propagate flags | ||
| if file.flags.ignore_links: file_doc.flags.ignore_links = True | ||
| if file.flags.ignore_permissions: file_doc.flags.ignore_permissions = True | ||
| if file.flags.ignore_validate: file_doc.flags.ignore_validate = True | ||
|
|
||
| file_doc.associate_files(file.attached_to_doctype, file.attached_to_name) | ||
| file_doc.save() | ||
| file_doc.save(ignore_permissions=True) | ||
| return file_doc | ||
|
|
||
| # if a filename-conflict is found, update the existing document with a new version instead | ||
|
|
@@ -636,6 +683,12 @@ def write_file(file: File, remove_spaces_in_file_name: bool = True) -> File: | |
| "content_type": file.content_type, | ||
| } | ||
| ) | ||
|
|
||
| # Propagate flags | ||
| if file.flags.ignore_links: file_doc.flags.ignore_links = True | ||
| if file.flags.ignore_permissions: file_doc.flags.ignore_permissions = True | ||
| if file.flags.ignore_validate: file_doc.flags.ignore_validate = True | ||
|
|
||
| file_doc.associate_files(file.attached_to_doctype, file.attached_to_name) | ||
| file = file_doc | ||
|
|
||
|
|
@@ -649,9 +702,8 @@ def write_file(file: File, remove_spaces_in_file_name: bool = True) -> File: | |
|
|
||
| @frappe.whitelist() | ||
| def delete_file(file: File, **kwargs) -> File: | ||
| if not frappe.conf.cloud_storage_settings or frappe.conf.cloud_storage_settings.get( | ||
| "use_local", False | ||
| ): | ||
| config = get_cloud_storage_config() | ||
| if not config or config.get("use_local", False): | ||
| file.delete_file_from_filesystem() | ||
| return file | ||
|
|
||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this function is a good idea, I recommend using the same keys documented in the configuration guide: https://github.com/agritheory/cloud_storage/blob/version-15/docs/configuration.md
This would maintain consistency with the official documentation and avoid confusion for users following the setup instructions.