3737from framework import sentry
3838from framework .exceptions import HTTPError
3939from osf import features
40- from osf .models import Node , NodeLog , Registration , BaseFileNode
40+ from osf .models import Node , NodeLog , Registration , BaseFileNode , ArchiveJob
4141from osf .models .files import TrashedFileNode
4242from osf .utils .requests import get_current_request
4343from osf .exceptions import RegistrationStuckRecoverableException , RegistrationStuckBrokenException
@@ -433,18 +433,29 @@ def archive_registrations(*args, **kwargs):
433433 ARCHIVED .append (reg )
434434 VERIFIED .remove (reg )
435435
436- def verify (registration , permissible_addons = DEFAULT_PERMISSIBLE_ADDONS , raise_error = False ):
436+ def verify (registration , permissible_addons = DEFAULT_PERMISSIBLE_ADDONS , verify_addons = True , raise_error = False ):
437437 permissible_addons = set (permissible_addons )
438438 maybe_suppress_error = contextlib .suppress (ValidationError ) if not raise_error else contextlib .nullcontext (enter_result = False )
439439
440- for reg in registration .node_and_primary_descendants ():
440+ # Collect all registrations and prefetch archive_jobs to avoid N+1 queries
441+ registrations = list (registration .node_and_primary_descendants ())
442+ registration_ids = [reg .id for reg in registrations ]
443+
444+ archive_jobs_dict = {
445+ job .dst_node_id : job
446+ for job in ArchiveJob .objects .filter (dst_node_id__in = registration_ids ).only ('dst_node_id' , 'status' )
447+ }
448+
449+ for reg in registrations :
441450 logger .info (f'Verifying { reg ._id } ' )
442- if reg .archive_job .status == ARCHIVER_SUCCESS :
451+ archive_job = archive_jobs_dict .get (reg .id ) or getattr (reg , 'archive_job' , None )
452+ if archive_job and archive_job .status == ARCHIVER_SUCCESS :
443453 continue
454+
444455 nonignorable_logs = get_logs_to_revert (reg )
445456 unacceptable_logs = nonignorable_logs .exclude (action__in = LOG_GREYLIST )
446457 if unacceptable_logs .exists ():
447- if len (permissible_addons ) == 1 or unacceptable_logs .exclude (action__in = PERMISSIBLE_BLACKLIST ):
458+ if len (permissible_addons ) == 1 or unacceptable_logs .exclude (action__in = PERMISSIBLE_BLACKLIST ). exists () :
448459 message = '{}: Original node {} has unacceptable logs: {}' .format (
449460 registration ._id ,
450461 reg .registered_from ._id ,
@@ -456,30 +467,23 @@ def verify(registration, permissible_addons=DEFAULT_PERMISSIBLE_ADDONS, raise_er
456467 raise ValidationError (message )
457468
458469 return False
459- if nonignorable_logs .filter (action__in = VERIFY_PROVIDER ).exists ():
460- for log in nonignorable_logs .filter (action__in = VERIFY_PROVIDER ):
461- for key in ['source' , 'destination' ]:
462- if key in log .params :
463- if log .params [key ]['provider' ] != 'osfstorage' :
464- message = '{}: {} Only OSFS moves and renames are permissible' .format (
465- registration ._id ,
466- log ._id
467- )
468- logger .error (message )
469-
470- with maybe_suppress_error :
471- raise ValidationError (message )
472-
473- return False
474- addons = reg .registered_from .get_addon_names ()
475- if set (addons ) - set (permissible_addons | {'wiki' }) != set ():
476- message = f'{ registration ._id } : Original node { reg .registered_from ._id } has addons: { addons } '
477- logger .error (message )
478-
479- with maybe_suppress_error :
480- raise ValidationError (message )
481-
482- return False
470+
471+ verify_provider_logs = nonignorable_logs .filter (action__in = VERIFY_PROVIDER ).only ('params' , '_id' )
472+ for log in verify_provider_logs :
473+ for key in ['source' , 'destination' ]:
474+ if key in log .params :
475+ if log .params [key ].get ('provider' ) != 'osfstorage' :
476+ message = '{}: {} Only OSFS moves and renames are permissible' .format (
477+ registration ._id ,
478+ log ._id
479+ )
480+ logger .error (message )
481+
482+ with maybe_suppress_error :
483+ raise ValidationError (message )
484+
485+ return False
486+
483487 if nonignorable_logs .exists ():
484488 logger .info ('{}: Original node {} has had revertable file operations' .format (
485489 registration ._id ,
@@ -490,6 +494,18 @@ def verify(registration, permissible_addons=DEFAULT_PERMISSIBLE_ADDONS, raise_er
490494 registration ._id ,
491495 reg .registered_from ._id
492496 ))
497+
498+ if verify_addons :
499+ addons = reg .registered_from .get_addon_names ()
500+ if set (addons ) - set (permissible_addons | {'wiki' }) != set ():
501+ message = f'{ registration ._id } : Original node { reg .registered_from ._id } has addons: { addons } '
502+ logger .error (message )
503+
504+ with maybe_suppress_error :
505+ raise ValidationError (message )
506+
507+ return False
508+
493509 return True
494510
495511def verify_registrations (registration_ids , permissible_addons ):
0 commit comments