Skip to content

Commit 376c776

Browse files
committed
Fixes #7425: Housekeeping command should honor zero verbosity
1 parent a1f271d commit 376c776

File tree

2 files changed

+40
-23
lines changed

2 files changed

+40
-23
lines changed

docs/release-notes/version-3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* [#7411](https://github.com/netbox-community/netbox/issues/7411) - Fix exception in UI when adding member devices to virtual chassis
1919
* [#7412](https://github.com/netbox-community/netbox/issues/7412) - Fix exception in UI when adding child device to device bay
2020
* [#7417](https://github.com/netbox-community/netbox/issues/7417) - Prevent exception when filtering objects list by invalid tag
21+
* [#7425](https://github.com/netbox-community/netbox/issues/7425) - Housekeeping command should honor zero verbosity
2122

2223
---
2324

netbox/extras/management/commands/housekeeping.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,48 +18,60 @@ class Command(BaseCommand):
1818
def handle(self, *args, **options):
1919

2020
# Clear expired authentication sessions (essentially replicating the `clearsessions` command)
21-
self.stdout.write("[*] Clearing expired authentication sessions")
22-
if options['verbosity'] >= 2:
23-
self.stdout.write(f"\tConfigured session engine: {settings.SESSION_ENGINE}")
21+
if options['verbosity']:
22+
self.stdout.write("[*] Clearing expired authentication sessions")
23+
if options['verbosity'] >= 2:
24+
self.stdout.write(f"\tConfigured session engine: {settings.SESSION_ENGINE}")
2425
engine = import_module(settings.SESSION_ENGINE)
2526
try:
2627
engine.SessionStore.clear_expired()
27-
self.stdout.write("\tSessions cleared.", self.style.SUCCESS)
28+
if options['verbosity']:
29+
self.stdout.write("\tSessions cleared.", self.style.SUCCESS)
2830
except NotImplementedError:
29-
self.stdout.write(
30-
f"\tThe configured session engine ({settings.SESSION_ENGINE}) does not support "
31-
f"clearing sessions; skipping."
32-
)
31+
if options['verbosity']:
32+
self.stdout.write(
33+
f"\tThe configured session engine ({settings.SESSION_ENGINE}) does not support "
34+
f"clearing sessions; skipping."
35+
)
3336

3437
# Delete expired ObjectRecords
35-
self.stdout.write("[*] Checking for expired changelog records")
38+
if options['verbosity']:
39+
self.stdout.write("[*] Checking for expired changelog records")
3640
if settings.CHANGELOG_RETENTION:
3741
cutoff = timezone.now() - timedelta(days=settings.CHANGELOG_RETENTION)
3842
if options['verbosity'] >= 2:
39-
self.stdout.write(f"Retention period: {settings.CHANGELOG_RETENTION} days")
43+
self.stdout.write(f"\tRetention period: {settings.CHANGELOG_RETENTION} days")
4044
self.stdout.write(f"\tCut-off time: {cutoff}")
4145
expired_records = ObjectChange.objects.filter(time__lt=cutoff).count()
4246
if expired_records:
43-
self.stdout.write(f"\tDeleting {expired_records} expired records... ", self.style.WARNING, ending="")
44-
self.stdout.flush()
47+
if options['verbosity']:
48+
self.stdout.write(
49+
f"\tDeleting {expired_records} expired records... ",
50+
self.style.WARNING,
51+
ending=""
52+
)
53+
self.stdout.flush()
4554
ObjectChange.objects.filter(time__lt=cutoff)._raw_delete(using=DEFAULT_DB_ALIAS)
46-
self.stdout.write("Done.", self.style.WARNING)
47-
else:
48-
self.stdout.write("\tNo expired records found.")
49-
else:
55+
if options['verbosity']:
56+
self.stdout.write("Done.", self.style.SUCCESS)
57+
elif options['verbosity']:
58+
self.stdout.write("\tNo expired records found.", self.style.SUCCESS)
59+
elif options['verbosity']:
5060
self.stdout.write(
5161
f"\tSkipping: No retention period specified (CHANGELOG_RETENTION = {settings.CHANGELOG_RETENTION})"
5262
)
5363

5464
# Check for new releases (if enabled)
55-
self.stdout.write("[*] Checking for latest release")
65+
if options['verbosity']:
66+
self.stdout.write("[*] Checking for latest release")
5667
if settings.RELEASE_CHECK_URL:
5768
headers = {
5869
'Accept': 'application/vnd.github.v3+json',
5970
}
6071

6172
try:
62-
self.stdout.write(f"\tFetching {settings.RELEASE_CHECK_URL}")
73+
if options['verbosity'] >= 2:
74+
self.stdout.write(f"\tFetching {settings.RELEASE_CHECK_URL}")
6375
response = requests.get(
6476
url=settings.RELEASE_CHECK_URL,
6577
headers=headers,
@@ -73,15 +85,19 @@ def handle(self, *args, **options):
7385
continue
7486
releases.append((version.parse(release['tag_name']), release.get('html_url')))
7587
latest_release = max(releases)
76-
self.stdout.write(f"\tFound {len(response.json())} releases; {len(releases)} usable")
77-
self.stdout.write(f"\tLatest release: {latest_release[0]}")
88+
if options['verbosity'] >= 2:
89+
self.stdout.write(f"\tFound {len(response.json())} releases; {len(releases)} usable")
90+
if options['verbosity']:
91+
self.stdout.write(f"\tLatest release: {latest_release[0]}", self.style.SUCCESS)
7892

7993
# Cache the most recent release
8094
cache.set('latest_release', latest_release, None)
8195

8296
except requests.exceptions.RequestException as exc:
83-
self.stdout.write(f"\tRequest error: {exc}")
97+
self.stdout.write(f"\tRequest error: {exc}", self.style.ERROR)
8498
else:
85-
self.stdout.write(f"\tSkipping: RELEASE_CHECK_URL not set")
99+
if options['verbosity']:
100+
self.stdout.write(f"\tSkipping: RELEASE_CHECK_URL not set")
86101

87-
self.stdout.write("Finished.", self.style.SUCCESS)
102+
if options['verbosity']:
103+
self.stdout.write("Finished.", self.style.SUCCESS)

0 commit comments

Comments
 (0)