<s> from collections import defaultdict <EOL> from lxml import etree , objectify <EOL> import oauth2 <EOL> from outcome_response import OutcomeResponse <EOL> from utils import InvalidLTIConfigError <EOL> REPLACE_REQUEST = '<STR_LIT>' <EOL> DELETE_REQUEST = '<STR_LIT>' <EOL> READ_REQUEST = '<STR_LIT>' <EOL> accessors = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> ] <EOL> class OutcomeRequest ( ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , opts = defaultdict ( lambda : None ) ) : <EOL> for accessor in accessors : <EOL> setattr ( self , accessor , None ) <EOL> for ( key , val ) in opts . iteritems ( ) : <EOL> setattr ( self , key , val ) <EOL> @ staticmethod <EOL> def from_post_request ( post_request ) : <EOL> '''<STR_LIT>''' <EOL> request = OutcomeRequest ( ) <EOL> request . post_request = post_request <EOL> request . process_xml ( post_request . data ) <EOL> return request <EOL> def post_replace_result ( self , score , result_data = None ) : <EOL> '''<STR_LIT>''' <EOL> self . operation = REPLACE_REQUEST <EOL> self . score = score <EOL> self . result_data = result_data <EOL> if result_data is not None : <EOL> if len ( result_data ) > <NUM_LIT:1> : <EOL> error_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' . format ( len ( result_data ) ) ) <EOL> raise InvalidLTIConfigError ( error_msg ) <EOL> elif '<STR_LIT:text>' not in result_data and '<STR_LIT:url>' not in result_data : <EOL> error_msg = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> raise InvalidLTIConfigError ( error_msg ) <EOL> else : <EOL> return self . post_outcome_request ( ) <EOL> else : <EOL> return self . post_outcome_request ( ) <EOL> def post_delete_result ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . operation = DELETE_REQUEST <EOL> return self . post_outcome_request ( ) <EOL> def post_read_result ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . operation = READ_REQUEST <EOL> return self . post_outcome_request ( ) <EOL> def is_replace_request ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . operation == REPLACE_REQUEST <EOL> def is_delete_request ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . operation == DELETE_REQUEST <EOL> def is_read_request ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . operation == READ_REQUEST <EOL> def was_outcome_post_successful ( self ) : <EOL> return self . outcome_response and self . outcome_response . is_success ( ) <EOL> def post_outcome_request ( self ) : <EOL> '''<STR_LIT>''' <EOL> if not self . has_required_attributes ( ) : <EOL> raise InvalidLTIConfigError ( <EOL> '<STR_LIT>' ) <EOL> consumer = oauth2 . Consumer ( key = self . consumer_key , <EOL> secret = self . consumer_secret ) <EOL> client = oauth2 . Client ( consumer ) <EOL> monkey_patch_headers = True <EOL> monkey_patch_function = None <EOL> if monkey_patch_headers : <EOL> import httplib2 <EOL> http = httplib2 . Http <EOL> normalize = http . _normalize_headers <EOL> def my_normalize ( self , headers ) : <EOL> print ( "<STR_LIT>" , headers ) <EOL> ret = normalize ( self , headers ) <EOL> if '<STR_LIT>' in ret : <EOL> ret [ '<STR_LIT>' ] = ret . pop ( '<STR_LIT>' ) <EOL> print ( "<STR_LIT>" , ret ) <EOL> return ret <EOL> http . _normalize_headers = my_normalize <EOL> monkey_patch_function = normalize <EOL> response , content = client . request ( <EOL> self . lis_outcome_service_url , <EOL> '<STR_LIT:POST>' , <EOL> body = self . generate_request_xml ( ) , <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT>' } ) <EOL> if monkey_patch_headers and monkey_patch_function : <EOL> import httplib2 <EOL> http = httplib2 . Http <EOL> http . _normalize_headers = monkey_patch_function <EOL> self . outcome_response = OutcomeResponse . from_post_response ( response , <EOL> content ) <EOL> return self . outcome_response <EOL> def process_xml ( self , xml ) : <EOL> '''<STR_LIT>''' <EOL> root = objectify . fromstring ( xml ) <EOL> self . message_identifier = str ( <EOL> root . imsx_POXHeader . imsx_POXRequestHeaderInfo . <EOL> imsx_messageIdentifier ) <EOL> try : <EOL> result = root . imsx_POXBody . replaceResultRequest <EOL> self . operation = REPLACE_REQUEST <EOL> self . lis_result_sourcedid = result . resultRecord . sourcedGUID . sourcedId <EOL> self . score = str ( result . resultRecord . result . <EOL> resultScore . textString ) <EOL> except : <EOL> pass <EOL> try : <EOL> result = root . imsx_POXBody . deleteResultRequest <EOL> self . operation = DELETE_REQUEST <EOL> self . lis_result_sourcedid = result . resultRecord . sourcedGUID . sourcedId <EOL> except : <EOL> pass <EOL> try : <EOL> result = root . imsx_POXBody . readResultRequest <EOL> self . operation = READ_REQUEST <EOL> self . lis_result_sourcedid = result . resultRecord . sourcedGUID . sourcedId <EOL> except : <EOL> pass <EOL> def has_required_attributes ( self ) : <EOL> return self . consumer_key is not None and self . consumer_secret is not None and self . lis_outcome_service_url is not None and self . lis_result_sourcedid is not None and self . operation is not None <EOL> def generate_request_xml ( self ) : <EOL> root = etree . Element ( <EOL> '<STR_LIT>' , <EOL> xmlns = '<STR_LIT>' ) <EOL> header = etree . SubElement ( root , '<STR_LIT>' ) <EOL> header_info = etree . SubElement ( header , '<STR_LIT>' ) <EOL> version = etree . SubElement ( header_info , '<STR_LIT>' ) <EOL> version . text = '<STR_LIT>' <EOL> message_identifier = etree . SubElement ( header_info , <EOL> '<STR_LIT>' ) <EOL> message_identifier . text = self . message_identifier <EOL> body = etree . SubElement ( root , '<STR_LIT>' ) <EOL> request = etree . SubElement ( body , '<STR_LIT>' % ( self . operation , <EOL> '<STR_LIT>' ) ) <EOL> record = etree . SubElement ( request , '<STR_LIT>' ) <EOL> guid = etree . SubElement ( record , '<STR_LIT>' ) <EOL> sourcedid = etree . SubElement ( guid , '<STR_LIT>' ) <EOL> sourcedid . text = self . lis_result_sourcedid <EOL> if self . score is not None : <EOL> result = etree . SubElement ( record , '<STR_LIT:result>' ) <EOL> result_score = etree . SubElement ( result , '<STR_LIT>' ) <EOL> language = etree . SubElement ( result_score , '<STR_LIT>' ) <EOL> language . text = '<STR_LIT>' <EOL> text_string = etree . SubElement ( result_score , '<STR_LIT>' ) <EOL> text_string . text = self . score . __str__ ( ) <EOL> if self . result_data : <EOL> resultData = etree . SubElement ( result , '<STR_LIT>' ) <EOL> if '<STR_LIT:text>' in self . result_data : <EOL> resultDataText = etree . SubElement ( resultData , '<STR_LIT:text>' ) <EOL> resultDataText . text = self . result_data [ '<STR_LIT:text>' ] <EOL> elif '<STR_LIT:url>' in self . result_data : <EOL> resultDataURL = etree . SubElement ( resultData , '<STR_LIT:url>' ) <EOL> resultDataURL . text = self . result_data [ '<STR_LIT:url>' ] <EOL> return etree . tostring ( root , xml_declaration = True , encoding = '<STR_LIT:utf-8>' ) </s>
