<s> from io import BytesIO <EOL> from django . utils . encoding import force_unicode <EOL> from tastypie . bundle import Bundle <EOL> from tastypie . serializers import Serializer , get_type_string <EOL> import defusedxml . lxml as lxml <EOL> from lxml . etree import Element <EOL> class CommCareCaseSerializer ( Serializer ) : <EOL> '''<STR_LIT>''' <EOL> def case_to_etree ( self , case ) : <EOL> '''<STR_LIT>''' <EOL> return lxml . parse ( BytesIO ( case . to_xml ( '<STR_LIT>' ) ) ) . getroot ( ) <EOL> def bundle_to_etree ( self , bundle ) : <EOL> '''<STR_LIT>''' <EOL> return self . case_to_etree ( bundle . obj ) <EOL> def to_etree ( self , data , options = None , name = None , depth = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( data , ( list , tuple ) ) : <EOL> element = Element ( name or '<STR_LIT>' ) <EOL> if name : <EOL> element = Element ( name ) <EOL> element . set ( '<STR_LIT:type>' , '<STR_LIT:list>' ) <EOL> else : <EOL> element = Element ( '<STR_LIT>' ) <EOL> for item in data : <EOL> element . append ( self . to_etree ( item , options , depth = depth + <NUM_LIT:1> ) ) <EOL> elif isinstance ( data , dict ) : <EOL> if depth == <NUM_LIT:0> : <EOL> element = Element ( name or '<STR_LIT>' ) <EOL> else : <EOL> element = Element ( name or '<STR_LIT:object>' ) <EOL> element . set ( '<STR_LIT:type>' , '<STR_LIT>' ) <EOL> for ( key , value ) in data . iteritems ( ) : <EOL> element . append ( self . to_etree ( value , options , name = key , depth = depth + <NUM_LIT:1> ) ) <EOL> elif isinstance ( data , Bundle ) : <EOL> element = self . bundle_to_etree ( data ) <EOL> elif hasattr ( data , '<STR_LIT>' ) : <EOL> if getattr ( data , '<STR_LIT>' , None ) == '<STR_LIT>' and data . is_m2m == False : <EOL> if data . full : <EOL> return self . to_etree ( data . fk_resource , options , name , depth + <NUM_LIT:1> ) <EOL> else : <EOL> return self . to_etree ( data . value , options , name , depth + <NUM_LIT:1> ) <EOL> elif getattr ( data , '<STR_LIT>' , None ) == '<STR_LIT>' and data . is_m2m == True : <EOL> if data . full : <EOL> element = Element ( name or '<STR_LIT>' ) <EOL> for bundle in data . m2m_bundles : <EOL> element . append ( self . to_etree ( bundle , options , bundle . resource_name , depth + <NUM_LIT:1> ) ) <EOL> else : <EOL> element = Element ( name or '<STR_LIT>' ) <EOL> for value in data . value : <EOL> element . append ( self . to_etree ( value , options , name , depth = depth + <NUM_LIT:1> ) ) <EOL> else : <EOL> return self . to_etree ( data . value , options , name ) <EOL> else : <EOL> element = Element ( name or '<STR_LIT:value>' ) <EOL> simple_data = self . to_simple ( data , options ) <EOL> data_type = get_type_string ( simple_data ) <EOL> if data_type != '<STR_LIT:string>' : <EOL> element . set ( '<STR_LIT:type>' , get_type_string ( simple_data ) ) <EOL> if data_type != '<STR_LIT:null>' : <EOL> if isinstance ( simple_data , unicode ) : <EOL> element . text = simple_data <EOL> else : <EOL> element . text = force_unicode ( simple_data ) <EOL> return element <EOL> class CustomXMLSerializer ( Serializer ) : <EOL> def to_etree ( self , data , options = None , name = None , depth = <NUM_LIT:0> ) : <EOL> if isinstance ( name , basestring ) : <EOL> name = name . strip ( ) <EOL> etree = super ( CustomXMLSerializer , self ) . to_etree ( data , options , name , depth ) <EOL> id = etree . find ( '<STR_LIT:id>' ) <EOL> if id is not None : <EOL> etree . attrib [ '<STR_LIT:id>' ] = id . findtext ( '<STR_LIT:.>' ) <EOL> etree . remove ( id ) <EOL> return etree <EOL> class XFormInstanceSerializer ( Serializer ) : <EOL> def to_xml ( self , data , options = None ) : <EOL> if isinstance ( data , Bundle ) : <EOL> return data . obj . get_xml ( ) <EOL> else : <EOL> return super ( XFormInstanceSerializer , self ) . to_xml ( data , options = options ) </s>
