-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4314 from GSA-TTS/main
- Loading branch information
Showing
5 changed files
with
95 additions
and
9 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
backend/audit/management/commands/delete_blank_access_entries.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from audit.models import ( | ||
Access, | ||
DeletedAccess, | ||
delete_access_and_create_record, | ||
) | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
import logging | ||
import sys | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
Django management command for deleting Access entries with blank email | ||
fields. A DeletedAccess entry is created for each deleted Access entry by | ||
using audit.models.delete_access_and_create_record. | ||
""" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
"--count", | ||
action="store_true", | ||
help="Only logs the blank Access entries count without deleting", | ||
) | ||
parser.add_argument( | ||
"--limit", | ||
type=int, | ||
help="Limits the number of blank Access entries to delete", | ||
default=None, | ||
) | ||
|
||
def handle(self, *args, **options): | ||
access_start_count = len(Access.objects.all()) | ||
logger.info(f"Access count: {access_start_count}") | ||
|
||
deleted_access_start_count = len(DeletedAccess.objects.all()) | ||
logger.info(f"DeletedAccess count: {deleted_access_start_count}") | ||
|
||
blank_accesses = Access.objects.filter(email="") | ||
logger.info(f"Blank Access count: {len(blank_accesses)}") | ||
|
||
if options.get("count"): | ||
sys.exit(0) | ||
|
||
limit = options.get("limit") | ||
if limit is not None: | ||
logger.info(f"Deletion limit: {limit}") | ||
|
||
deleted_count = 0 | ||
|
||
for blank_access in blank_accesses: | ||
if deleted_count == limit: | ||
logger.info("Deletion limit reached") | ||
break | ||
|
||
logger.info( | ||
f"Deleting blank Access for {blank_access.sac.report_id}, {blank_access.fullname}" | ||
) | ||
|
||
_, deletion_record = delete_access_and_create_record(blank_access) | ||
|
||
logger.info(f"Created DeletedAccess {deletion_record.id}") | ||
|
||
deleted_count += 1 | ||
|
||
logger.info(f"Deleted {deleted_count} blank Access entries") | ||
|
||
access_finish_count = len(Access.objects.all()) | ||
logger.info(f"Final Access count: {access_finish_count}") | ||
|
||
deleted_access_finish_count = len(DeletedAccess.objects.all()) | ||
logger.info(f"Final DeletedAccess count: {deleted_access_finish_count}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters