Custom post-processing step as part of export-records command:

- We must delete the permission objects associated with a user or group, as auth.permissions are *NOT* included in the exported data file
- The permissions will be automatically created on first run of the server
This commit is contained in:
Oliver Walters 2021-04-25 11:29:07 +10:00
parent 8ad16ceb9c
commit d9d6a2392d

View File

@ -2,6 +2,7 @@
from shutil import copyfile from shutil import copyfile
import os import os
import json
import sys import sys
try: try:
@ -278,10 +279,33 @@ def export_records(c, filename='data.json'):
print("Cancelled export operation") print("Cancelled export operation")
sys.exit(1) sys.exit(1)
cmd = f"dumpdata --indent 2 --output {filename} {content_excludes()}" tmpfile = f"{filename}.tmp"
cmd = f"dumpdata --indent 2 --output {tmpfile} {content_excludes()}"
# Dump data to temporary file
manage(c, cmd, pty=True) manage(c, cmd, pty=True)
# Post-process the file, to remove any "permissions" specified for a group
with open(tmpfile, "r") as f_in:
data = json.loads(f_in.read())
for entry in data:
if "model" in entry:
# Clear out any permissions specified for a group
if entry["model"] == "auth.group":
entry["fields"]["permissions"] = []
# Clear out any permissions specified for a user
if entry["model"] == "auth.user":
entry["fields"]["user_permissions"] = []
# Write the processed data to file
with open(filename, "w") as f_out:
f_out.write(json.dumps(data, indent=2))
@task(help={'filename': 'Input filename'}) @task(help={'filename': 'Input filename'})
def import_records(c, filename='data.json'): def import_records(c, filename='data.json'):
""" """