mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Export records update - allow overwriting existing files without user input (#3156)
* Add flag to overwrite existing file when exporting records
* Remove temp. file at end of export process
* Run flake8 on tasks.py as well
* Fix style
* Change style of default text
* Add type bool
* dev-setup
* Revert "dev-setup"
This reverts commit 789356422a
.
* Update tasks.py with new flags to allow choosing where permissions end up, and if temporary files are kept or not
This commit is contained in:
parent
258957c14c
commit
79f498a648
@ -7,7 +7,7 @@ exclude: |
|
|||||||
)$
|
)$
|
||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v4.2.0
|
rev: v4.3.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
|
52
tasks.py
52
tasks.py
@ -224,7 +224,7 @@ def update(c):
|
|||||||
def style(c):
|
def style(c):
|
||||||
"""Run PEP style checks against InvenTree sourcecode"""
|
"""Run PEP style checks against InvenTree sourcecode"""
|
||||||
print("Running PEP style checks...")
|
print("Running PEP style checks...")
|
||||||
c.run('flake8 InvenTree')
|
c.run('flake8 InvenTree tasks.py')
|
||||||
|
|
||||||
|
|
||||||
@task
|
@task
|
||||||
@ -282,9 +282,30 @@ def content_excludes():
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
@task(help={'filename': "Output filename (default = 'data.json')"})
|
@task(help={
|
||||||
def export_records(c, filename='data.json'):
|
'filename': "Output filename (default = 'data.json')",
|
||||||
"""Export all database records to a file"""
|
'overwrite': "Overwrite existing files without asking first (default = off/False)",
|
||||||
|
'include_permissions': "Include user and group permissions in the output file (filename) (default = off/False)",
|
||||||
|
'delete_temp': "Delete temporary files (containing permissions) at end of run. Note that this will delete temporary files from previous runs as well. (default = off/False)"
|
||||||
|
})
|
||||||
|
def export_records(c, filename='data.json', overwrite=False, include_permissions=False, delete_temp=False):
|
||||||
|
"""Export all database records to a file.
|
||||||
|
|
||||||
|
Write data to the file defined by filename.
|
||||||
|
If --overwrite is not set, the user will be prompted about overwriting an existing files.
|
||||||
|
If --include-permissions is not set, the file defined by filename will have permissions specified for a user or group removed.
|
||||||
|
If --delete-temp is not set, the temporary file (which includes permissions) will not be deleted. This file is named filename.tmp
|
||||||
|
|
||||||
|
For historical reasons, calling this function without any arguments will thus result in two files:
|
||||||
|
- data.json: does not include permissions
|
||||||
|
- data.json.tmp: includes permissions
|
||||||
|
|
||||||
|
If you want the script to overwrite any existing files without asking, add argument -o / --overwrite.
|
||||||
|
|
||||||
|
If you only want one file, add argument - d / --delete-temp.
|
||||||
|
|
||||||
|
If you want only one file, with permissions, then additionally add argument -i / --include-permissions
|
||||||
|
"""
|
||||||
# Get an absolute path to the file
|
# Get an absolute path to the file
|
||||||
if not os.path.isabs(filename):
|
if not os.path.isabs(filename):
|
||||||
filename = os.path.join(localDir(), filename)
|
filename = os.path.join(localDir(), filename)
|
||||||
@ -292,7 +313,7 @@ def export_records(c, filename='data.json'):
|
|||||||
|
|
||||||
print(f"Exporting database records to file '{filename}'")
|
print(f"Exporting database records to file '{filename}'")
|
||||||
|
|
||||||
if os.path.exists(filename):
|
if os.path.exists(filename) and overwrite is False:
|
||||||
response = input("Warning: file already exists. Do you want to overwrite? [y/N]: ")
|
response = input("Warning: file already exists. Do you want to overwrite? [y/N]: ")
|
||||||
response = str(response).strip().lower()
|
response = str(response).strip().lower()
|
||||||
|
|
||||||
@ -313,16 +334,17 @@ def export_records(c, filename='data.json'):
|
|||||||
with open(tmpfile, "r") as f_in:
|
with open(tmpfile, "r") as f_in:
|
||||||
data = json.loads(f_in.read())
|
data = json.loads(f_in.read())
|
||||||
|
|
||||||
for entry in data:
|
if include_permissions is False:
|
||||||
if "model" in entry:
|
for entry in data:
|
||||||
|
if "model" in entry:
|
||||||
|
|
||||||
# Clear out any permissions specified for a group
|
# Clear out any permissions specified for a group
|
||||||
if entry["model"] == "auth.group":
|
if entry["model"] == "auth.group":
|
||||||
entry["fields"]["permissions"] = []
|
entry["fields"]["permissions"] = []
|
||||||
|
|
||||||
# Clear out any permissions specified for a user
|
# Clear out any permissions specified for a user
|
||||||
if entry["model"] == "auth.user":
|
if entry["model"] == "auth.user":
|
||||||
entry["fields"]["user_permissions"] = []
|
entry["fields"]["user_permissions"] = []
|
||||||
|
|
||||||
# Write the processed data to file
|
# Write the processed data to file
|
||||||
with open(filename, "w") as f_out:
|
with open(filename, "w") as f_out:
|
||||||
@ -330,6 +352,10 @@ def export_records(c, filename='data.json'):
|
|||||||
|
|
||||||
print("Data export completed")
|
print("Data export completed")
|
||||||
|
|
||||||
|
if delete_temp is True:
|
||||||
|
print("Removing temporary file")
|
||||||
|
os.remove(tmpfile)
|
||||||
|
|
||||||
|
|
||||||
@task(help={'filename': 'Input filename', 'clear': 'Clear existing data before import'}, post=[rebuild_models, rebuild_thumbnails])
|
@task(help={'filename': 'Input filename', 'clear': 'Clear existing data before import'}, post=[rebuild_models, rebuild_thumbnails])
|
||||||
def import_records(c, filename='data.json', clear=False):
|
def import_records(c, filename='data.json', clear=False):
|
||||||
|
Loading…
Reference in New Issue
Block a user