<s> """<STR_LIT>""" <EOL> from lxml import etree <EOL> from django . core . management . base import BaseCommand <EOL> from corehq . apps . app_manager . const import APP_V2 <EOL> from corehq . apps . app_manager . models import Application , Module , OpenCaseAction , UpdateCaseAction , PreloadAction <EOL> from corehq . apps . app_manager . xform_builder import XFormBuilder <EOL> from custom . openclinica . utils import odm_nsmap <EOL> ODK_DATA_TYPES = { <EOL> '<STR_LIT:text>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : '<STR_LIT:int>' , <EOL> '<STR_LIT:float>' : '<STR_LIT>' , <EOL> '<STR_LIT:date>' : '<STR_LIT:date>' , <EOL> '<STR_LIT:time>' : '<STR_LIT:time>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:string>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : '<STR_LIT:date>' , <EOL> '<STR_LIT>' : '<STR_LIT:time>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : '<STR_LIT:string>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> class StudyObject ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , defn , meta ) : <EOL> """<STR_LIT>""" <EOL> self . defn = defn <EOL> self . meta = meta <EOL> class Study ( StudyObject ) : <EOL> def __init__ ( self , defn , meta ) : <EOL> super ( Study , self ) . __init__ ( defn , meta ) <EOL> self . oid = defn . get ( '<STR_LIT>' ) <EOL> self . name = defn . xpath ( '<STR_LIT>' , namespaces = odm_nsmap ) [ <NUM_LIT:0> ] . text <EOL> def iter_events ( self ) : <EOL> for se_ref in self . meta . xpath ( '<STR_LIT>' , namespaces = odm_nsmap ) : <EOL> se_oid = se_ref . get ( '<STR_LIT>' ) <EOL> se_def = self . meta . xpath ( '<STR_LIT>' . format ( se_oid ) , namespaces = odm_nsmap ) [ <NUM_LIT:0> ] <EOL> yield StudyEvent ( se_def , self . meta ) <EOL> @ staticmethod <EOL> def get_subject_form_source ( name ) : <EOL> """<STR_LIT>""" <EOL> xform = XFormBuilder ( name ) <EOL> xform . new_question ( '<STR_LIT:name>' , '<STR_LIT>' ) <EOL> xform . new_question ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> xform . new_question ( '<STR_LIT>' , '<STR_LIT>' , data_type = '<STR_LIT:date>' ) <EOL> xform . new_question ( '<STR_LIT>' , '<STR_LIT>' , data_type = '<STR_LIT:int>' , choices = { <NUM_LIT:1> : '<STR_LIT>' , <NUM_LIT:2> : '<STR_LIT>' } ) <EOL> xform . new_question ( '<STR_LIT>' , '<STR_LIT>' , data_type = '<STR_LIT:date>' ) <EOL> return xform . tostring ( pretty_print = True ) <EOL> def new_subject_module ( self , app ) : <EOL> module = app . add_module ( Module . new_module ( '<STR_LIT>' , None ) ) <EOL> module . unique_id = '<STR_LIT>' <EOL> module . case_type = '<STR_LIT>' <EOL> reg_form = module . new_form ( '<STR_LIT>' , None ) <EOL> reg_form . unique_id = '<STR_LIT>' <EOL> reg_form . source = self . get_subject_form_source ( '<STR_LIT>' ) <EOL> reg_form . actions . open_case = OpenCaseAction ( name_path = '<STR_LIT>' , external_id = None ) <EOL> reg_form . actions . open_case . condition . type = '<STR_LIT>' <EOL> reg_form . actions . update_case = UpdateCaseAction ( update = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> reg_form . actions . update_case . condition . type = '<STR_LIT>' <EOL> edit_form = module . new_form ( '<STR_LIT>' , None ) <EOL> edit_form . unique_id = '<STR_LIT>' <EOL> edit_form . source = self . get_subject_form_source ( '<STR_LIT>' ) <EOL> edit_form . requires = '<STR_LIT>' <EOL> edit_form . actions . case_preload = PreloadAction ( preload = { <EOL> '<STR_LIT>' : '<STR_LIT:name>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> edit_form . actions . case_preload . condition . type = '<STR_LIT>' <EOL> edit_form . actions . update_case = UpdateCaseAction ( update = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> edit_form . actions . update_case . condition . type = '<STR_LIT>' <EOL> return module <EOL> def get_new_app ( self , domain_name , app_name , version = APP_V2 ) : <EOL> app = Application . new_app ( domain_name , app_name , application_version = version ) <EOL> app . name = self . name <EOL> subject_module = self . new_subject_module ( app ) <EOL> for event in self . iter_events ( ) : <EOL> module = event . new_module_for_app ( app , subject_module ) <EOL> for study_form in event . iter_forms ( ) : <EOL> study_form . add_new_form_to_module ( module ) <EOL> return app <EOL> class StudyEvent ( StudyObject ) : <EOL> def __init__ ( self , defn , meta ) : <EOL> super ( StudyEvent , self ) . __init__ ( defn , meta ) <EOL> self . oid = defn . get ( '<STR_LIT>' ) <EOL> self . name = defn . get ( '<STR_LIT:Name>' ) <EOL> self . is_repeating = defn . get ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> self . event_type = defn . get ( '<STR_LIT>' ) <EOL> self . unique_id = self . oid . lower ( ) <EOL> def iter_forms ( self ) : <EOL> for form_ref in self . defn . xpath ( '<STR_LIT>' , namespaces = odm_nsmap ) : <EOL> form_oid = form_ref . get ( '<STR_LIT>' ) <EOL> form_def = self . meta . xpath ( '<STR_LIT>' . format ( form_oid ) , namespaces = odm_nsmap ) [ <NUM_LIT:0> ] <EOL> yield StudyForm ( form_def , self . meta ) <EOL> def new_module_for_app ( self , app , subject_module ) : <EOL> """<STR_LIT>""" <EOL> module = app . add_module ( Module . new_module ( self . name , None ) ) <EOL> module . unique_id = self . unique_id <EOL> module . case_type = '<STR_LIT>' <EOL> module . root_module_id = subject_module . unique_id <EOL> module . parent_select . active = True <EOL> module . parent_select . module_id = subject_module . unique_id <EOL> return module <EOL> class StudyForm ( StudyObject ) : <EOL> def __init__ ( self , defn , meta ) : <EOL> super ( StudyForm , self ) . __init__ ( defn , meta ) <EOL> self . oid = defn . get ( '<STR_LIT>' ) <EOL> self . name = defn . get ( '<STR_LIT:Name>' ) <EOL> self . is_repeating = defn . get ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> self . unique_id = self . oid . lower ( ) <EOL> def iter_item_groups ( self ) : <EOL> for ig_ref in self . defn . xpath ( '<STR_LIT>' , namespaces = odm_nsmap ) : <EOL> ig_oid = ig_ref . get ( '<STR_LIT>' ) <EOL> ig_def = self . meta . xpath ( '<STR_LIT>' . format ( ig_oid ) , namespaces = odm_nsmap ) [ <NUM_LIT:0> ] <EOL> yield ItemGroup ( ig_def , self . meta ) <EOL> def add_new_form_to_module ( self , module ) : <EOL> """<STR_LIT>""" <EOL> form = module . new_form ( self . name , None ) <EOL> form . unique_id = self . unique_id <EOL> form . source = self . build_xform ( ) <EOL> def build_xform ( self ) : <EOL> xform = XFormBuilder ( self . name ) <EOL> for ig in self . iter_item_groups ( ) : <EOL> data_type = '<STR_LIT>' if self . is_repeating else '<STR_LIT>' <EOL> group = xform . new_group ( ig . question_name , ig . question_label , data_type ) <EOL> for item in ig . iter_items ( ) : <EOL> group . new_question ( item . question_name , item . question_label , ODK_DATA_TYPES [ item . data_type ] , <EOL> choices = item . choices ) <EOL> return xform . tostring ( pretty_print = True , encoding = '<STR_LIT:utf-8>' , xml_declaration = True ) <EOL> class ItemGroup ( StudyObject ) : <EOL> def __init__ ( self , defn , meta ) : <EOL> super ( ItemGroup , self ) . __init__ ( defn , meta ) <EOL> self . oid = defn . get ( '<STR_LIT>' ) <EOL> self . name = defn . get ( '<STR_LIT:Name>' ) <EOL> self . is_repeating = defn . get ( '<STR_LIT>' ) == '<STR_LIT>' <EOL> self . sas_dataset_name = defn . get ( '<STR_LIT>' ) <EOL> self . comment = defn . get ( '<STR_LIT>' ) <EOL> self . question_name = self . oid . lower ( ) <EOL> self . question_label = self . name <EOL> def iter_items ( self ) : <EOL> for item_ref in self . defn . xpath ( '<STR_LIT>' , namespaces = odm_nsmap ) : <EOL> item_oid = item_ref . get ( '<STR_LIT>' ) <EOL> item_def = self . meta . xpath ( '<STR_LIT>' . format ( item_oid ) , namespaces = odm_nsmap ) [ <NUM_LIT:0> ] <EOL> yield Item ( item_def , self . meta , self ) <EOL> class Item ( StudyObject ) : <EOL> def __init__ ( self , defn , meta , item_group ) : <EOL> super ( Item , self ) . __init__ ( defn , meta ) <EOL> self . oid = defn . get ( '<STR_LIT>' ) <EOL> self . name = defn . get ( '<STR_LIT:Name>' ) <EOL> self . data_type = defn . get ( '<STR_LIT>' ) <EOL> self . length = defn . get ( '<STR_LIT>' ) <EOL> self . sas_field_name = defn . get ( '<STR_LIT>' ) <EOL> self . comment = defn . get ( '<STR_LIT>' ) <EOL> self . item_group = item_group <EOL> cl_ref = defn . xpath ( '<STR_LIT>' , namespaces = odm_nsmap ) <EOL> if cl_ref : <EOL> self . data_type = '<STR_LIT>' <EOL> self . choices = self . get_choices ( cl_ref [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) ) <EOL> else : <EOL> self . choices = None <EOL> self . question_name = self . oid . lower ( ) <EOL> text = defn . xpath ( '<STR_LIT>' , namespaces = odm_nsmap ) <EOL> self . question_label = text [ <NUM_LIT:0> ] . text . strip ( ) if text else self . name <EOL> def get_choices ( self , cl_oid ) : <EOL> choices = { } <EOL> cl_def = self . meta . xpath ( '<STR_LIT>' . format ( cl_oid ) , namespaces = odm_nsmap ) [ <NUM_LIT:0> ] <EOL> for cl_item in cl_def : <EOL> value = cl_item . get ( '<STR_LIT>' ) <EOL> label = cl_item . xpath ( '<STR_LIT>' , namespaces = odm_nsmap ) [ <NUM_LIT:0> ] . text <EOL> choices [ value ] = label <EOL> return choices <EOL> class Command ( BaseCommand ) : <EOL> help = '<STR_LIT>' <EOL> args = '<STR_LIT>' <EOL> def handle ( self , * args , ** options ) : <EOL> domain_name , app_name , odm_filename = args <EOL> study = self . get_study ( odm_filename ) <EOL> app = study . get_new_app ( domain_name , app_name ) <EOL> app . save ( ) <EOL> @ staticmethod <EOL> def get_study ( odm_filename ) : <EOL> with open ( odm_filename ) as odm_file : <EOL> odm = etree . parse ( odm_file ) <EOL> meta = odm . xpath ( '<STR_LIT>' , namespaces = odm_nsmap ) [ <NUM_LIT:0> ] <EOL> study_def = odm . xpath ( '<STR_LIT>' , namespaces = odm_nsmap ) [ <NUM_LIT:0> ] <EOL> return Study ( study_def , meta ) </s>
