From 0ab5f8d538cb13bbea328891f6ed76a09c5de07a Mon Sep 17 00:00:00 2001 From: vermeul <swen@ethz.ch> Date: Tue, 19 Dec 2017 17:00:15 +0100 Subject: [PATCH] fixed project attachments, added tests --- src/python/PyBis/pybis/pybis.py | 19 ++++++++++++++----- src/python/PyBis/tests/test_project.py | 21 +++++++++++++++++++++ src/python/PyBis/tests/testfile | 1 + 3 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 src/python/PyBis/tests/testfile diff --git a/src/python/PyBis/pybis/pybis.py b/src/python/PyBis/pybis/pybis.py index 75a670f1b2c..a14dc1bba33 100644 --- a/src/python/PyBis/pybis/pybis.py +++ b/src/python/PyBis/pybis/pybis.py @@ -641,7 +641,7 @@ class Openbis: "get_tags()", "get_terms()", 'new_space(name, description)', - 'new_project(space, code, description)', + 'new_project(space, code, description, attachments)', 'new_experiment(type, code, project, props={})', 'new_sample(type, space, project, experiment, parents)', 'new_object(type, space, project, experiment, parents)', # 'new_sample(type, space, project, experiment)' alias @@ -1357,13 +1357,16 @@ class Openbis: fo[option] = fetch_option[option] return fo - def get_project(self, projectId): + def get_project(self, projectId, only_data=False): options = ['space', 'registrator', 'modifier', 'attachments'] - if is_identifier(projectId): + if is_identifier(projectId) or is_permid(projectId): request = self._create_get_request( 'getProjects', 'project', projectId, options ) resp = self._post_request(self.as_v3, request) + if only_data: + return resp[projectId] + return Project(self, resp[projectId]) else: @@ -1380,6 +1383,9 @@ class Openbis: resp = self._post_request(self.as_v3, request) if len(resp['objects']) == 0: raise ValueError("No such project: %s" % projectId) + if only_data: + return resp['objects'][0] + return Project(self, resp['objects'][0]) def get_projects(self, space=None, code=None): @@ -3410,7 +3416,7 @@ class Space(OpenBisObject): return [ 'code', 'permId', 'description', 'registrator', 'registrationDate', 'modificationDate', 'get_projects()', - "new_project(code='', description='')", + "new_project(code='', description='', attachments)", 'get_samples()', 'delete()' ] @@ -3866,9 +3872,12 @@ class Project(OpenBisObject): def save(self): if self.is_new: request = self._new_attrs() - self.openbis._post_request(self.openbis.as_v3, request) + resp = self.openbis._post_request(self.openbis.as_v3, request) self.a.__dict__['_is_new'] = False print("Project successfully created.") + new_project_data = self.openbis.get_project(resp[0]['permId'], only_data=True) + self._set_data(new_project_data) + return self else: request = self._up_attrs() self.openbis._post_request(self.openbis.as_v3, request) diff --git a/src/python/PyBis/tests/test_project.py b/src/python/PyBis/tests/test_project.py index 2b7d4f4429a..033360fc292 100644 --- a/src/python/PyBis/tests/test_project.py +++ b/src/python/PyBis/tests/test_project.py @@ -4,6 +4,7 @@ import re import pytest import time +import os from pybis import DataSet from pybis import Openbis @@ -28,3 +29,23 @@ def test_create_delete_project(space): with pytest.raises(ValueError): project_no_longer_exists=o.get_project(project_name) assert "project {} should have been deleted".format(project_name) is None + + +def test_create_project_with_attachment(space): + o=space.openbis + + timestamp = time.strftime('%a_%y%m%d_%H%M%S').upper() + project_name = 'project_'+timestamp + filename = os.path.join(os.path.dirname(__file__), 'testfile') + + if not os.path.exists(filename): + raise ValueError("File not found: {}".format(filename)) + + project=o.new_project(space=space, code=project_name, attachments=filename) + assert project.attachments is not None + project.save() + + project_exists=o.get_project(project_name) + assert project_exists is not None + assert project_exists.attachments is not None + diff --git a/src/python/PyBis/tests/testfile b/src/python/PyBis/tests/testfile new file mode 100644 index 00000000000..778168770e0 --- /dev/null +++ b/src/python/PyBis/tests/testfile @@ -0,0 +1 @@ +Hello, Test! -- GitLab