<s> '''<STR_LIT>''' <EOL> import logging <EOL> import shutil <EOL> import urlparse <EOL> import json <EOL> from django import forms <EOL> from django . core . exceptions import MultipleObjectsReturned <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import ( HttpResponseRedirect , HttpResponse , <EOL> HttpResponseBadRequest ) <EOL> from django . shortcuts import render , render_to_response <EOL> from django . template import RequestContext <EOL> from django . views . generic import View <EOL> from rest_framework . views import APIView <EOL> from rest_framework . response import Response <EOL> from rest_framework import status <EOL> from django . http import Http404 <EOL> from chunked_upload . models import ChunkedUpload <EOL> from chunked_upload . views import ChunkedUploadView , ChunkedUploadCompleteView <EOL> from core . models import os , get_user_import_dir <EOL> from . single_file_column_parser import process_metadata_table <EOL> from . tasks import parse_isatab <EOL> from file_store . tasks import download_file , DownloadError <EOL> from file_store . models import get_temp_dir , generate_file_source_translator <EOL> from . models import AttributeOrder , Assay <EOL> from . serializers import AttributeOrderSerializer , AssaySerializer <EOL> from . utils import ( generate_solr_params , search_solr , format_solr_response , <EOL> get_owner_from_assay , update_attribute_order_ranks , <EOL> is_field_in_hidden_list , customize_attribute_response ) <EOL> logger = logging . getLogger ( __name__ ) <EOL> class DataSetImportView ( View ) : <EOL> """<STR_LIT>""" <EOL> template_name = "<STR_LIT>" <EOL> success_view_name = '<STR_LIT>' <EOL> isa_tab_cookie_name = '<STR_LIT>' <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> form = ImportISATabFileForm ( ) <EOL> context = RequestContext ( request , { '<STR_LIT>' : form } ) <EOL> response = render_to_response ( self . template_name , <EOL> context_instance = context ) <EOL> return response <EOL> class ImportISATabView ( View ) : <EOL> """<STR_LIT>""" <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> try : <EOL> isa_tab_url = request . POST [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> logger . error ( "<STR_LIT>" ) <EOL> return HttpResponseBadRequest ( "<STR_LIT>" ) <EOL> else : <EOL> response = HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> response . set_cookie ( '<STR_LIT>' , isa_tab_url ) <EOL> return response <EOL> class ImportISATabFileForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> isa_tab_file = forms . FileField ( label = '<STR_LIT>' , required = False ) <EOL> isa_tab_url = forms . URLField ( label = '<STR_LIT>' , required = False , <EOL> widget = forms . TextInput ( attrs = { '<STR_LIT:size>' : '<STR_LIT>' } ) ) <EOL> def clean ( self ) : <EOL> cleaned_data = super ( ImportISATabFileForm , self ) . clean ( ) <EOL> f = cleaned_data . get ( "<STR_LIT>" ) <EOL> url = cleaned_data . get ( "<STR_LIT>" ) <EOL> if f or url : <EOL> return cleaned_data <EOL> else : <EOL> raise forms . ValidationError ( <EOL> "<STR_LIT>" ) <EOL> class ProcessISATabView ( View ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> success_view_name = '<STR_LIT>' <EOL> isa_tab_cookie_name = '<STR_LIT>' <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> try : <EOL> url_from_cookie = request . COOKIES [ self . isa_tab_cookie_name ] <EOL> except KeyError : <EOL> logger . info ( "<STR_LIT>" ) <EOL> form = ImportISATabFileForm ( ) <EOL> context = RequestContext ( request , { '<STR_LIT>' : form } ) <EOL> return render_to_response ( self . template_name , <EOL> context_instance = context ) <EOL> form = ImportISATabFileForm ( { '<STR_LIT>' : url_from_cookie } ) <EOL> if form . is_valid ( ) : <EOL> url = form . cleaned_data [ '<STR_LIT>' ] <EOL> else : <EOL> context = RequestContext ( request , { '<STR_LIT>' : form } ) <EOL> response = render_to_response ( self . template_name , <EOL> context_instance = context ) <EOL> response . delete_cookie ( self . isa_tab_cookie_name ) <EOL> return response <EOL> u = urlparse . urlparse ( url ) <EOL> file_name = u . path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> temp_file_path = os . path . join ( get_temp_dir ( ) , file_name ) <EOL> try : <EOL> download_file ( url , temp_file_path ) <EOL> except DownloadError as e : <EOL> logger . error ( "<STR_LIT>" , e ) <EOL> error = "<STR_LIT>" . format ( url ) <EOL> context = RequestContext ( request , { '<STR_LIT>' : form , '<STR_LIT:error>' : error } ) <EOL> response = render_to_response ( self . template_name , <EOL> context_instance = context ) <EOL> response . delete_cookie ( self . isa_tab_cookie_name ) <EOL> return response <EOL> logger . debug ( "<STR_LIT>" , temp_file_path ) <EOL> dataset_uuid = parse_isatab . delay ( request . user . username , False , <EOL> temp_file_path ) . get ( ) [ <NUM_LIT:0> ] <EOL> os . unlink ( temp_file_path ) <EOL> if dataset_uuid : <EOL> response = HttpResponseRedirect ( <EOL> reverse ( self . success_view_name , args = ( dataset_uuid , ) ) ) <EOL> response . delete_cookie ( self . isa_tab_cookie_name ) <EOL> return response <EOL> else : <EOL> error = "<STR_LIT>" <EOL> context = RequestContext ( request , { '<STR_LIT>' : form , '<STR_LIT:error>' : error } ) <EOL> response = render_to_response ( self . template_name , <EOL> context_instance = context ) <EOL> response . delete_cookie ( self . isa_tab_cookie_name ) <EOL> return response <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> form = ImportISATabFileForm ( request . POST , request . FILES ) <EOL> if form . is_valid ( ) : <EOL> f = form . cleaned_data [ '<STR_LIT>' ] <EOL> url = form . cleaned_data [ '<STR_LIT>' ] <EOL> logger . debug ( "<STR_LIT>" , url ) <EOL> context = RequestContext ( request , { '<STR_LIT>' : form } ) <EOL> if url : <EOL> u = urlparse . urlparse ( url ) <EOL> file_name = u . path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> temp_file_path = os . path . join ( get_temp_dir ( ) , file_name ) <EOL> try : <EOL> download_file ( url , temp_file_path ) <EOL> except DownloadError as e : <EOL> logger . error ( "<STR_LIT>" , e ) <EOL> error = "<STR_LIT>" + url <EOL> context = RequestContext ( request , <EOL> { '<STR_LIT>' : form , '<STR_LIT:error>' : error } ) <EOL> return render_to_response ( self . template_name , <EOL> context_instance = context ) <EOL> else : <EOL> temp_file_path = os . path . join ( get_temp_dir ( ) , f . name ) <EOL> try : <EOL> handle_uploaded_file ( f , temp_file_path ) <EOL> except IOError as e : <EOL> error_msg = "<STR_LIT>" <EOL> error_msg += "<STR_LIT>" <EOL> logger . error ( error_msg , e . errno , e . filename , e . strerror ) <EOL> error = "<STR_LIT>" <EOL> context = RequestContext ( request , <EOL> { '<STR_LIT>' : form , '<STR_LIT:error>' : error } ) <EOL> return render_to_response ( self . template_name , <EOL> context_instance = context ) <EOL> logger . debug ( "<STR_LIT>" , temp_file_path ) <EOL> dataset_uuid = ( parse_isatab . delay ( <EOL> request . user . username , <EOL> False , <EOL> temp_file_path <EOL> ) . get ( ) ) [ <NUM_LIT:0> ] <EOL> os . unlink ( temp_file_path ) <EOL> if dataset_uuid : <EOL> return HttpResponseRedirect ( <EOL> reverse ( self . success_view_name , args = ( dataset_uuid , ) ) ) <EOL> else : <EOL> error = '<STR_LIT>' <EOL> context = RequestContext ( request , <EOL> { '<STR_LIT>' : form , '<STR_LIT:error>' : error } ) <EOL> return render_to_response ( self . template_name , <EOL> context_instance = context ) <EOL> else : <EOL> context = RequestContext ( request , { '<STR_LIT>' : form } ) <EOL> return render_to_response ( self . template_name , <EOL> context_instance = context ) <EOL> def handle_uploaded_file ( source_file , target_path ) : <EOL> """<STR_LIT>""" <EOL> with open ( target_path , '<STR_LIT>' ) as destination : <EOL> for chunk in source_file . chunks ( ) : <EOL> destination . write ( chunk ) <EOL> class ProcessMetadataTableView ( View ) : <EOL> """<STR_LIT>""" <EOL> template_name = '<STR_LIT>' <EOL> success_view_name = '<STR_LIT>' <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> return render ( request , self . template_name ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> try : <EOL> metadata_file = request . FILES [ '<STR_LIT:file>' ] <EOL> title = request . POST [ '<STR_LIT:title>' ] <EOL> data_file_column = request . POST [ '<STR_LIT>' ] <EOL> except ( KeyError , ValueError ) : <EOL> error = { '<STR_LIT>' : <EOL> '<STR_LIT>' } <EOL> return render ( request , self . template_name , error ) <EOL> source_column_index = request . POST . getlist ( '<STR_LIT>' ) <EOL> if not source_column_index : <EOL> error = { '<STR_LIT>' : <EOL> '<STR_LIT>' } <EOL> return render ( request , self . template_name , error ) <EOL> source_column_index = [ column . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> for column in source_column_index ] <EOL> try : <EOL> dataset_uuid = process_metadata_table ( <EOL> username = request . user . username , title = title , <EOL> metadata_file = metadata_file , <EOL> source_columns = source_column_index , <EOL> data_file_column = data_file_column , <EOL> auxiliary_file_column = request . POST . get ( '<STR_LIT>' , <EOL> None ) , <EOL> base_path = request . POST . get ( '<STR_LIT>' , "<STR_LIT>" ) , <EOL> data_file_permanent = request . POST . get ( '<STR_LIT>' , <EOL> False ) , <EOL> species_column = request . POST . get ( '<STR_LIT>' , None ) , <EOL> genome_build_column = request . POST . get ( '<STR_LIT>' , <EOL> None ) , <EOL> annotation_column = request . POST . get ( '<STR_LIT>' , None ) , <EOL> slug = request . POST . get ( '<STR_LIT>' , None ) , <EOL> is_public = request . POST . get ( '<STR_LIT>' , False ) ) <EOL> except ValueError as exc : <EOL> error = { '<STR_LIT>' : exc } <EOL> return render ( request , self . template_name , error ) <EOL> return HttpResponseRedirect ( <EOL> reverse ( self . success_view_name , args = ( dataset_uuid , ) ) ) <EOL> class CheckDataFilesView ( View ) : <EOL> """<STR_LIT>""" <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> if not request . is_ajax ( ) or not request . body : <EOL> return HttpResponseBadRequest ( ) <EOL> file_data = json . loads ( request . body ) <EOL> try : <EOL> base_path = file_data [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> base_path = "<STR_LIT>" <EOL> bad_file_list = [ ] <EOL> translate_file_source = generate_file_source_translator ( <EOL> username = request . user . username , base_path = base_path ) <EOL> try : <EOL> for file_path in file_data [ "<STR_LIT:list>" ] : <EOL> if not isinstance ( file_path , str ) : <EOL> bad_file_list . append ( file_path ) <EOL> else : <EOL> file_path = translate_file_source ( file_path ) <EOL> if not os . path . exists ( file_path ) : <EOL> bad_file_list . append ( file_path ) <EOL> logger . debug ( "<STR_LIT>" , file_path ) <EOL> except KeyError : <EOL> return HttpResponseBadRequest ( ) <EOL> return HttpResponse ( "<STR_LIT>" + json . dumps ( bad_file_list ) , <EOL> content_type = "<STR_LIT:application/json>" ) <EOL> class ChunkedFileUploadView ( ChunkedUploadView ) : <EOL> model = ChunkedUpload <EOL> field_name = "<STR_LIT>" <EOL> class ChunkedFileUploadCompleteView ( ChunkedUploadCompleteView ) : <EOL> model = ChunkedUpload <EOL> def on_completion ( self , uploaded_file , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> upload_id = request . POST [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> logger . error ( "<STR_LIT>" ) <EOL> return <EOL> try : <EOL> chunked_upload = ChunkedUpload . objects . get ( upload_id = upload_id ) <EOL> except ( ChunkedUpload . DoesNotExist , MultipleObjectsReturned ) as exc : <EOL> logger . error ( <EOL> "<STR_LIT>" , <EOL> upload_id , exc ) <EOL> return <EOL> user_import_dir = get_user_import_dir ( request . user ) <EOL> if not os . path . exists ( user_import_dir ) : <EOL> try : <EOL> os . mkdir ( user_import_dir ) <EOL> except OSError as exc : <EOL> logger . error ( "<STR_LIT>" , <EOL> user_import_dir , exc ) <EOL> else : <EOL> logger . info ( "<STR_LIT>" , <EOL> user_import_dir ) <EOL> dst = os . path . join ( user_import_dir , chunked_upload . filename ) <EOL> try : <EOL> shutil . move ( chunked_upload . file . path , dst ) <EOL> except ( shutil . Error , IOError ) as exc : <EOL> logger . error ( <EOL> "<STR_LIT>" , <EOL> exc ) <EOL> def get_response_data ( self , chunked_upload , request ) : <EOL> message = "<STR_LIT>" . format ( <EOL> chunked_upload . filename ) <EOL> return { "<STR_LIT:message>" : message } <EOL> class Assays ( APIView ) : <EOL> """<STR_LIT>""" <EOL> def get_object ( self , uuid ) : <EOL> try : <EOL> return Assay . objects . get ( uuid = uuid ) <EOL> except Assay . DoesNotExist : <EOL> raise Http404 <EOL> def get ( self , request , uuid , format = None ) : <EOL> assay = self . get_object ( uuid ) <EOL> serializer = AssaySerializer ( assay ) <EOL> return Response ( serializer . data ) <EOL> class AssaysFiles ( APIView ) : <EOL> """<STR_LIT>""" <EOL> def get ( self , request , uuid , format = None ) : <EOL> params = request . query_params <EOL> solr_params = generate_solr_params ( params , uuid ) <EOL> solr_response = search_solr ( solr_params , '<STR_LIT>' ) <EOL> solr_response_json = format_solr_response ( solr_response ) <EOL> return Response ( solr_response_json ) <EOL> class AssaysAttributes ( APIView ) : <EOL> """<STR_LIT>""" <EOL> def get_objects ( self , uuid ) : <EOL> attributes = AttributeOrder . objects . filter ( assay__uuid = uuid ) <EOL> if len ( attributes ) : <EOL> return attributes <EOL> else : <EOL> raise Http404 <EOL> def get ( self , request , uuid , format = None ) : <EOL> attribute_order = self . get_objects ( uuid ) <EOL> serializer = AttributeOrderSerializer ( attribute_order , many = True ) <EOL> owner = get_owner_from_assay ( uuid ) <EOL> request_user = request . user <EOL> if owner == request_user : <EOL> attributes = serializer . data <EOL> else : <EOL> attributes = [ ] <EOL> for attribute in serializer . data : <EOL> if attribute . get ( '<STR_LIT>' ) : <EOL> attributes . append ( attribute ) <EOL> for ind in range ( len ( attributes ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if is_field_in_hidden_list ( attributes [ ind ] . get ( '<STR_LIT>' ) ) : <EOL> del attributes [ ind ] <EOL> else : <EOL> attributes [ ind ] [ '<STR_LIT>' ] = customize_attribute_response ( <EOL> [ attributes [ ind ] . get ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] . get ( <EOL> '<STR_LIT>' ) <EOL> if owner != request_user : <EOL> for ind in range ( <NUM_LIT:0> , len ( attributes ) ) : <EOL> attributes [ ind ] [ '<STR_LIT>' ] = ind + <NUM_LIT:1> <EOL> return Response ( attributes ) <EOL> def put ( self , request , uuid , format = None ) : <EOL> owner = get_owner_from_assay ( uuid ) <EOL> request_user = request . user <EOL> if owner == request_user : <EOL> solr_field = request . data . get ( '<STR_LIT>' , None ) <EOL> id = request . data . get ( '<STR_LIT:id>' , None ) <EOL> new_rank = request . data . get ( '<STR_LIT>' , None ) <EOL> if id : <EOL> attribute_order = AttributeOrder . objects . get ( <EOL> assay__uuid = uuid , id = id ) <EOL> elif solr_field : <EOL> attribute_order = AttributeOrder . objects . get ( <EOL> assay__uuid = uuid , solr_field = solr_field ) <EOL> else : <EOL> return Response ( <EOL> '<STR_LIT>' , <EOL> status = status . HTTP_400_BAD_REQUEST ) <EOL> if new_rank and new_rank != attribute_order . rank : <EOL> try : <EOL> update_attribute_order_ranks ( attribute_order , new_rank ) <EOL> except Exception as e : <EOL> return e <EOL> serializer = AttributeOrderSerializer ( attribute_order , <EOL> data = request . data , <EOL> partial = True ) <EOL> if serializer . is_valid ( ) : <EOL> serializer . save ( ) <EOL> attributes = serializer . data <EOL> attributes [ '<STR_LIT>' ] = customize_attribute_response ( <EOL> [ attributes . get ( '<STR_LIT>' ) ] ) [ <NUM_LIT:0> ] . get ( <EOL> '<STR_LIT>' ) <EOL> return Response ( <EOL> attributes , <EOL> status = status . HTTP_202_ACCEPTED <EOL> ) <EOL> return Response ( <EOL> serializer . errors , <EOL> status = status . HTTP_400_BAD_REQUEST <EOL> ) <EOL> else : <EOL> message = '<STR_LIT>' <EOL> return Response ( message , status = status . HTTP_401_UNAUTHORIZED ) </s>
