0
0
Fork 0
mirror of https://github.com/healthchecks/healthchecks.git synced 2025-04-11 15:51:19 +00:00

Improve type hints in management commands

This commit is contained in:
Pēteris Caune 2023-10-18 13:46:44 +03:00
parent ce622da6bd
commit e8be347d1a
No known key found for this signature in database
GPG key ID: E28D7679E9A9EDE2
8 changed files with 29 additions and 17 deletions

View file

@ -1,6 +1,7 @@
from __future__ import annotations
from getpass import getpass
from typing import Any
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
@ -13,7 +14,7 @@ from hc.accounts.views import _make_user
class Command(BaseCommand):
help = """Create a super-user account."""
def handle(self, *args, **options) -> str:
def handle(self, **options: Any) -> str:
email = None
password = None

View file

@ -1,6 +1,7 @@
from __future__ import annotations
from datetime import timedelta as td
from typing import Any
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
@ -21,7 +22,7 @@ class Command(BaseCommand):
"""
def handle(self, *args, **options) -> str:
def handle(self, **options: Any) -> str:
month_ago = now() - td(days=30)
# Old accounts, never logged in, no team memberships

View file

@ -2,10 +2,12 @@ from __future__ import annotations
import time
from datetime import timedelta as td
from typing import Any
from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand
from django.db.models import QuerySet
from django.utils.timezone import now
from hc.accounts.models import Profile
@ -16,17 +18,20 @@ from hc.lib import emails
class Command(BaseCommand):
help = """Send warnings to accounts marked for deletion. """
def pause(self):
def pause(self) -> None:
time.sleep(1)
def members(self, user):
def members(self, user: User) -> QuerySet[User]:
q = User.objects.filter(memberships__project__owner=user)
q = q.exclude(last_login=None)
return q.order_by("email")
def send_channel_notifications(self, profile, skip_emails):
def send_channel_notifications(
self, profile: Profile, skip_emails: list[str]
) -> None:
# Sending deletion notices to configured notification channels is
# a last ditch effort: only do this if 14 or fewer days are left.
assert profile.deletion_scheduled_date
delta = profile.deletion_scheduled_date - now()
if delta.days > 14:
return
@ -56,7 +61,7 @@ class Command(BaseCommand):
if error:
self.stdout.write(f" Error sending notification: {error}")
def handle(self, *args, **options) -> str:
def handle(self, **options: Any) -> str:
q = Profile.objects.order_by("id")
q = q.filter(deletion_scheduled_date__gt=now())

View file

@ -2,6 +2,7 @@ from __future__ import annotations
import time
from datetime import timedelta as td
from typing import Any
from django.conf import settings
from django.core.management.base import BaseCommand
@ -15,7 +16,7 @@ from hc.lib import emails
YEAR_AGO = now() - td(days=365)
def has_projects_with_active_members(profile):
def has_projects_with_active_members(profile: Profile) -> bool:
q = Member.objects.filter(project__owner_id=profile.user_id)
recent_signup = Q(user__date_joined__gt=YEAR_AGO)
recent_login = Q(user__last_login__gt=YEAR_AGO)
@ -36,10 +37,10 @@ class Command(BaseCommand):
"""
def pause(self):
def pause(self) -> None:
time.sleep(1)
def handle(self, *args, **options) -> str:
def handle(self, **options: Any) -> str:
q = Profile.objects.order_by("id")
# Exclude accounts with logins in the last year
q = q.exclude(user__last_login__gt=YEAR_AGO)

View file

@ -1,6 +1,7 @@
from __future__ import annotations
from datetime import timedelta as td
from typing import Any
from django.core.management.base import BaseCommand
@ -11,10 +12,10 @@ from hc.lib.date import month_boundaries
class Command(BaseCommand):
help = "Prune old Flip objects."
def handle(self, *args, **options):
def handle(self, **options: Any) -> str:
threshold = min(month_boundaries(3, "UTC")) - td(days=1)
q = Flip.objects.filter(created__lt=threshold)
n_pruned, _ = q.delete()
return "Done! Pruned %d flips." % n_pruned
return f"Done! Pruned {n_pruned} flips."

View file

@ -1,5 +1,7 @@
from __future__ import annotations
from typing import Any
from django.core.management.base import BaseCommand
from django.db.models import Min
@ -9,7 +11,7 @@ from hc.api.models import Check, Notification
class Command(BaseCommand):
help = "Prune stored notifications"
def handle(self, *args, **options):
def handle(self, **options: Any) -> str:
total = 0
q = Check.objects.filter(n_pings__gt=100)
@ -23,4 +25,4 @@ class Command(BaseCommand):
num_deleted, _ = qq.delete()
total += num_deleted
return "Done! Pruned %d notifications." % total
return f"Done! Pruned {total} notifications."

View file

@ -1,5 +1,6 @@
from __future__ import annotations
from typing import Any
from uuid import UUID
from django.conf import settings
@ -13,7 +14,7 @@ from hc.lib.s3 import client
class Command(BaseCommand):
help = "Prune ping bodies of deleted checks from object store."
def handle(self, *args, **options):
def handle(self, **options: Any) -> str:
existing = set(map(str, Check.objects.values_list("code", flat=True)))
c = client()

View file

@ -1,6 +1,7 @@
from __future__ import annotations
from datetime import timedelta as td
from typing import Any
from django.core.management.base import BaseCommand
from django.utils.timezone import now
@ -11,10 +12,9 @@ from hc.api.models import TokenBucket
class Command(BaseCommand):
help = "Prune pings based on limits in user profiles"
def handle(self, *args, **options):
def handle(self, **options: Any) -> str:
day_ago = now() - td(days=1)
q = TokenBucket.objects.filter(updated__lt=day_ago)
n_pruned, _ = q.delete()
return "Done! Pruned %d token bucket entries" % n_pruned
return f"Done! Pruned {n_pruned} token bucket entries"