Example #1
0
    def get(self, request, pk):
        """Download the JSON Schema file from a Template

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: JSON Schema file
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            template_object = self.get_object(pk, request=request)

            return get_file_http_response(
                template_object.content,
                template_object.filename,
                "application/schema+json",
                ".schema.json",
            )
        except Http404:
            content = {"message": "Template not found."}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #2
0
    def get(self, request, pk):
        """ Download the XSD file from a Template

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: XSD file
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            template_object = self.get_object(pk)

            return get_file_http_response(template_object.content,
                                          template_object.filename, 'text/xsd',
                                          'xsd')
        except Http404:
            content = {'message': 'Template not found.'}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {'message': str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #3
0
def download_current_xml(request, curate_data_structure_id):
    """Make the current XML document available for download.

    Args:
        request:
        curate_data_structure_id:

    Returns:

    """
    # get curate data structure
    curate_data_structure = _get_curate_data_structure_by_id(
        curate_data_structure_id, request)

    # generate xml string
    xml_data = render_xml(request,
                          curate_data_structure.data_structure_element_root)

    # build response with file
    return get_file_http_response(
        file_content=xml_data,
        file_name=curate_data_structure.name,
        content_type="application/xml",
        extension="xml",
    )
Example #4
0
    def get(self, request, pk):
        """ Download the XML file from a data

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: XML file
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            data_object = self.get_object(request, pk)

            return get_file_http_response(data_object.xml_content, data_object.title, 'text/xml', 'xml')
        except Http404:
            content = {'message': 'Data not found.'}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #5
0
    def get(self, request, pk):
        """ Download the Blob file

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: Blob file
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            blob_object = self.get_object(pk)

            return get_file_http_response(blob_object.blob,
                                          blob_object.filename)
        except Http404:
            content = {'message': 'Blob not found.'}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #6
0
    def get(self, request, pk):
        """Download Query Ontology

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: OWL file
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            query_ontology_object = self.get_object(request, pk)
            return get_file_http_response(
                query_ontology_object.content,
                query_ontology_object.title,
                "text/xml",
                "owl",
            )
        except Http404:
            content = {"message": "Query Ontology not found."}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #7
0
def download_displayed_data(request):
    """

    Args:
        request:

    Returns:

    """
    # retrieve all the parameters from the request
    node_id = request.GET.get('current_node', None)
    doc_id = request.GET.get('doc_id', None)
    file_name = request.GET.get('file_name', None)
    load_doc = {}
    if node_id is not None and doc_id is not None:
        node_name = get_node_name(node_id)
        c_id_leaf = str(node_name) + '_' + str(doc_id)
        c_id_link = node_id + '_' + doc_id

        # Get the document from the cache
        # The doc had been reached initially from the tree
        if c_id_leaf in leaf_cache:
            load_doc = leaf_cache.get(c_id_leaf)
        # The doc had been reached initially from a link
        elif c_id_link in link_cache:
            load_doc = link_cache.get(c_id_link)
    return get_file_http_response(file_content=load_doc["download"],
                                  file_name=file_name,
                                  content_type="text/xml",
                                  extension="xml")
Example #8
0
    def get(self, request, pk):
        """Download the XSD file from a Template

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: XSD file
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            template_object = self.get_object(pk, request=request)

            return get_file_http_response(template_object.content,
                                          template_object.filename, "text/xsd",
                                          "xsd")
        except Http404:
            content = {"message": "Template not found."}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except AccessControlError:
            content = {"message": "Access Forbidden."}
            return Response(content, status=status.HTTP_403_FORBIDDEN)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #9
0
    def get(self, request, pk):
        """Download the JSON file from a data

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: JSON file
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            data_object = self.get_object(request, pk)

            return get_file_http_response(
                json.dumps(data_object.dict_content),
                data_object.title,
                "application/json",
                "json",
            )
        except Http404:
            content = {"message": "Data not found."}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #10
0
    def get(self, request, pk):
        """Download the file

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: ZipFile
            - code: 204
              content: The zip file is not yet ready
            - code: 203
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            compressed_file_object = self.get_object(pk, request.user)
            if compressed_file_object.is_ready:
                return get_file_http_response(
                    compressed_file_object.file.read(), compressed_file_object.file_name
                )
            else:
                content = {"message": "The zip file is not yet ready."}
                return Response(content, status=status.HTTP_204_NO_CONTENT)
        except Http404:
            content = {"message": "Compressed file not found."}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #11
0
    def get(self, request, pk):
        """Download the Blob file

        Args:

            request: HTTP request
            pk: ObjectId

        Returns:

            - code: 200
              content: Blob file
            - code: 403
              content: Authentication error
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            blob_object = self.get_object(request, pk)

            return get_file_http_response(blob_object.blob, blob_object.filename)
        except AccessControlError as e:
            content = {"message": str(e)}
            return Response(content, status=status.HTTP_403_FORBIDDEN)
        except Http404:
            content = {"message": "Blob not found."}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #12
0
def export_data(transform_result_list, user, title):
    """Get the transformed Data file

    Args:
        transform_result_list:
        user:
        title:

    Returns:
        HttpResponse:
    """
    # get the list of the transformed content(first element since we have only one data)
    transform_result_content_list = transform_result_list[
        0].transform_result_content

    file_content = ""
    file_name = title
    extension = ""
    content_type = ""
    if len(transform_result_content_list) > 1:
        # export as a zip file (more than file)
        exported_file = ExportedCompressedFile(
            file_name="Query_Results.zip",
            is_ready=False,
            mime_type="application/zip",
            user_id=str(user.id),
        )

        # Save in database to generate an Id and be accessible via url
        exported_file = exported_compressed_file_api.upsert(exported_file)

        # Export in Zip
        AbstractExporter.export(exported_file.id, transform_result_list, user)

        # Serialize object
        return_value = ExporterExportedCompressedFileSerializer(exported_file)
        compressed_file_object = exported_compressed_file_api.get_by_id(
            return_value.data["id"], user)

        file_content = compressed_file_object.file.read()
        file_name = compressed_file_object.file_name

    elif len(transform_result_content_list) == 1:
        # export as a ordinary file (first element since we have only one transformed content)
        file_content = transform_result_content_list[0].content_converted

        # get the extension
        extension = transform_result_content_list[0].content_extension

        # get the type content by removing '.' from the extension
        if extension:
            content_type = "text/" + extension.split(".")[1]

    return get_file_http_response(
        file_content,
        file_name,
        content_type,
        extension,
    )
Example #13
0
    def get(self, request, pk):
        """Download the XSD file from a Template

        Args:

            request: HTTP request
            pk: ObjectId

        Examples:

            ../template/[template_id]/download
            ../template/[template_id]/download?pretty_print=false

        Returns:

            - code: 200
              content: XSD file
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            template_object = self.get_object(pk, request=request)

            # get xml content
            content = template_object.content

            # get format bool
            format = request.query_params.get("pretty_print", False)

            # format content
            if to_bool(format):
                content = format_content_xml(content)

            return get_file_http_response(content, template_object.filename,
                                          "text/xsd", "xsd")
        except Http404:
            content = {"message": "Template not found."}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except XMLError:
            content = {"message": "Content is not well formatted XML."}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)
        except AccessControlError:
            content = {"message": "Access Forbidden."}
            return Response(content, status=status.HTTP_403_FORBIDDEN)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #14
