<s> from __future__ import print_function <EOL> from datetime import date <EOL> import json <EOL> import logging <EOL> from casexml . apps . case . models import CommCareCase <EOL> from corehq . apps . fixtures . models import FixtureDataItem , FixtureDataType , FieldList , FixtureItemField <EOL> from couchdbkit import ResourceNotFound <EOL> from dimagi . ext . couchdbkit import * <EOL> from dimagi . utils . couch . cache import cache_core <EOL> import requests <EOL> from toggle . models import Toggle <EOL> logger = logging . getLogger ( __name__ ) <EOL> class Dhis2Settings ( Document ) : <EOL> domain = StringProperty ( ) <EOL> dhis2 = DictProperty ( ) <EOL> @ classmethod <EOL> def for_domain ( cls , domain ) : <EOL> res = cache_core . cached_view ( <EOL> cls . get_db ( ) , <EOL> "<STR_LIT>" , <EOL> key = [ domain , '<STR_LIT>' , None ] , <EOL> reduce = False , <EOL> include_docs = True , <EOL> wrapper = cls . wrap ) <EOL> return res [ <NUM_LIT:0> ] if len ( res ) > <NUM_LIT:0> else None <EOL> @ classmethod <EOL> def all_enabled ( cls ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> toggle = Toggle . get ( '<STR_LIT>' ) <EOL> except ResourceNotFound : <EOL> return <EOL> for domain in toggle . enabled_users : <EOL> if domain . startswith ( '<STR_LIT>' ) : <EOL> domain = domain . split ( '<STR_LIT::>' ) [ <NUM_LIT:1> ] <EOL> settings = cls . for_domain ( domain ) <EOL> if settings and settings . is_enabled ( ) : <EOL> yield settings <EOL> @ classmethod <EOL> def is_enabled_for_domain ( cls , domain ) : <EOL> settings = cls . for_domain ( domain ) <EOL> return settings is not None and settings . is_enabled ( ) <EOL> def is_enabled ( self ) : <EOL> return self . dhis2 [ '<STR_LIT>' ] <EOL> class JsonApiError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Dhis2ApiQueryError ( JsonApiError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Dhis2ConfigurationError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Dhis2IntegrationError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def json_serializer ( obj ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> return obj . isoformat ( ) <EOL> class JsonApiRequest ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , username , password ) : <EOL> self . baseurl = host + '<STR_LIT>' <EOL> self . headers = { '<STR_LIT>' : '<STR_LIT:application/json>' } <EOL> self . auth = ( username , password ) <EOL> @ staticmethod <EOL> def json_or_error ( response ) : <EOL> """<STR_LIT>""" <EOL> if <NUM_LIT:200> <= response . status_code < <NUM_LIT> : <EOL> return response . json ( ) <EOL> else : <EOL> raise JsonApiError ( '<STR_LIT>' % <EOL> ( response . url , response . status_code , response . text ) ) <EOL> def get ( self , path , ** kwargs ) : <EOL> logger . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> self . baseurl + path , self . headers , kwargs ) <EOL> try : <EOL> response = requests . get ( self . baseurl + path , headers = self . headers , auth = self . auth , ** kwargs ) <EOL> except requests . RequestException as err : <EOL> logger . exception ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:url>' : self . baseurl + path , '<STR_LIT>' : self . headers } , <EOL> err ) <EOL> raise JsonApiError ( str ( err ) ) <EOL> return JsonApiRequest . json_or_error ( response ) <EOL> def post ( self , path , data , ** kwargs ) : <EOL> headers = self . headers . copy ( ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> json_data = json . dumps ( data , default = json_serializer ) <EOL> logger . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> self . baseurl + path , self . headers , json_data , kwargs ) <EOL> try : <EOL> response = requests . post ( self . baseurl + path , json_data , headers = headers , auth = self . auth , ** kwargs ) <EOL> except requests . RequestException as err : <EOL> logger . exception ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:url>' : self . baseurl + path , '<STR_LIT:data>' : json_data , '<STR_LIT>' : headers } , <EOL> err ) <EOL> raise JsonApiError ( str ( err ) ) <EOL> return JsonApiRequest . json_or_error ( response ) <EOL> def put ( self , path , data , ** kwargs ) : <EOL> headers = self . headers . copy ( ) <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT:application/json>' <EOL> json_data = json . dumps ( data , default = json_serializer ) <EOL> logger . debug ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> self . baseurl + path , self . headers , json_data , kwargs ) <EOL> try : <EOL> response = requests . put ( self . baseurl + path , json_data , headers = headers , auth = self . auth , ** kwargs ) <EOL> except requests . RequestException as err : <EOL> logger . exception ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:url>' : self . baseurl + path , '<STR_LIT:data>' : json_data , '<STR_LIT>' : headers } , <EOL> err ) <EOL> raise JsonApiError ( str ( err ) ) <EOL> return JsonApiRequest . json_or_error ( response ) <EOL> class Dhis2Api ( object ) : <EOL> def __init__ ( self , host , username , password , top_org_unit_name = None ) : <EOL> self . _username = username <EOL> self . top_org_unit_name = top_org_unit_name <EOL> self . _top_org_unit = None <EOL> self . _request = JsonApiRequest ( host , username , password ) <EOL> self . _tracked_entity_attributes = { } <EOL> self . _not_tracked_entity_attributes = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> self . _data_elements = { } <EOL> def _fetch_tracked_entity_attributes ( self ) : <EOL> response = self . _request . get ( '<STR_LIT>' , params = { '<STR_LIT>' : '<STR_LIT:false>' , '<STR_LIT>' : '<STR_LIT:false>' } ) <EOL> for te in response [ '<STR_LIT>' ] : <EOL> self . _tracked_entity_attributes [ te [ '<STR_LIT:name>' ] ] = te [ '<STR_LIT:id>' ] <EOL> def _fetch_data_elements ( self ) : <EOL> response = self . _request . get ( '<STR_LIT>' , params = { '<STR_LIT>' : '<STR_LIT:false>' , '<STR_LIT>' : '<STR_LIT:false>' } ) <EOL> for de in response [ '<STR_LIT>' ] : <EOL> self . _data_elements [ de [ '<STR_LIT:name>' ] ] = de [ '<STR_LIT:id>' ] <EOL> def _data_to_attributes ( self , data ) : <EOL> """<STR_LIT>""" <EOL> for attr in self . _not_tracked_entity_attributes : <EOL> data . pop ( attr , None ) <EOL> if any ( key not in self . _tracked_entity_attributes for key in data ) : <EOL> self . _fetch_tracked_entity_attributes ( ) <EOL> attributes = [ { <EOL> '<STR_LIT>' : self . get_te_attr_id ( key ) , <EOL> '<STR_LIT:value>' : value <EOL> } for key , value in data . iteritems ( ) ] <EOL> return attributes <EOL> def add_te_inst ( self , te_name , ou_id , instance_data ) : <EOL> """<STR_LIT>""" <EOL> request_data = { <EOL> '<STR_LIT>' : self . get_te_id ( te_name ) , <EOL> '<STR_LIT>' : ou_id , <EOL> '<STR_LIT>' : self . _data_to_attributes ( instance_data ) <EOL> } <EOL> response = self . _request . post ( '<STR_LIT>' , request_data ) <EOL> if response [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> raise Dhis2ApiQueryError ( '<STR_LIT>' % <EOL> ( te_name , response ) ) <EOL> return response [ '<STR_LIT>' ] <EOL> def update_te_inst ( self , instance_data ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> te_inst_id = instance_data . pop ( '<STR_LIT>' ) <EOL> ou_id = instance_data . pop ( '<STR_LIT>' ) <EOL> except KeyError as err : <EOL> raise KeyError ( '<STR_LIT>' % err ) <EOL> request_data = { <EOL> '<STR_LIT>' : te_inst_id , <EOL> '<STR_LIT>' : ou_id , <EOL> '<STR_LIT>' : self . _data_to_attributes ( instance_data ) <EOL> } <EOL> response = self . _request . put ( '<STR_LIT>' + te_inst_id , request_data ) <EOL> if response [ '<STR_LIT:status>' ] != '<STR_LIT>' : <EOL> logger . error ( '<STR_LIT>' , <EOL> te_inst_id , response ) <EOL> def get_top_org_unit ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _top_org_unit is None : <EOL> if self . top_org_unit_name : <EOL> response = self . _request . get ( '<STR_LIT>' , <EOL> params = { '<STR_LIT>' : '<STR_LIT:false>' , <EOL> '<STR_LIT>' : self . top_org_unit_name } ) <EOL> self . _top_org_unit = response [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> else : <EOL> org_units_json = self . _request . get ( '<STR_LIT>' , params = { '<STR_LIT>' : '<STR_LIT:false>' } ) <EOL> org_unit = org_units_json [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> org_unit = self . _request . get ( '<STR_LIT>' + org_unit [ '<STR_LIT:id>' ] ) <EOL> while True : <EOL> if not org_unit . get ( '<STR_LIT>' ) : <EOL> break <EOL> org_unit = self . _request . get ( '<STR_LIT>' + org_unit [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] ) <EOL> self . _top_org_unit = org_unit <EOL> return self . _top_org_unit <EOL> def get_resource_id ( self , resource , name ) : <EOL> """<STR_LIT>""" <EOL> response = self . _request . get ( resource , params = { '<STR_LIT>' : '<STR_LIT:false>' , '<STR_LIT>' : name } ) <EOL> if not response [ resource ] : <EOL> return None <EOL> if len ( response [ resource ] ) > <NUM_LIT:1> : <EOL> raise Dhis2ApiQueryError ( '<STR_LIT>' ) <EOL> return response [ resource ] [ <NUM_LIT:0> ] [ '<STR_LIT:id>' ] <EOL> def get_program_id ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . get_resource_id ( '<STR_LIT>' , name ) <EOL> def get_program_stage_id ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . get_resource_id ( '<STR_LIT>' , name ) <EOL> def get_te_id ( self , name ) : <EOL> """<STR_LIT>""" <EOL> return self . get_resource_id ( '<STR_LIT>' , name ) <EOL> def get_te_attr_id ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if name not in self . _tracked_entity_attributes : <EOL> self . _tracked_entity_attributes [ name ] = self . get_resource_id ( '<STR_LIT>' , name ) <EOL> return self . _tracked_entity_attributes [ name ] <EOL> def get_te_inst ( self , te_inst_id ) : <EOL> """<STR_LIT>""" <EOL> inst = self . _request . get ( '<STR_LIT>' + te_inst_id ) <EOL> instance = { attr [ '<STR_LIT>' ] : attr [ '<STR_LIT:value>' ] for attr in inst [ '<STR_LIT>' ] } <EOL> instance . update ( { <EOL> '<STR_LIT>' : inst [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : inst [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : inst [ '<STR_LIT>' ] , <EOL> } ) <EOL> return instance <EOL> def gen_instances_with_unset ( self , te_name , attr_name ) : <EOL> """<STR_LIT>""" <EOL> top_ou = self . get_top_org_unit ( ) <EOL> te_id = self . get_te_id ( te_name ) <EOL> attr_id = self . get_te_attr_id ( attr_name ) <EOL> page = <NUM_LIT:1> <EOL> while True : <EOL> response = self . _request . get ( <EOL> '<STR_LIT>' , <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> '<STR_LIT>' : te_id , <EOL> '<STR_LIT>' : top_ou [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : attr_id <EOL> } ) <EOL> instances = self . entities_to_dicts ( response ) <EOL> for inst in instances : <EOL> if not inst . get ( attr_name ) : <EOL> yield inst <EOL> if page < response [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> page += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> def gen_instances_with_equals ( self , te_name , attr_name , attr_value ) : <EOL> """<STR_LIT>""" <EOL> top_ou = self . get_top_org_unit ( ) <EOL> te_id = self . get_te_id ( te_name ) <EOL> attr_id = self . get_te_attr_id ( attr_name ) <EOL> if attr_id is None : <EOL> raise Dhis2ConfigurationError ( <EOL> '<STR_LIT>' , <EOL> attr_name , self . _request . baseurl ) <EOL> page = <NUM_LIT:1> <EOL> while True : <EOL> response = self . _request . get ( <EOL> '<STR_LIT>' , <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> '<STR_LIT>' : te_id , <EOL> '<STR_LIT>' : top_ou [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : attr_id + '<STR_LIT>' + attr_value <EOL> } ) <EOL> instances = self . entities_to_dicts ( response ) <EOL> for inst in instances : <EOL> yield inst <EOL> if page < response [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> page += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> def gen_instances_in_program ( self , program ) : <EOL> """<STR_LIT>""" <EOL> top_ou = self . get_top_org_unit ( ) <EOL> program_id = self . get_program_id ( program ) <EOL> page = <NUM_LIT:1> <EOL> while True : <EOL> response = self . _request . get ( <EOL> '<STR_LIT>' , <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : '<STR_LIT:false>' , <EOL> '<STR_LIT>' : top_ou [ '<STR_LIT:id>' ] , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : program_id <EOL> } ) <EOL> instances = self . entities_to_dicts ( response ) <EOL> for inst in instances : <EOL> yield inst <EOL> if page < response [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> page += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> def gen_org_units ( self ) : <EOL> """<STR_LIT>""" <EOL> page = <NUM_LIT:1> <EOL> while True : <EOL> response = self . _request . get ( <EOL> '<STR_LIT>' , <EOL> params = { <EOL> '<STR_LIT>' : '<STR_LIT:true>' , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : page , <EOL> '<STR_LIT>' : '<STR_LIT:false>' <EOL> } ) <EOL> for org_unit in response [ '<STR_LIT>' ] : <EOL> yield org_unit <EOL> if page < response [ '<STR_LIT>' ] [ '<STR_LIT>' ] : <EOL> page += <NUM_LIT:1> <EOL> else : <EOL> break <EOL> def gen_org_units_with_parents ( self ) : <EOL> """<STR_LIT>""" <EOL> for ou in self . gen_org_units ( ) : <EOL> ou [ '<STR_LIT>' ] = self . get_org_unit_parent_id ( ou [ '<STR_LIT:id>' ] ) <EOL> yield ou <EOL> def get_org_unit_parent_id ( self , ou_id ) : <EOL> details = self . _request . get ( '<STR_LIT>' + ou_id ) <EOL> return details [ '<STR_LIT>' ] [ '<STR_LIT:id>' ] if details . get ( '<STR_LIT>' ) else None <EOL> def enroll_in ( self , te_inst_id , program , when = None , data = None ) : <EOL> """<STR_LIT>""" <EOL> program_id = self . get_resource_id ( '<STR_LIT>' , program ) <EOL> return self . enroll_in_id ( te_inst_id , program_id , when , data ) <EOL> def enroll_in_id ( self , te_inst_id , program_id , when = None , data = None ) : <EOL> """<STR_LIT>""" <EOL> if when is None : <EOL> when = date . today ( ) <EOL> request_data = { <EOL> "<STR_LIT>" : te_inst_id , <EOL> "<STR_LIT>" : program_id , <EOL> "<STR_LIT>" : when , <EOL> "<STR_LIT>" : when <EOL> } <EOL> if data : <EOL> request_data [ '<STR_LIT>' ] = self . _data_to_attributes ( data ) <EOL> return self . _request . post ( '<STR_LIT>' , request_data ) <EOL> def enrolled_in ( self , te_inst_id , program ) : <EOL> """<STR_LIT>""" <EOL> return any ( inst [ '<STR_LIT>' ] == te_inst_id for inst in self . gen_instances_in_program ( program ) ) <EOL> def _get_event_data_values ( self , xform , data_element_names ) : <EOL> """<STR_LIT>""" <EOL> data_values = [ ] <EOL> for field , element_name in data_element_names . iteritems ( ) : <EOL> if isinstance ( field , tuple ) : <EOL> field_name , field_value = field <EOL> if field_value in xform . form . get ( field_name , '<STR_LIT>' ) . split ( ) : <EOL> data_values . append ( { <EOL> '<STR_LIT>' : self . _data_elements [ element_name ] , <EOL> '<STR_LIT:value>' : True <EOL> } ) <EOL> else : <EOL> if field in xform . form : <EOL> data_values . append ( { <EOL> '<STR_LIT>' : self . _data_elements [ element_name ] , <EOL> '<STR_LIT:value>' : xform . form [ field ] <EOL> } ) <EOL> return data_values <EOL> def form_to_event ( self , program_id , xform , data_element_names , program_stage_id = None , te_inst_id = None ) : <EOL> """<STR_LIT>""" <EOL> if any ( e not in self . _data_elements for e in data_element_names . values ( ) ) : <EOL> self . _fetch_data_elements ( ) <EOL> if xform . form . get ( '<STR_LIT>' ) : <EOL> org_unit = xform . form [ '<STR_LIT>' ] <EOL> else : <EOL> case_id = xform . form [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> case = CommCareCase . get ( case_id ) <EOL> if getattr ( case , '<STR_LIT>' , None ) : <EOL> org_unit = case [ '<STR_LIT>' ] <EOL> else : <EOL> return <EOL> event = { <EOL> '<STR_LIT>' : program_id , <EOL> '<STR_LIT>' : org_unit , <EOL> '<STR_LIT>' : xform . received_on . date ( ) , <EOL> '<STR_LIT:status>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : self . _username , <EOL> '<STR_LIT>' : self . _get_event_data_values ( xform , data_element_names ) <EOL> } <EOL> if program_stage_id : <EOL> event [ '<STR_LIT>' ] = program_stage_id <EOL> if te_inst_id : <EOL> event [ '<STR_LIT>' ] = te_inst_id <EOL> if xform . metadata . location : <EOL> event [ '<STR_LIT>' ] = { <EOL> '<STR_LIT>' : xform . metadata . location . latitude , <EOL> '<STR_LIT>' : xform . metadata . location . longitude , <EOL> } <EOL> return event <EOL> def send_events ( self , events ) : <EOL> """<STR_LIT>""" <EOL> return self . _request . post ( '<STR_LIT>' , events ) <EOL> @ staticmethod <EOL> def entities_to_dicts ( response ) : <EOL> """<STR_LIT>""" <EOL> entities = [ ] <EOL> for row in response [ '<STR_LIT>' ] : <EOL> entity = { } <EOL> for i , item in enumerate ( row ) : <EOL> if response [ '<STR_LIT>' ] [ i ] [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> item = response [ '<STR_LIT>' ] [ '<STR_LIT>' ] [ item ] <EOL> entity [ response [ '<STR_LIT>' ] [ i ] [ '<STR_LIT>' ] ] = item <EOL> entities . append ( entity ) <EOL> return entities <EOL> class FixtureManagerError ( Exception ) : <EOL> pass <EOL> def to_field_list ( value ) : <EOL> """<STR_LIT>""" <EOL> return FieldList ( field_list = [ FixtureItemField ( field_value = value , properties = { } ) ] ) <EOL> def to_field_value ( field_list ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return field_list . field_list [ <NUM_LIT:0> ] . field_value <EOL> except IndexError : <EOL> return None <EOL> class FixtureManager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , model_class , domain , tag ) : <EOL> self . model_class = model_class <EOL> self . domain = domain <EOL> self . tag = tag <EOL> def get ( self , fixture_id ) : <EOL> item = FixtureDataItem . get ( fixture_id ) <EOL> fields = { k : to_field_value ( v ) for k , v in item . fields . iteritems ( ) } <EOL> return self . model_class ( _fixture_id = item . get_id , ** fields ) <EOL> def all ( self ) : <EOL> for item in FixtureDataItem . get_item_list ( self . domain , self . tag ) : <EOL> fields = { k : to_field_value ( v ) for k , v in item . fields . iteritems ( ) } <EOL> yield self . model_class ( _fixture_id = item . get_id , ** fields ) <EOL> class Dhis2OrgUnit ( object ) : <EOL> """<STR_LIT>""" <EOL> objects = None <EOL> def __init__ ( self , id , name , parent_id , _fixture_id = None ) : <EOL> self . id = id <EOL> self . name = name <EOL> self . parent_id = parent_id <EOL> self . _fixture_id = _fixture_id <EOL> def get_id ( self ) : <EOL> return self . _fixture_id <EOL> def save ( self ) : <EOL> if self . objects is None : <EOL> raise FixtureManagerError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> data_type = FixtureDataType . by_domain_tag ( self . objects . domain , self . objects . tag ) . one ( ) <EOL> if data_type is None : <EOL> raise Dhis2ConfigurationError ( <EOL> '<STR_LIT>' % <EOL> ( self . objects . domain , self . objects . tag ) ) <EOL> data_item = FixtureDataItem ( <EOL> data_type_id = data_type . get_id , <EOL> domain = self . objects . domain , <EOL> fields = { <EOL> '<STR_LIT:id>' : to_field_list ( self . id ) , <EOL> '<STR_LIT:name>' : to_field_list ( self . name ) , <EOL> '<STR_LIT>' : to_field_list ( self . parent_id ) <EOL> } ) <EOL> data_item . save ( ) <EOL> self . _fixture_id = data_item . get_id <EOL> return self . _fixture_id <EOL> def delete ( self ) : <EOL> if self . _fixture_id is None : <EOL> return <EOL> item = FixtureDataItem . get ( self . _fixture_id ) <EOL> item . delete ( ) </s>
