Updates to data import/export steps (#6244)

* Updates to data import/export steps

- Allow import/export of users.owner model
- Allow export of API tokens (optional, default=False)

* Exclude plugin configuration data by default

* Add option to exclude socialaccount information from exported data
This commit is contained in:
Oliver 2024-01-16 01:26:57 +11:00 committed by GitHub
parent 576bef5d82
commit 829e01dd33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -58,18 +58,24 @@ def apps():
] ]
def content_excludes(): def content_excludes(
"""Returns a list of content types to exclude from import/export.""" allow_tokens: bool = True, allow_plugins: bool = True, allow_sso: bool = True
):
"""Returns a list of content types to exclude from import/export.
Arguments:
allow_tokens (bool): Allow tokens to be exported/importe
allow_plugins (bool): Allow plugin information to be exported/imported
allow_sso (bool): Allow SSO tokens to be exported/imported
"""
excludes = [ excludes = [
'contenttypes', 'contenttypes',
'auth.permission', 'auth.permission',
'users.apitoken',
'error_report.error', 'error_report.error',
'admin.logentry', 'admin.logentry',
'django_q.schedule', 'django_q.schedule',
'django_q.task', 'django_q.task',
'django_q.ormq', 'django_q.ormq',
'users.owner',
'exchange.rate', 'exchange.rate',
'exchange.exchangebackend', 'exchange.exchangebackend',
'common.notificationentry', 'common.notificationentry',
@ -77,6 +83,19 @@ def content_excludes():
'user_sessions.session', 'user_sessions.session',
] ]
# Optionally exclude user token information
if not allow_tokens:
excludes.append('users.apitoken')
# Optionally exclude plugin information
if not allow_plugins:
excludes.append('plugin.pluginconfig')
excludes.append('plugin.pluginsetting')
# Optionally exclude SSO application information
if not allow_sso:
excludes.append('socialaccount.socialapp')
output = '' output = ''
for e in excludes: for e in excludes:
@ -399,8 +418,11 @@ def update(c, skip_backup=False, frontend: bool = False, no_frontend: bool = Fal
@task( @task(
help={ help={
'filename': "Output filename (default = 'data.json')", 'filename': "Output filename (default = 'data.json')",
'overwrite': 'Overwrite existing files without asking first (default = off/False)', 'overwrite': 'Overwrite existing files without asking first (default = False)',
'include_permissions': 'Include user and group permissions in the output file (filename) (default = off/False)', 'include_permissions': 'Include user and group permissions in the output file (default = False)',
'include_tokens': 'Include API tokens in the output file (default = False)',
'include_plugins': 'Include plugin data in the output file (default = False)',
'include_sso': 'Include SSO token data in the output file (default = 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)', '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)',
} }
) )
@ -409,6 +431,9 @@ def export_records(
filename='data.json', filename='data.json',
overwrite=False, overwrite=False,
include_permissions=False, include_permissions=False,
include_tokens=False,
include_plugins=False,
include_sso=False,
delete_temp=False, delete_temp=False,
): ):
"""Export all database records to a file. """Export all database records to a file.
@ -438,7 +463,13 @@ def export_records(
tmpfile = f'{filename}.tmp' tmpfile = f'{filename}.tmp'
cmd = f"dumpdata --indent 2 --output '{tmpfile}' {content_excludes()}" excludes = content_excludes(
allow_tokens=include_tokens,
allow_plugins=include_plugins,
allow_sso=include_sso,
)
cmd = f"dumpdata --indent 2 --output '{tmpfile}' {excludes}"
# Dump data to temporary file # Dump data to temporary file
manage(c, cmd, pty=True) manage(c, cmd, pty=True)