0
def download_source_file(request):
    """

    Args:
        request:

    Returns:

    """
    doc_id = request.GET.get('doc_id', None)
    data = Data.get_by_id(doc_id)
    return get_file_http_response(file_content=data.xml_content,
                                  file_name=data.title,
                                  content_type="text/xml",
                                  extension="xml")
Example #15
0
def download(request):
    """ Generate the output files to download it

    Args:
        request:

    Returns:

    """
    id_file_csv = request.POST.get('hidden_id_file_csv', None)
    id_file_json = request.POST.get('hidden_id_file_json', None)
    id_file_xml = request.POST.get('hidden_id_file_xml', None)
    name = request.POST.get('hidden_query_name', None)
    formats = request.POST.get('formats', None)

    try:
        if formats == 'csv':
            output_file = temp_output_file_api.load_file(id_file_csv)
            extension = ".csv"
            content_type = "application/CSV"
        elif formats == 'json':
            output_file = temp_output_file_api.load_file(id_file_json)
            extension = ".json"
            content_type = "application/json"
        else:
            output_file = temp_output_file_api.load_file(id_file_xml)
            extension = ".xml"
            content_type = "application/xml"
    except:
        messages.add_message(
            request, messages.ERROR,
            "An internal problem occurred, the administrator has been notified."
        )
        return redirect(reverse("custom_queries_queryChoose"))

    today = datetime.now()
    name += "_" + str(today.month) + \
            "_" + str(today.day) + \
            "_" + str(today.year) + \
            "_" + str(today.hour) + \
            "_" + str(today.minute) + \
            "_" + str(today.second)

    # FIXME: test with large files
    response = get_file_http_response(output_file, name, content_type,
                                      extension)

    return response
