1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #5948: Invalidate cached queries when running renaturalize

This commit is contained in:
Jeremy Stretch
2021-03-09 16:39:55 -05:00
parent 1cc82f2ab7
commit c3ad2d0a80
2 changed files with 15 additions and 10 deletions

View File

@ -17,6 +17,7 @@
* [#5872](https://github.com/netbox-community/netbox/issues/5872) - Ordering of devices by primary IP should respect PREFER_IPV4 configuration parameter * [#5872](https://github.com/netbox-community/netbox/issues/5872) - Ordering of devices by primary IP should respect PREFER_IPV4 configuration parameter
* [#5922](https://github.com/netbox-community/netbox/issues/5922) - Fix options for filtering object permissions in admin UI * [#5922](https://github.com/netbox-community/netbox/issues/5922) - Fix options for filtering object permissions in admin UI
* [#5935](https://github.com/netbox-community/netbox/issues/5935) - Fix filtering prefixes list by multiple prefix values * [#5935](https://github.com/netbox-community/netbox/issues/5935) - Fix filtering prefixes list by multiple prefix values
* [#5948](https://github.com/netbox-community/netbox/issues/5948) - Invalidate cached queries when running `renaturalize`
## v2.10.5 (2021-02-24) ## v2.10.5 (2021-02-24)

View File

@ -1,3 +1,4 @@
from cacheops import invalidate_model
from django.apps import apps from django.apps import apps
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
@ -27,7 +28,7 @@ class Command(BaseCommand):
app_label, model_name = name.split('.') app_label, model_name = name.split('.')
except ValueError: except ValueError:
raise CommandError( raise CommandError(
"Invalid format: {}. Models must be specified in the form app_label.ModelName.".format(name) f"Invalid format: {name}. Models must be specified in the form app_label.ModelName."
) )
try: try:
app_config = apps.get_app_config(app_label) app_config = apps.get_app_config(app_label)
@ -36,13 +37,13 @@ class Command(BaseCommand):
try: try:
model = app_config.get_model(model_name) model = app_config.get_model(model_name)
except LookupError: except LookupError:
raise CommandError("Unknown model: {}.{}".format(app_label, model_name)) raise CommandError(f"Unknown model: {app_label}.{model_name}")
fields = [ fields = [
field for field in model._meta.concrete_fields if type(field) is NaturalOrderingField field for field in model._meta.concrete_fields if type(field) is NaturalOrderingField
] ]
if not fields: if not fields:
raise CommandError( raise CommandError(
"Invalid model: {}.{} does not employ natural ordering".format(app_label, model_name) f"Invalid model: {app_label}.{model_name} does not employ natural ordering"
) )
models.append( models.append(
(model, fields) (model, fields)
@ -67,7 +68,7 @@ class Command(BaseCommand):
models = self._get_models(args) models = self._get_models(args)
if options['verbosity']: if options['verbosity']:
self.stdout.write("Renaturalizing {} models.".format(len(models))) self.stdout.write(f"Renaturalizing {len(models)} models.")
for model, fields in models: for model, fields in models:
for field in fields: for field in fields:
@ -78,7 +79,7 @@ class Command(BaseCommand):
# Print the model and field name # Print the model and field name
if options['verbosity']: if options['verbosity']:
self.stdout.write( self.stdout.write(
"{}.{} ({})... ".format(model._meta.label, field.target_field, field.name), f"{model._meta.label}.{field.target_field} ({field.name})... ",
ending='\n' if options['verbosity'] >= 2 else '' ending='\n' if options['verbosity'] >= 2 else ''
) )
self.stdout.flush() self.stdout.flush()
@ -89,23 +90,26 @@ class Command(BaseCommand):
naturalized_value = naturalize(value, max_length=field.max_length) naturalized_value = naturalize(value, max_length=field.max_length)
if options['verbosity'] >= 2: if options['verbosity'] >= 2:
self.stdout.write(" {} -> {}".format(value, naturalized_value), ending='') self.stdout.write(f" {value} -> {naturalized_value}", ending='')
self.stdout.flush() self.stdout.flush()
# Update each unique field value in bulk # Update each unique field value in bulk
changed = model.objects.filter(name=value).update(**{field.name: naturalized_value}) changed = model.objects.filter(name=value).update(**{field.name: naturalized_value})
if options['verbosity'] >= 2: if options['verbosity'] >= 2:
self.stdout.write(" ({})".format(changed)) self.stdout.write(f" ({changed})")
count += changed count += changed
# Print the total count of alterations for the field # Print the total count of alterations for the field
if options['verbosity'] >= 2: if options['verbosity'] >= 2:
self.stdout.write(self.style.SUCCESS("{} {} updated ({} unique values)".format( self.stdout.write(self.style.SUCCESS(
count, model._meta.verbose_name_plural, queryset.count() f"{count} {model._meta.verbose_name_plural} updated ({queryset.count()} unique values)"
))) ))
elif options['verbosity']: elif options['verbosity']:
self.stdout.write(self.style.SUCCESS(str(count))) self.stdout.write(self.style.SUCCESS(str(count)))
# Invalidate cached queries
invalidate_model(model)
if options['verbosity']: if options['verbosity']:
self.stdout.write(self.style.SUCCESS("Done.")) self.stdout.write(self.style.SUCCESS("Done."))