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-api: migrate file api #6055

Merged
merged 13 commits into from Sep 25, 2018
Merged

Conversation

Thunderbottom
Copy link
Contributor

@Thunderbottom Thunderbottom commented Sep 5, 2018

Migrated file doctype APIs to files.py for streamlined access

closes #2800

adds changes to the way file api functions. For example:

Pythonic Way

The old call to save a file would require one to do the following:

f = save_file(filename, filedata, doctype, docname,
              folder, decode_base64, is_private, docfield)

The new save function functions as an object of the File class:

_file = frappe.get_doc({"doctype": "File",
                        "file_name": filename,
                        "attached_to_doctype": doctype,
                        "attached_to_name": docname,
                        "attached_to_field": docfield,
                        "content": filedata,
                        "decode": decode_base64,
                        "folder": folder,
                        "is_private": is_private})
_file.save()

This allows easier manipulation on files using a single file object, as opposed to manually accessing files through separate function calls each time a file needs to be manipulated.

REST API

To consume File as a part of the REST API, create a session. I'll use requests as an example:

import requests

sess = requests.Session()
# login to the user account
s.post("http://sitename.whatever/",
       data={'cmd': 'login', 'pwd': 'password', "usr": "email_id"},
       headers={'accept': 'application/json'})

This will create a requests session which you can then use to POST or GET requests.

POST

After creating a session, you can POST a File using requests by doing:

s.post("http://sitename.whatever/api/resource/File",
       data=json.dumps({
                "file_name": filename,
                "attached_to_doctype": doctype,
                "attached_to_name": docname,
                "attached_to_field": docfield,
                "content": data,
                "decode": boolean,
                "folder": folder,
                "is_private": boolean}),
       headers={'accept': 'application/json'})

GET

Similarly, you can perform GET requests by doing:

s.get("http://sitename.whatever/api/resource/File")

For more information about consuming the API, refer the documentation.

migrate from file_manager.py to file.py

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
migrate api from file_manager.py to file.py

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
migrate api from file_manager.py to file.py

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
migrate api from file_manager.py to file.py

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
successfully executed all test cases

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
* migrate more functions to file class
* add get_content(), returns file content from file_name
* move get_file_path() to get_full_path() to decrease naming ambiguity

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
file can now be saved by calling save() on the file object instance

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
…of file_url

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
filename, file_extension = os.path.splitext(fname)
_file = frappe.get_doc("File", {"file_url": data_import_doc.import_file})
fcontent = _file.get_content()
filename, file_extension = os.path.splitext(_file.file_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file.get_extension()

fid = frappe.form_dict.get('fid')
return frappe.utils.file_manager.remove_file(fid)
return remove_file(fid=fid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

frappe.delete_doc('File', fid)

@@ -55,7 +54,7 @@ def test_incoming_with_attach(self):
frappe.db.sql("DELETE FROM `tabCommunication` WHERE sender='test_sender@example.com'")
existing_file = frappe.get_doc({'doctype': 'File', 'file_name': 'erpnext-conf-14.png'})
frappe.delete_doc("File", existing_file.name)
delete_file_from_filesystem(existing_file)
existing_file.delete_file_from_filesystem()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

frappe.delete_doc should handle this

@@ -73,7 +72,8 @@ def test_incoming_with_attach(self):
# cleanup
existing_file = frappe.get_doc({'doctype': 'File', 'file_name': 'erpnext-conf-14.png'})
frappe.delete_doc("File", existing_file.name)
delete_file_from_filesystem(existing_file)
existing_file.delete_file_from_filesystem()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

Signed-off-by: Chinmay Pai <chinmaydpai@gmail.com>
@netchampfaris netchampfaris merged commit 171c650 into frappe:develop Sep 25, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Idea: File manager API
3 participants