def get_user_details(self, response): authenticated_panoptes = Panoptes( endpoint=PanoptesUtils.base_url(), client_id=PanoptesUtils.client_id(), client_secret=PanoptesUtils.client_secret()) authenticated_panoptes.bearer_token = response['access_token'] authenticated_panoptes.logged_in = True authenticated_panoptes.refresh_token = response['refresh_token'] bearer_expiry = datetime.now() + timedelta( seconds=response['expires_in']) authenticated_panoptes.bearer_expires = (bearer_expiry) with authenticated_panoptes: user = authenticated_panoptes.get('/me')[0]['users'][0] ids = ['admin user'] if not user['admin']: ids = [ project.href for project in Project.where( current_user_roles='collaborator') ] return { 'username': user['login'], 'email': user['email'], 'is_superuser': user['admin'], 'projects': ids }
def get_authenticated_panoptes(bearer_token, bearer_expiry): guest_authenticated_panoptes = Panoptes(endpoint=PanoptesUtils.base_url()) guest_authenticated_panoptes.bearer_token = bearer_token guest_authenticated_panoptes.logged_in = True bearer_expiry = datetime.strptime(bearer_expiry, "%Y-%m-%d %H:%M:%S.%f") guest_authenticated_panoptes.bearer_expires = (bearer_expiry) return guest_authenticated_panoptes
def get_user_details(self, response): with Panoptes() as p: p.bearer_token = response['access_token'] p.logged_in = True p.refresh_token = response['refresh_token'] p.bearer_expires = (datetime.now() + timedelta(seconds=response['expires_in'])) user = p.get('/me')[0]['users'][0] ids = ['admin user'] if not user['admin']: ids = [project.id for project in Project.where()] return { 'username': user['login'], 'email': user['email'], 'is_superuser': user['admin'], 'projects': ids, }
def apply(self, filenames): if self.pipeline.multiple_subject_sets: scope = self.bundle else: scope = self.pipeline theia_authenticated_client = Panoptes( endpoint=PanoptesUtils.base_url(), client_id=PanoptesUtils.client_id(), client_secret=PanoptesUtils.client_secret() ) with theia_authenticated_client: target_set = self._get_subject_set(scope, self.project.id, scope.name_subject_set()) using_manifest = False metadata_dictionary = {} path_example = filenames[0] manifest_file_location = path.join((path.dirname(path_example) + "_interstitial_products"), "manifest.csv") if self.include_metadata and path.exists(manifest_file_location): using_manifest = True with open(manifest_file_location, newline='') as csvfile: reader = csv.DictReader(csvfile) for row in reader: metadata_dictionary[row['#filename']] = row for filename in filenames: img = Image.open(filename) img.save(filename, 'png') #This line might have to be done with os.path to translate across OSes name_only = filename.split("/")[len(filename.split("/")) - 1] metadata = {} if using_manifest: metadata = metadata_dictionary[name_only] new_subject = self._create_subject(self.project.id, filename, metadata=metadata) target_set.add(new_subject)
class TestUploadSubject: @patch('theia.operations.panoptes_operations.UploadSubject.config', return_value=False) @patch('theia.api.models.Pipeline.name_subject_set', return_value='pipeline name') @patch( 'theia.operations.panoptes_operations.UploadSubject._get_subject_set', return_value=SubjectSet()) @patch( 'theia.operations.panoptes_operations.UploadSubject._create_subject', return_value=Subject()) @patch('panoptes_client.SubjectSet.add') @patch('PIL.Image.open', return_value=Mock()) def test_apply_single(self, mockOpen, mockAdd, mockCreate, mockGet, mockGetName, mockIncludeMetadata, *args): project = Project(id=8) pipeline = Pipeline(project=project) bundle = JobBundle(pipeline=pipeline) operation = UploadSubject(bundle) operation.apply(['some_file']) mockOpen.assert_called_once() mockGetName.assert_called_once() mockGet.assert_called_once_with(pipeline, 8, 'pipeline name') mockCreate.assert_called_once_with(8, 'some_file', metadata={}) mockAdd.assert_called_once_with(mockCreate.return_value) @patch('theia.operations.panoptes_operations.UploadSubject.config', return_value=False) @patch('theia.api.models.JobBundle.name_subject_set', return_value='bundle name') @patch( 'theia.operations.panoptes_operations.UploadSubject._get_subject_set', return_value=SubjectSet()) @patch( 'theia.operations.panoptes_operations.UploadSubject._create_subject', return_value=Subject()) @patch('panoptes_client.SubjectSet.add') @patch('PIL.Image.open', return_value=Mock()) def test_apply_multiple(self, mockOpen, mockAdd, mockCreate, mockGet, mockGetName, mockIncludeMetadata, *args): project = Project(id=8) pipeline = Pipeline(project=project, multiple_subject_sets=True) bundle = JobBundle(pipeline=pipeline) operation = UploadSubject(bundle) operation.apply(['some_file']) mockOpen.assert_called_once() mockGetName.assert_called_once() mockGet.assert_called_once_with(bundle, 8, 'bundle name') mockCreate.assert_called_once_with(8, 'some_file', metadata={}) mockAdd.assert_called_once_with(mockCreate.return_value) @patch('theia.api.models.JobBundle.save') @patch('theia.api.models.Pipeline.save') @patch( 'theia.operations.panoptes_operations.UploadSubject._create_subject_set' ) @patch('panoptes_client.SubjectSet.find', autospec=True) @patch('panoptes_client.Panoptes', return_value=Panoptes()) def test__get_subject_set(self, mock_client, mockFind, mockCreateSet, *args): mockFind.reset_mock() mockCreateSet.reset_mock() emptyJobBundle = JobBundle() linkedJobBundle = JobBundle(subject_set_id=3) emptyPipeline = Pipeline() linkedPipeline = Pipeline(subject_set_id=3) operation = UploadSubject(emptyJobBundle) result = operation._get_subject_set(emptyJobBundle, 8, 'some name') mockFind.assert_not_called() mockCreateSet.assert_called_once_with(8, 'some name') mockFind.reset_mock() mockCreateSet.reset_mock() operation = UploadSubject(linkedJobBundle) result = operation._get_subject_set(linkedJobBundle, 8, 'some name') mockFind.assert_called_once_with(3) mockCreateSet.assert_not_called() mockFind.reset_mock() mockCreateSet.reset_mock() operation = UploadSubject(emptyPipeline) result = operation._get_subject_set(emptyPipeline, 8, 'some name') mockFind.assert_not_called() mockCreateSet.assert_called_once_with(8, 'some name') mockFind.reset_mock() mockCreateSet.reset_mock() operation = UploadSubject(linkedPipeline) result = operation._get_subject_set(linkedPipeline, 8, 'some name') mockFind.assert_called_once_with(3) mockCreateSet.assert_not_called() @patch('panoptes_client.Project.find', return_value=Mock()) @patch('panoptes_client.Subject.save', autospec=True) @patch('panoptes_client.Subject.add_location', autospec=True) def test__create_subject_no_metadata(self, mockAdd, mockSave, mockFind): operation = UploadSubject(None) created_subject = operation._create_subject(1, 'some_file') mockFind.assert_called_once_with(1) mockAdd.assert_called_once_with(created_subject, 'some_file') # weird mockSave.assert_called_once() assert (mockFind.return_value == created_subject.links.project.id ) # weird @patch('panoptes_client.Project.find', return_value=Mock()) @patch('panoptes_client.Subject.save', autospec=True) @patch('panoptes_client.Subject.add_location', autospec=True) def test__create_subject_with_metadata(self, mockAdd, mockSave, mockFind): operation = UploadSubject(None) created_subject = operation._create_subject(1, 'some_file', {'foo': 'bar'}) mockFind.assert_called_once_with(1) mockAdd.assert_called_once_with(created_subject, 'some_file') # weird mockSave.assert_called_once() assert (mockFind.return_value == created_subject.links.project.id ) # weird assert (created_subject.metadata == {'foo': 'bar'}) @patch('panoptes_client.Project.find', return_value=Mock()) @patch('panoptes_client.SubjectSet.save', autospec=True) def test__create_subject_set(self, mockSave, mockFind): operation = UploadSubject(None) created_set = operation._create_subject_set(1, 'some name') mockFind.assert_called_once_with(1) mockSave.assert_called_once() assert (created_set.display_name == 'some name') assert (mockFind.return_value == created_set.links.project.id) # weird