Example #16
0
def download_displayed_data(request):
    """

    Args:
        request:

    Returns:

    """
    # retrieve all the parameters from the request
    node_id = request.GET.get("current_node", None)
    doc_id = request.GET.get("doc_id", None)
    file_name = request.GET.get("file_name", None)
    load_doc = {}
    if node_id is not None and doc_id is not None:
        node_name = get_node_name(node_id)
        id_leafdoc = str(node_name) + "_" + str(doc_id)
        id_linkdoc = node_id + "_" + doc_id
        # Get the document from the cache
        # The doc had been reached initially from a link
        if id_linkdoc in link_cache:
            load_doc = link_cache.get(id_linkdoc)
        # The doc had been reached initially from the tree
        elif id_leafdoc in leaf_cache:
            load_doc = leaf_cache.get(id_leafdoc)
        # The doc had been cached by the admin
        else:
            navigation_node = navigation_operations.get_navigation_node_for_document(
                node_id, doc_id)
            nodename = navigation_node.name
            nodename_index = nodename.find("#")
            node_name = nodename[nodename_index + 1:]
            id_doc_cached = node_name + "_" + str(doc_id)
            listof = {}
            alldatacached = DataCached.get_all()
            for datacached in alldatacached:
                dict_keys_docids = datacached.cached_documents_dict
                for dict_key_docid in dict_keys_docids:
                    listof.update(dict_key_docid)
            if id_doc_cached in listof.keys():
                load_doc = leaf_cache.get(str(listof[id_doc_cached]))

    return get_file_http_response(
        file_content=load_doc["download"],
        file_name=file_name,
        content_type="text/xml",
        extension="xml",
    )
Example #17
0
    def get(self, request, pk):
        """Download the XML file from a data

        Args:

            request: HTTP request
            pk: ObjectId

        Examples:

            ../data/download/[data_id]
            ../data/download/[data_id]?pretty_print=true

        Returns:

            - code: 200
              content: XML file
            - code: 404
              content: Object was not found
            - code: 500
              content: Internal server error
        """
        try:
            # Get object
            data_object = self.get_object(request, pk)

            # get xml content
            xml_content = data_object.xml_content

            # get format bool
            format = request.query_params.get("pretty_print", False)

            # format content
            if to_bool(format):
                xml_content = format_content_xml(xml_content)

            return get_file_http_response(xml_content, data_object.title,
                                          "text/xml", "xml")
        except Http404:
            content = {"message": "Data not found."}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except XMLError:
            content = {"message": "Content is not well formatted XML."}
            return Response(content, status=status.HTTP_400_BAD_REQUEST)
        except Exception as api_exception:
            content = {"message": str(api_exception)}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #18
0
def download_xsd(request):
    """Make the current XSD available for download.

    Args:
        request:

    Returns:

    """
    xsd_string = request.session['newXmlTemplateCompose']

    # return the file
    return get_file_http_response(file_content=xsd_string,
                                  file_name="schema.xsd",
                                  content_type="application/xsd",
                                  extension=".xsd")
Example #19
0
def download_query_ontology(request, pk=None):
    """ Download ontology.

    Args:
        request:
        pk:

    Returns:

    """
    # get the ontology
    ontology = query_ontology_api.get_by_id(pk)
    # return the file
    return get_file_http_response(file_content=ontology.content,
                                  file_name=ontology.title,
                                  content_type="application/xml",
                                  extension=".owl")
    def get(self, request, provider, record):
        """Retrieve the local data of a given handle record

        Args:
            request:
            provider:
            record:

        Returns:
        """
        id_provider = self.provider_manager.get(provider)
        provider_response = id_provider.get(record)

        try:
            query_result = get_data_by_pid(
                json.loads(provider_response.content)["url"], request.user
            )
            return Response(
                DataSerializer(query_result).data, status=status.HTTP_200_OK
            )
        except DoesNotExist:
            # Try to find PID in blobs
            try:
                query_result = get_blob_by_pid(
                    json.loads(provider_response.content)["url"], request.user
                )

                return get_file_http_response(query_result.blob, query_result.filename)
            except AccessControlError as e:
                content = {"message": str(e)}
                return Response(content, status=status.HTTP_403_FORBIDDEN)
            except DoesNotExist:
                content = {
                    "status": "error",
                    "code": status.HTTP_404_NOT_FOUND,
                    "message": "No document with specified handle found",
                }
                return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as ex:
            content = {
                "status": "error",
                "code": status.HTTP_500_INTERNAL_SERVER_ERROR,
                "message": str(ex),
            }
            return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #21
