Exemplo n.º 1
0
    def setup_upload(self, dbkey=None):
        """Create an upload with associated inputs.

        :param integer dbkey: if set use the upload record with given db key.
        :returns: a :py:class:`geonode.mtapi.models.Upload` instance
        """
        if dbkey:
            [upload] = Upload.objects.filter(id=dbkey)
            return upload

        files = [
            ("gmpe_logic_tree.xml", "lt_gmpe"),
            ("small_exposure.xml", "exposure"),
            ("source_model1.xml", "source"),
            ("source_model2.xml", "source"),
            ("source_model_logic_tree.xml", "lt_source"),
            ("vulnerability.xml", "vulnerability")]
        upload = view_utils.prepare_upload()
        for file, type in files:
            path = os.path.join(upload.path, file)
            # This is equivalent to what the touch command does.
            open(path, "w+").close()
            input = Input(path=path, owner=upload.owner, input_type=type,
                          upload=upload)
            input.save()
        return upload
Exemplo n.º 2
0
def handle_uploaded_file(upload, uploaded_file):
    """Store a single uploaded file on disk and in the database.

    :param upload: the :py:class:`geonode.mtapi.models.Upload` instance
        associated with this upload.
    :param uploaded_file: an uploaded file from the POST request
    :returns: the resulting :py:class:`geonode.mtapi.models.Input` instance
    """
    size = 0
    chunk_counter = 0
    input_type = None
    path = "%s/%s" % (upload.path, uploaded_file.name)
    destination = open(path, "wb+")
    for chunk in uploaded_file.chunks():
        destination.write(chunk)
        size += len(chunk)
        chunk_counter += 1
        if chunk_counter == 1:
            input_type = detect_input_type(chunk)
    destination.close()
    source = Input(upload=upload, owner=upload.owner, size=size, path=path,
                   input_type=input_type)
    source.save()
    print(source)
    return source
Exemplo n.º 3
0
    def setup_upload(self, dbkey=None):
        """Create an upload with associated inputs.

        :param integer dbkey: if set use the upload record with given db key.
        :returns: a :py:class:`geonode.mtapi.models.Upload` instance
        """
        if dbkey:
            [upload] = Upload.objects.filter(id=dbkey)
            return upload

        files = [("gmpe_logic_tree.xml", "lt_gmpe"),
                 ("small_exposure.xml", "exposure"),
                 ("source_model1.xml", "source"),
                 ("source_model2.xml", "source"),
                 ("source_model_logic_tree.xml", "lt_source"),
                 ("vulnerability.xml", "vulnerability")]
        upload = view_utils.prepare_upload()
        for file, type in files:
            path = os.path.join(upload.path, file)
            # This is equivalent to what the touch command does.
            open(path, "w+").close()
            input = Input(path=path,
                          owner=upload.owner,
                          input_type=type,
                          upload=upload)
            input.save()
        return upload
Exemplo n.º 4
0
 def test_prepare_result_with_pending_upload(self):
     """
     The json for pending uploads contains no `files` array.
     """
     user = OqUser.objects.filter(user_name="openquake")[0]
     upload = Upload(owner=user, path="/a/1", status="pending", job_pid=0)
     Input(upload=upload,
           owner=upload.owner,
           size=11,
           path=upload.path + "/a",
           input_type="source")
     self.assertEqual("", views.prepare_upload_result(upload))