|
| 1 | +import logging |
| 2 | +from datetime import timedelta |
| 3 | +from django.core.management.base import BaseCommand |
| 4 | +from django.utils import timezone |
| 5 | +from osf.models import Registration |
| 6 | +from osf.models.admin_log_entry import AdminLogEntry, MANUAL_ARCHIVE_RESTART |
| 7 | +from website import settings |
| 8 | +from scripts.approve_registrations import approve_past_pendings |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | + |
| 13 | +class Command(BaseCommand): |
| 14 | + help = 'Process registrations that were manually restarted and may need approval' |
| 15 | + |
| 16 | + def add_arguments(self, parser): |
| 17 | + parser.add_argument( |
| 18 | + '--dry-run', |
| 19 | + action='store_true', |
| 20 | + help='Show what would be done without actually doing it', |
| 21 | + ) |
| 22 | + parser.add_argument( |
| 23 | + '--hours-back', |
| 24 | + type=int, |
| 25 | + default=72, |
| 26 | + help='How many hours back to look for manual restarts (default: 72)', |
| 27 | + ) |
| 28 | + parser.add_argument( |
| 29 | + '--registration-id', |
| 30 | + type=str, |
| 31 | + help='Process a specific registration ID only', |
| 32 | + ) |
| 33 | + |
| 34 | + def handle(self, *args, **options): |
| 35 | + dry_run = options['dry_run'] |
| 36 | + hours_back = options['hours_back'] |
| 37 | + specific_registration = options.get('registration_id') |
| 38 | + |
| 39 | + if dry_run: |
| 40 | + self.stdout.write(self.style.WARNING('Running in DRY RUN mode - no changes will be made')) |
| 41 | + |
| 42 | + since = timezone.now() - timedelta(hours=hours_back) |
| 43 | + |
| 44 | + query = AdminLogEntry.objects.filter( |
| 45 | + action_flag=MANUAL_ARCHIVE_RESTART, |
| 46 | + action_time__gte=since |
| 47 | + ) |
| 48 | + |
| 49 | + if specific_registration: |
| 50 | + try: |
| 51 | + reg = Registration.objects.get(_id=specific_registration) |
| 52 | + query = query.filter(object_id=reg.pk) |
| 53 | + self.stdout.write(f"Processing specific registration: {specific_registration}") |
| 54 | + except Registration.DoesNotExist: |
| 55 | + self.stdout.write(self.style.ERROR(f"Registration {specific_registration} not found")) |
| 56 | + return |
| 57 | + |
| 58 | + manual_restart_logs = query.values_list('object_id', flat=True).distinct() |
| 59 | + |
| 60 | + registrations_to_check = Registration.objects.filter( |
| 61 | + pk__in=manual_restart_logs, |
| 62 | + ) |
| 63 | + |
| 64 | + self.stdout.write(f"Found {registrations_to_check.count()} manually restarted registrations to check") |
| 65 | + |
| 66 | + approvals_ready = [] |
| 67 | + skipped_registrations = [] |
| 68 | + |
| 69 | + for registration in registrations_to_check: |
| 70 | + status = self.should_auto_approve(registration) |
| 71 | + |
| 72 | + if status == 'ready': |
| 73 | + approval = registration.registration_approval |
| 74 | + if approval: |
| 75 | + approvals_ready.append(approval) |
| 76 | + self.stdout.write( |
| 77 | + self.style.SUCCESS(f"✓ Queuing registration {registration._id} for approval") |
| 78 | + ) |
| 79 | + else: |
| 80 | + skipped_registrations.append((registration._id, status)) |
| 81 | + self.stdout.write( |
| 82 | + self.style.WARNING(f"⚠ Skipping registration {registration._id}: {status}") |
| 83 | + ) |
| 84 | + |
| 85 | + if approvals_ready: |
| 86 | + if dry_run: |
| 87 | + self.stdout.write( |
| 88 | + self.style.WARNING(f"DRY RUN: Would approve {len(approvals_ready)} registrations") |
| 89 | + ) |
| 90 | + else: |
| 91 | + try: |
| 92 | + approve_past_pendings(approvals_ready, dry_run=False) |
| 93 | + self.stdout.write( |
| 94 | + self.style.SUCCESS(f"✓ Successfully approved {len(approvals_ready)} manually restarted registrations") |
| 95 | + ) |
| 96 | + except Exception as e: |
| 97 | + self.stdout.write( |
| 98 | + self.style.ERROR(f"✗ Error approving registrations: {e}") |
| 99 | + ) |
| 100 | + else: |
| 101 | + self.stdout.write('No registrations ready for approval') |
| 102 | + |
| 103 | + self.stdout.write(f"Total checked: {registrations_to_check.count()}") |
| 104 | + self.stdout.write(f"Ready for approval: {len(approvals_ready)}") |
| 105 | + self.stdout.write(f"Skipped: {len(skipped_registrations)}") |
| 106 | + |
| 107 | + if skipped_registrations: |
| 108 | + self.stdout.write('\nSkipped registrations:') |
| 109 | + for reg_id, reason in skipped_registrations: |
| 110 | + self.stdout.write(f" - {reg_id}: {reason}") |
| 111 | + |
| 112 | + def should_auto_approve(self, registration): |
| 113 | + if registration.is_public: |
| 114 | + return 'already public' |
| 115 | + |
| 116 | + if registration.is_registration_approved: |
| 117 | + return 'already approved' |
| 118 | + |
| 119 | + if registration.archiving: |
| 120 | + return 'still archiving' |
| 121 | + |
| 122 | + archive_job = registration.archive_job |
| 123 | + if archive_job and hasattr(archive_job, 'status'): |
| 124 | + if archive_job.status not in ['SUCCESS', None]: |
| 125 | + return f'archive status: {archive_job.status}' |
| 126 | + |
| 127 | + approval = registration.registration_approval |
| 128 | + if not approval: |
| 129 | + return 'no approval object' |
| 130 | + |
| 131 | + if approval.is_approved: |
| 132 | + return 'approval already approved' |
| 133 | + |
| 134 | + if approval.is_rejected: |
| 135 | + return 'approval was rejected' |
| 136 | + |
| 137 | + time_since_initiation = timezone.now() - approval.initiation_date |
| 138 | + if time_since_initiation < settings.REGISTRATION_APPROVAL_TIME: |
| 139 | + remaining = settings.REGISTRATION_APPROVAL_TIME - time_since_initiation |
| 140 | + return f'not ready yet ({remaining} remaining)' |
| 141 | + |
| 142 | + if registration.is_stuck_registration: |
| 143 | + return 'registration still stuck' |
| 144 | + |
| 145 | + return 'ready' |
0 commit comments