0
def download_blank_query_ontology(request):
    """ Download ontology.

    Args:
        request:
        pk:

    Returns:

    """
    # open the blank owl file
    owl_file = open(finders.find('core_explore_tree_app/common/owl/blank.owl'))
    # retrieve the content of it
    content = owl_file.read()
    # return the file
    return get_file_http_response(file_content=content,
                                  file_name='blank',
                                  content_type="application/xml",
                                  extension=".owl")
Example #22
0
def download_template(request):
    """ Download template.

    Args:
        request:

    Returns:

    """
    template_id = request.GET.get('template_id', None)
    if template_id:
        template = template_api.get(template_id)
        # return the file
        return get_file_http_response(file_content=template.content,
                                      file_name=template.display_name,
                                      content_type='application/xsd',
                                      extension=".xsd")
    else:
        return redirect(reverse("core_schema_viewer_index"))
Example #23
0
    def get(self, request, pk):
        """ Retrieve blob

        Args:
            pk:

        Returns:

        """
        try:
            # Get object
            blob_object = self.get_object(pk)

            return get_file_http_response(blob_object.blob, blob_object.filename)
        except Http404:
            content = {'message': 'Blob not found.'}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #24
0
def download_xsd(request, curate_data_structure_id):
    """Make the current XSD available for download.

    Args:
        request:
        curate_data_structure_id:

    Returns:

    """
    # get curate data structure
    curate_data_structure = _get_curate_data_structure_by_id(
        curate_data_structure_id, request)

    # get the template
    template = curate_data_structure.template
    # return the file
    return get_file_http_response(file_content=template.content,
                                  file_name=template.filename,
                                  content_type='application/xsd',
                                  extension='.xsd')
Example #25
0
def download_latest(request):
    """Download the map file.

    /rest/tsne/map/download/latest

    Args:
        request:

    Returns:

    """
    try:
        # get T-SNE map
        tsne_map = tsne_map_api.get_last(request.user)
        # return csv file
        return get_file_http_response(tsne_map, 'tsne_app', extension='csv')
    except exceptions.DoesNotExist as e:
        content = {'message': 'No data found with the given id.'}
        return Response(content, status=status.HTTP_404_NOT_FOUND)
    except Exception as api_exception:
        content = {'message': api_exception.message}
        return Response(content, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #26
0
    def get(self, request, pk):
        """ Download Query Ontology

        Args:
            request:
            pk:

        Returns:

        """
        try:
            # Get object
            query_ontology_object = self.get_object(request, pk)
            return get_file_http_response(query_ontology_object.content,
                                          query_ontology_object.title,
                                          'text/xml', 'owl')
        except Http404:
            content = {'message': 'Query Ontology not found.'}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Example #27
0
def download_xml(request, sandbox_data_structure_id):
    """ Download XML file

    Args:
        request:
        sandbox_data_structure_id:

    Returns:

    """
    # get sandbox data structure
    sandbox_data_structure = sandbox_data_structure_api.get_by_id(
        sandbox_data_structure_id)

    # build XML renderer
    xml_data = XmlRenderer(
        sandbox_data_structure.data_structure_element_root).render()

    # build response with file
    return get_file_http_response(file_content=xml_data,
                                  file_name=sandbox_data_structure.name,
                                  content_type='application/xml',
                                  extension='xml')
Example #28
0
    def get(self, request, pk):
        """ Retrieve template

        Args:
            request:
            pk:

        Returns:

        """
        try:
            # Get object
            template_object = self.get_object(pk)

            return get_file_http_response(template_object.content,
                                          template_object.filename, 'text/xsd',
                                          'xsd')
        except Http404:
            content = {'message': 'Template not found.'}
            return Response(content, status=status.HTTP_404_NOT_FOUND)
        except Exception as api_exception:
            content = {'message': api_exception.message}
            return Response(content,
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)