#NEWLINE# @ transaction . atomic #NEWLINE# def load_workflow( app_label , version_slug , force = False ) : #NEWLINE# #INDENT# workflow_directory = django_apps . get_app_config ( app_label ) . path #NEWLINE# data = parse_workflow_directory ( workflow_directory ) #NEWLINE# workflow_data = data [ 'workflow' ] #NEWLINE# ( workflow , workflow_created ) = Workflow . objects . update_or_create ( slug = workflow_data [ 'slug' ] , defaults = { #NEWLINE# #INDENT# 'name' : workflow_data [ 'name' ] , #NEWLINE# 'description' : workflow_data [ 'description' ] , #NEWLINE# 'code_directory' : workflow_directory , #NEWLINE# 'sample_data_load_function' : workflow_data . get ( 'sample_data_load_function' ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# for certification_data in workflow_data [ 'certifications' ] : #NEWLINE# #INDENT# Certification . objects . update_or_create ( slug = certification_data [ 'slug' ] , workflow = workflow , defaults = { #NEWLINE# #INDENT# 'name' : certification_data [ 'name' ] , #NEWLINE# 'description' : certification_data [ 'description' ] , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# for certification_data in workflow_data [ 'certifications' ] : #NEWLINE# #INDENT# certification = Certification . objects . get ( slug = certification_data [ 'slug' ] , workflow = workflow ) #NEWLINE# required_certification_slugs = certification_data . get ( 'required_certifications' , [ ] ) #NEWLINE# required_certifications = Certification . objects . filter ( workflow = workflow , slug__in = required_certification_slugs ) #NEWLINE# if ( required_certifications . count ( ) != len ( required_certification_slugs ) ) : #NEWLINE# #INDENT# raise WorkflowError ( 'Certification {} requires non-existent certification.' . format ( certification_data [ 'slug' ] ) ) #NEWLINE# #UNINDENT# certification . required_certifications = list ( required_certifications ) #NEWLINE# #UNINDENT# desired_versions = [ version_data for version_data in data [ 'versions' ] if ( required_certification_slugs [ 'slug' ] == version_slug ) ] #NEWLINE# if ( len ( desired_versions ) != 1 ) : #NEWLINE# #INDENT# raise WorkflowError ( 'Invalid version requested: {}' . format ( version_slug ) ) #NEWLINE# #UNINDENT# load_workflow_version ( desired_versions [ 0 ] , workflow , force = force )