#NEWLINE# @ require_http_methods ( [ 'HEAD' , 'POST' ] ) #NEWLINE# @ csrf_exempt #NEWLINE# def form_upload( request , username ) : #NEWLINE# #INDENT# class DoXmlFormUpload ( ) : #NEWLINE# #INDENT# def __init__( self , xml_file , user ) : #NEWLINE# #INDENT# self . xml_file = xml_file #NEWLINE# self . user = user #NEWLINE# #UNINDENT# def publish( self ) : #NEWLINE# #INDENT# return publish_xml_form ( self . xml_file , self . user ) #NEWLINE# #UNINDENT# #UNINDENT# form_user = get_object_or_404 ( User , username__iexact = username ) #NEWLINE# ( profile , created ) = UserProfile . objects . get_or_create ( user = form_user ) #NEWLINE# authenticator = HttpDigestAuthenticator ( ) #NEWLINE# if ( not authenticator . authenticate ( request ) ) : #NEWLINE# #INDENT# return authenticator . build_challenge_response ( ) #NEWLINE# #UNINDENT# if ( form_user != request . user ) : #NEWLINE# #INDENT# return HttpResponseForbidden ( _ ( ( 'Not allowed to upload form[s] to %(user)s account.' % { #NEWLINE# #INDENT# 'user' : form_user , #NEWLINE# #UNINDENT# } ) ) ) #NEWLINE# #UNINDENT# if ( request . method == 'HEAD' ) : #NEWLINE# #INDENT# response = OpenRosaResponse ( status = 204 ) #NEWLINE# response [ 'Location' ] = request . build_absolute_uri ( ) . replace ( request . get_full_path ( ) , ( '/%s/formUpload' % form_user . username ) ) #NEWLINE# return response #NEWLINE# #UNINDENT# xform_def = request . FILES . get ( 'form_def_file' , None ) #NEWLINE# content = '' #NEWLINE# if isinstance ( xform_def , File ) : #NEWLINE# #INDENT# do_form_upload = DoXmlFormUpload ( xform_def , form_user ) #NEWLINE# dd = publish_form ( do_form_upload . publish ) #NEWLINE# status = 201 #NEWLINE# if isinstance ( dd , XForm ) : #NEWLINE# #INDENT# content = _ ( ( '%s successfully published.' % dd . id_string ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# content = dd [ 'text' ] #NEWLINE# if isinstance ( content , Exception ) : #NEWLINE# #INDENT# content = content . message #NEWLINE# status = 500 #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# status = 400 #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return OpenRosaResponse ( content , status = status )