@task
@write
def add_validation_jobs(pks, job_pk, **kw):
    log.info('[%s@None] Adding validation jobs for addons starting at: %s '
             ' for job: %s'
             % (len(pks), pks[0], job_pk))

    job = ValidationJob.objects.get(pk=job_pk)
    curr_ver = job.curr_max_version.version_int
    target_ver = job.target_version.version_int
    prelim_app = list(amo.UNDER_REVIEW_STATUSES) + [amo.STATUS_BETA]
    for addon in Addon.objects.filter(pk__in=pks):
        ids = []
        base = addon.versions.filter(apps__application=job.application,
                                     apps__max__version_int__gte=curr_ver,
                                     apps__max__version_int__lt=target_ver)

        already_compat = addon.versions.filter(
            files__status=amo.STATUS_PUBLIC,
            apps__max__version_int__gte=target_ver)
        if already_compat.count():
            log.info('Addon %s already has a public version %r which is '
                     'compatible with target version of app %s %s (or newer)'
                     % (addon.pk, [v.pk for v in already_compat.all()],
                        job.application, job.target_version))
            continue

        try:
            public = (base.filter(files__status=amo.STATUS_PUBLIC)
                          .latest('id'))
        except __HOLE__:
            public = None

        if public:
            ids.extend([f.id for f in public.files.all()])
            ids.extend(base.filter(files__status__in=prelim_app,
                                   id__gt=public.id)
                           .values_list('files__id', flat=True))

        else:
            try:
                prelim = (base.filter(files__status__in=amo.LITE_STATUSES)
                              .latest('id'))
            except ObjectDoesNotExist:
                prelim = None

            if prelim:
                ids.extend([f.id for f in prelim.files.all()])
                ids.extend(base.filter(files__status__in=prelim_app,
                                       id__gt=prelim.id)
                               .values_list('files__id', flat=True))

            else:
                ids.extend(base.filter(files__status__in=prelim_app)
                               .values_list('files__id', flat=True))

        ids = set(ids)  # Just in case.
        log.info('Adding %s files for validation for '
                 'addon: %s for job: %s' % (len(ids), addon.pk, job_pk))
        for id in set(ids):
            result = ValidationResult.objects.create(validation_job_id=job_pk,
                                                     file_id=id)
            bulk_validate_file.delay(result.pk)
dataset/ETHPy150Open mozilla/addons-server/src/olympia/zadmin/tasks.py/add_validation_jobs