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

add option to override defaults to converttargetextras #946

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/targets/target_fields.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ more the of the ``Extra Field`` and ``Target Field`` names respectively.
./manage.py converttargetextras --target_extra extra_bool extra_number --model_field example_bool example_number

This command will go through each target and transfer the data from the ``Extra Field`` to the ``Target Field``. If the
``Target Field`` is already populated, the data will not be transferred. When finished, the ``Extra Field`` data will be
``Target Field`` is already populated with a value other than the default value, the data will not be transferred unless
the ``--force`` flag is set. When finished, the ``Extra Field`` data will be
deleted, and you will likely want to remove the ``EXTRA_FIELDS`` setting from your ``settings.py`` file.

Adding ``Extra Fields``
Expand Down
16 changes: 13 additions & 3 deletions tom_targets/management/commands/converttargetextras.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def add_arguments(self, parser):
action='store_true',
help='Provide a list of available TargetExtras and Model Fields.',
)
parser.add_argument(
'--force',
action='store_true',
help='Overwrite any existing values in the relevant model fields with those from the corresponding '
'TargetExtra.',
)

def prompt_extra_field(self, extra_field_keys):
"""
Expand Down Expand Up @@ -97,7 +103,7 @@ def confirm_conversion(self, chosen_extra, chosen_model_field):
else:
self.stdout.write('Invalid response. Please try again.')

def convert_target_extra(self, chosen_extra, chosen_model_field):
def convert_target_extra(self, chosen_extra, chosen_model_field, force=False):
"""
Perform the actual conversion from a `chosen_extra` to a `chosen_model_field` for each target that has one of
these TargetExtras.
Expand All @@ -107,7 +113,11 @@ def convert_target_extra(self, chosen_extra, chosen_model_field):
"""
for extra in TargetExtra.objects.filter(key=chosen_extra):
target = Target.objects.get(pk=extra.target.pk)
if getattr(target, chosen_model_field, None):
model_field_default = Target._meta.get_field(chosen_model_field).get_default()
# If model already has a value, don't overwrite unless it's the default value or force is True
if not force and \
getattr(target, chosen_model_field, None) and \
getattr(target, chosen_model_field) != model_field_default:
self.stdout.write(f"{self.style.ERROR('Warning:')} {target}.{chosen_model_field} "
f"already has a value: {getattr(target, chosen_model_field)}. Skipping.")
continue
Expand Down Expand Up @@ -164,6 +174,6 @@ def handle(self, *args, **options):
if not confirmed:
continue

self.convert_target_extra(chosen_extra, chosen_model_field)
self.convert_target_extra(chosen_extra, chosen_model_field, options['force'])

return
Loading