#NEWLINE# @ login_required #NEWLINE# def upload_document( request ) : #NEWLINE# #INDENT# def parse_meeting_date( d ) : #NEWLINE# #INDENT# return datetime . datetime . strptime ( d , '%Y-%m-%dT%H:%M:%S' ) #NEWLINE# #UNINDENT# if ( request . method == 'POST' ) : #NEWLINE# #INDENT# form = UploadDocumentForm ( request . POST , request . FILES ) #NEWLINE# if ( form . is_valid ( ) and ( not ( ( form . cleaned_data [ 'upload_type' ] == 'file' ) and ( 'docfile' not in request . FILES ) ) ) and ( not ( ( form . cleaned_data [ 'upload_type' ] == 'paste' ) and ( not form . cleaned_data [ 'content' ] ) ) ) and ( not ( ( form . cleaned_data [ 'upload_type' ] == 'url' ) and ( not form . cleaned_data [ 'url' ] ) ) ) ) : #NEWLINE# #INDENT# newdoc = Document ( ) #NEWLINE# newdoc . owner = request . user #NEWLINE# newdoc . anc = form . cleaned_data [ 'anc' ] #NEWLINE# newdoc . doc_type = form . cleaned_data [ 'doc_type' ] #NEWLINE# if ( form . cleaned_data [ 'meeting_date_hidden' ] . strip ( ) != '' ) : #NEWLINE# #INDENT# newdoc . meeting_date = parse_meeting_date ( form . cleaned_data [ 'meeting_date_hidden' ] ) #NEWLINE# #UNINDENT# if ( form . cleaned_data [ 'upload_type' ] == 'file' ) : #NEWLINE# #INDENT# newdoc . set_document_content ( request . FILES [ 'docfile' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( form . cleaned_data [ 'upload_type' ] == 'paste' ) : #NEWLINE# #INDENT# newdoc . set_document_content ( form . cleaned_data [ 'content' ] ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( form . cleaned_data [ 'upload_type' ] == 'url' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# import urllib2 #NEWLINE# req = urllib2 . Request ( form . cleaned_data [ 'url' ] ) #NEWLINE# req . add_unredirected_header ( 'User-Agent' , 'ancfinder.com' ) #NEWLINE# req . add_unredirected_header ( 'Accept' , '*/*' ) #NEWLINE# resp = urllib2 . urlopen ( req ) #NEWLINE# if ( resp . code != 200 ) : #NEWLINE# #INDENT# raise ValueError ( 'URL returned an error.' ) #NEWLINE# #UNINDENT# mime_type = resp . info ( ) [ 'content-type' ] . split ( ';' ) [ 0 ] . strip ( ) #NEWLINE# if ( mime_type != 'application/pdf' ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Not a PDF: ' + mime_type ) ) #NEWLINE# #UNINDENT# content = resp . read ( ) #NEWLINE# newdoc . set_document_content ( content , mime_type = mime_type ) #NEWLINE# newdoc . source_url = form . cleaned_data [ 'url' ] #NEWLINE# #UNINDENT# except Exception as e : #NEWLINE# #INDENT# return render_to_response ( 'ancfindersite/upload_document.html' , { #NEWLINE# #INDENT# 'form' : form , #NEWLINE# 'url_error' : str ( e ) , #NEWLINE# #UNINDENT# } , context_instance = RequestContext ( request ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# newdoc . save ( ) #NEWLINE# return HttpResponseRedirect ( reverse ( 'ancfindersite.backend_views.edit_document' , args = [ newdoc . id ] ) ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# form = UploadDocumentForm ( initial = { #NEWLINE# #INDENT# 'anc' : request . GET . get ( 'anc' ) , #NEWLINE# 'doc_type' : request . GET . get ( 'doc_type' ) , #NEWLINE# 'meeting_date_hidden' : request . GET . get ( 'meeting_date' ) , #NEWLINE# 'meeting_date_display' : ( parse_meeting_date ( request . GET . get ( 'meeting_date' ) ) . strftime ( '%x' ) if request . GET . get ( 'meeting_date' ) else None ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# if ( request . GET . get ( 'meeting_date' ) is None ) : #NEWLINE# #INDENT# del form . fields [ 'meeting_date_display' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# form . fields [ 'meeting_date_display' ] . widget . attrs [ 'disabled' ] = 'disabled' #NEWLINE# #UNINDENT# #UNINDENT# return render_to_response ( 'ancfindersite/upload_document.html' , { #NEWLINE# #INDENT# 'form' : form , #NEWLINE# #UNINDENT# } , context_instance = RequestContext ( request ) )