<s> from django . db import transaction <EOL> from orchestra . models import Certification <EOL> from orchestra . models import WorkerCertification <EOL> from orchestra . models import Workflow <EOL> import logging <EOL> logger = logging . getLogger ( __name__ ) <EOL> @ transaction . atomic <EOL> def migrate_certifications ( source_workflow_slug , <EOL> destination_workflow_slug , <EOL> certification_slugs ) : <EOL> source_worker_certifications = WorkerCertification . objects . filter ( <EOL> certification__workflow__slug = source_workflow_slug ) <EOL> source_workflow = Workflow . objects . get ( slug = source_workflow_slug ) <EOL> source_certifications = source_workflow . certifications <EOL> if certification_slugs : <EOL> source_certifications = source_certifications . filter ( <EOL> slug__in = certification_slugs ) <EOL> not_found_certifications = set ( certification_slugs ) - set ( list ( <EOL> source_certifications . values_list ( '<STR_LIT>' , flat = True ) ) ) <EOL> if not_found_certifications : <EOL> logger . exception ( <EOL> "<STR_LIT>" . format ( <EOL> not_found_certifications ) ) <EOL> for source_certification in source_certifications . all ( ) : <EOL> certification_slug = source_certification . slug <EOL> logger . warning ( '<STR_LIT>' . format ( <EOL> certification_slug , <EOL> source_workflow_slug , <EOL> destination_workflow_slug ) ) <EOL> try : <EOL> destination_certification = Certification . objects . get ( <EOL> workflow__slug = destination_workflow_slug , <EOL> slug = certification_slug ) <EOL> except Certification . DoesNotExist : <EOL> logger . exception ( <EOL> ( "<STR_LIT>" <EOL> . format ( certification_slug , destination_workflow_slug ) ) ) <EOL> continue <EOL> for worker_certification in source_worker_certifications . filter ( <EOL> certification = source_certification ) . order_by ( '<STR_LIT>' ) : <EOL> WorkerCertification . objects . get_or_create ( <EOL> certification = destination_certification , <EOL> worker = worker_certification . worker , <EOL> task_class = worker_certification . task_class , <EOL> role = worker_certification . role ) </s>
