| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2025, Alexei Znamensky <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: django_dumpdata |
| 10 |
author: |
| 11 |
- Alexei Znamensky (@russoz) |
| 12 |
short_description: Wrapper for C(django-admin dumpdata) |
| 13 |
version_added: 11.3.0 |
| 14 |
description: |
| 15 |
- This module is a wrapper for the execution of C(django-admin dumpdata). |
| 16 |
extends_documentation_fragment: |
| 17 |
- community.general._attributes |
| 18 |
- community.general._django |
| 19 |
- community.general._django.database |
| 20 |
- community.general._django.data |
| 21 |
attributes: |
| 22 |
check_mode: |
| 23 |
support: none |
| 24 |
diff_mode: |
| 25 |
support: none |
| 26 |
options: |
| 27 |
all: |
| 28 |
description: Dump all records, including those which might otherwise be filtered or modified by a custom manager. |
| 29 |
type: bool |
| 30 |
indent: |
| 31 |
description: |
| 32 |
- Indentation size for the output. |
| 33 |
- Default is not to indent, so the output is generated in one single line. |
| 34 |
type: int |
| 35 |
natural_foreign: |
| 36 |
description: Use natural keys when serializing for foreign keys. |
| 37 |
type: bool |
| 38 |
natural_primary: |
| 39 |
description: Omit primary keys when serializing. |
| 40 |
type: bool |
| 41 |
primary_keys: |
| 42 |
description: |
| 43 |
- List of primary keys to include in the dump. |
| 44 |
- Only available when dumping one single model. |
| 45 |
type: list |
| 46 |
elements: str |
| 47 |
aliases: ["pks"] |
| 48 |
fixture: |
| 49 |
description: |
| 50 |
- Path to the output file. |
| 51 |
- The fixture filename may end with V(.bz2), V(.gz), V(.lzma) or V(.xz), in which case the corresponding |
| 52 |
compression format is used. |
| 53 |
- This corresponds to the C(--output) parameter for the C(django-admin dumpdata) command. |
| 54 |
type: path |
| 55 |
aliases: [output] |
| 56 |
required: true |
| 57 |
apps_models: |
| 58 |
description: |
| 59 |
- Dump only the applications and models listed in the dump. |
| 60 |
- Format must be either V(app_label) or V(app_label.ModelName). |
| 61 |
- If not passed, all applications and models are to be dumped. |
| 62 |
type: list |
| 63 |
elements: str |
| 64 |
""" |
| 65 |
|
| 66 |
EXAMPLES = r""" |
| 67 |
- name: Dump all data |
| 68 |
community.general.django_dumpdata: |
| 69 |
settings: myproject.settings |
| 70 |
fixture: /tmp/mydata.json |
| 71 |
|
| 72 |
- name: Dump data excluding certain apps, into a compressed JSON file |
| 73 |
community.general.django_dumpdata: |
| 74 |
settings: myproject.settings |
| 75 |
database: myotherdb |
| 76 |
excludes: |
| 77 |
- auth |
| 78 |
- contenttypes |
| 79 |
fixture: /tmp/mydata.json.gz |
| 80 |
""" |
| 81 |
|
| 82 |
RETURN = r""" |
| 83 |
run_info: |
| 84 |
description: Command-line execution information. |
| 85 |
type: dict |
| 86 |
returned: success and O(verbosity) >= 3 |
| 87 |
version: |
| 88 |
description: Version of Django. |
| 89 |
type: str |
| 90 |
returned: always |
| 91 |
sample: 5.1.2 |
| 92 |
""" |
| 93 |
|
| 94 |
from ansible_collections.community.general.plugins.module_utils._django import DjangoModuleHelper |
| 95 |
|
| 96 |
|
| 97 |
class DjangoDumpData(DjangoModuleHelper): |
| 98 |
module = dict( |
| 99 |
argument_spec=dict( |
| 100 |
all=dict(type="bool"), |
| 101 |
indent=dict(type="int"), |
| 102 |
natural_foreign=dict(type="bool"), |
| 103 |
natural_primary=dict(type="bool"), |
| 104 |
primary_keys=dict(type="list", elements="str", aliases=["pks"], no_log=False), |
| 105 |
# the underlying vardict does not allow the name "output" |
| 106 |
fixture=dict(type="path", required=True, aliases=["output"]), |
| 107 |
apps_models=dict(type="list", elements="str"), |
| 108 |
), |
| 109 |
supports_check_mode=False, |
| 110 |
) |
| 111 |
django_admin_cmd = "dumpdata" |
| 112 |
django_admin_arg_order = ( |
| 113 |
"all format indent excludes database_dash natural_foreign natural_primary primary_keys fixture apps_models" |
| 114 |
) |
| 115 |
_django_args = ["data", "database_dash"] |
| 116 |
|
| 117 |
def __init_module__(self): |
| 118 |
self.vars.set("database_dash", self.vars.database, output=False) |
| 119 |
|
| 120 |
|
| 121 |
def main(): |
| 122 |
DjangoDumpData.execute() |
| 123 |
|
| 124 |
|
| 125 |
if __name__ == "__main__": |
| 126 |
main() |