1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/peeringdb_server/management/commands/pdb_cleanup_vq.py
Matt Griswold 04f1928b3c Updates 2.23 (#843)
* pipenv lock and pyupgrade

* pyupgrade, black format, add docs

* update for py3.7
2020-09-29 20:13:38 -05:00

57 lines
1.8 KiB
Python

from datetime import datetime, timedelta, timezone
from django.core.management.base import BaseCommand
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.utils import timezone
from peeringdb_server.models import VerificationQueueItem, User
from peeringdb_server.management.commands.pdb_base_command import PeeringDBBaseCommand
class Command(PeeringDBBaseCommand):
help = "Use this tool to clean up the Verification Queue"
def add_arguments(self, parser):
super().add_arguments(parser)
subparsers = parser.add_subparsers()
parser_users = subparsers.add_parser(
"users",
parents=[parser],
add_help=False,
help="Tool to remove outdated user verification requests",
)
parser_users.set_defaults(func=self._clean_users)
def handle(self, *args, **options):
super().handle(*args, **options)
if options.get("func"):
options["func"]()
def _clean_users(self):
model = User
content_type = ContentType.objects.get_for_model(model)
date = datetime.now(timezone.utc) - timedelta(days=settings.VQUEUE_USER_MAX_AGE)
qset = VerificationQueueItem.objects.filter(
content_type=content_type, created__lte=date
)
count = qset.count()
self.log(f"Deleting VerificationQueueItems for Users created before {date:%x}")
self.log(f"Items flagged for deletion: {count}")
counter = 0
for vqi in qset:
counter += 1
self.log(
f"Deleting VerificationQueueItem for {vqi.content_type} "
f"id #{vqi.object_id}... {counter} / {count}"
)
if self.commit:
vqi.delete()