当前位置: 首页>>代码示例>>Python>>正文


Python Experiment.delete方法代码示例

本文整理汇总了Python中tardis.tardis_portal.models.Experiment.delete方法的典型用法代码示例。如果您正苦于以下问题:Python Experiment.delete方法的具体用法?Python Experiment.delete怎么用?Python Experiment.delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tardis.tardis_portal.models.Experiment的用法示例。


在下文中一共展示了Experiment.delete方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TarDownloadTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class TarDownloadTestCase(TestCase):
    def setUp(self):
        # create user
        self.testuser = User(username="testuser")
        self.testuser.save()

        # create test experiment
        self.exp = Experiment(
            title="tar download test" * 15, created_by=self.testuser, public_access=Experiment.PUBLIC_ACCESS_FULL
        )
        self.exp.save()

        # create test dataset
        self.ds = self.exp.datasets.create(description="testing tar download dataset")

        datafile_content = "\n".join(["some data %d" % i for i in range(1000)])
        filesize = len(datafile_content)
        md5sum = hashlib.md5(datafile_content).hexdigest()
        # create test datafiles and datafile objects
        self.dfs = []
        for i in range(20):
            df = self.ds.datafile_set.create(
                filename="testfile%d.txt" % i,
                mimetype="text/plain",
                size=filesize,
                md5sum=md5sum,
                directory="/".join(["testdir%d" % i for i in range(i, i + 4)]),
            )
            df.file_object = StringIO(datafile_content)
            df.refresh_from_db()
            self.dfs.append(df)

        # mock client
        self.client = Client()

    def tearDown(self):
        # delete created objects and files

        [ds.delete() for ds in self.exp.datasets.all()]
        self.exp.delete()

    def test_tar_experiment_download(self):
        self.assertTrue(all(df.verified for df in self.dfs))
        response = self.client.get(
            reverse("tardis.tardis_portal.download.streaming_download_experiment", args=(self.exp.id, "tar"))
        )
        with NamedTemporaryFile("w") as tarfile:
            for c in response.streaming_content:
                tarfile.write(c)
            tarfile.flush()
            self.assertEqual(int(response["Content-Length"]), os.stat(tarfile.name).st_size)
            tf = TarFile(tarfile.name)
            for df in self.dfs:
                full_path = os.path.join(
                    self.exp.title.replace(" ", "_"), self.ds.description, df.directory, df.filename
                )
                tf.extract(full_path, "/tmp")
                self.assertEqual(os.stat(os.path.join("/tmp", full_path)).st_size, int(df.size))
开发者ID:jasonrig,项目名称:mytardis,代码行数:60,代码来源:test_tar_download.py

示例2: ExperimentACLTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]

#.........这里部分代码省略.........
            canRead=True,
            isOwner=True,
            aclOwnershipType=ExperimentACL.OWNER_OWNED,
            )
        acl.save()

        # user2 owns experiment2
        acl = ExperimentACL(
            pluginId=django_user,
            entityId=str(self.user2.id),
            experiment=self.experiment2,
            canRead=True,
            isOwner=True,
            aclOwnershipType=ExperimentACL.OWNER_OWNED,
            )
        acl.save()

        # experiment4 is accessible via location
        acl = ExperimentACL(
            pluginId='ip_address',
            entityId='127.0.0.1',
            experiment=self.experiment4,
            canRead=True,
            aclOwnershipType=ExperimentACL.SYSTEM_OWNED,
            )
        acl.save()

    def tearDown(self):
        self.client1.logout()
        self.client2.logout()
        self.client3.logout()
        self.client4.logout()

        self.experiment1.delete()
        self.experiment2.delete()
        self.experiment3.delete()
        self.experiment4.delete()

        self.user1.delete()
        self.user2.delete()
        self.user3.delete()
        self.user4.delete()

    def testReadAccess(self):
        login = self.client1.login(username=self.user1.username,
                                   password='secret')
        self.assertTrue(login)

        # user1 should be see experiment1
        response = self.client1.get('/experiment/view/%i/'
                                   % (self.experiment1.id))
        self.assertEqual(response.status_code, 200)

        # user1 should not be allowed to see experiment2
        response = self.client1.get('/experiment/view/%i/'
                                   % (self.experiment2.id))
        self.assertEqual(response.status_code, 403)

        # user1 should be allowed to see experiment3 as it's public
        response = self.client1.get('/experiment/view/%i/'
                                   % (self.experiment3.id))
        self.assertEqual(response.status_code, 200)

        # user1 should be allowed to see experiment4 based on his IP address
        response = self.client1.get('/experiment/view/%i/'
                                   % (self.experiment4.id))
开发者ID:aaryani,项目名称:aa-migration-test1,代码行数:70,代码来源:test_authorisation.py

示例3: ParameterSetManagerTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]

#.........这里部分代码省略.........
            data_type=ParameterName.LINK)
        self.parametername_exp_link.save()

        self.exp_link_param = DatafileParameter(
            parameterset=self.datafileparameterset,
            name=self.parametername_exp_link)
        exp_url = self.exp.get_absolute_url()  # /experiment/view/1/
        self.exp_link_param.set_value(exp_url)
        self.exp_link_param.save()

        # Create a ParameterName and Parameter of type LINK to a dataset
        self.parametername_dataset_link = ParameterName(
            schema=self.schema, name="dataset_link",
            full_name="This parameter is a dataset LINK",
            data_type=ParameterName.LINK)
        self.parametername_dataset_link.save()

        self.dataset_link_param = DatafileParameter(
            parameterset=self.datafileparameterset,
            name=self.parametername_dataset_link)
        dataset_url = self.dataset.get_absolute_url()  # /dataset/1/
        self.dataset_link_param.set_value(dataset_url)
        self.dataset_link_param.save()

        # Create a ParameterName type LINK to an unresolvable (non-URL)
        # free-text value
        self.parametername_unresolvable_link = ParameterName(
                schema=self.schema, name="freetext_link",
                full_name="This parameter is a non-URL LINK",
                data_type=ParameterName.LINK)
        self.parametername_unresolvable_link.save()

    def tearDown(self):
        self.exp.delete()
        self.user.delete()
        self.parametername1.delete()
        self.parametername2.delete()
        self.parametername3.delete()
        self.parametername_exp_link.delete()
        self.parametername_dataset_link.delete()
        self.parametername_unresolvable_link.delete()
        self.schema.delete()

    def test_existing_parameterset(self):

        psm = ParameterSetManager(parameterset=self.datafileparameterset)

        self.assertTrue(psm.get_schema().namespace ==
                        "http://localhost/psmtest/df/")

        self.assertTrue(psm.get_param("parameter1").string_value == "test1")

        self.assertTrue(psm.get_param("parameter2", True) == 2)

    def test_new_parameterset(self):

        psm = ParameterSetManager(parentObject=self.datafile,
                                  schema="http://localhost/psmtest/df2/")

        self.assertTrue(psm.get_schema().namespace ==
                        "http://localhost/psmtest/df2/")

        psm.set_param("newparam1", "test3", "New Parameter 1")

        self.assertTrue(psm.get_param("newparam1").string_value ==
                        "test3")
开发者ID:IntersectAustralia,项目名称:mytardis,代码行数:70,代码来源:test_parametersetmanager.py

示例4: DownloadTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class DownloadTestCase(TestCase):

    def setUp(self):
        # create a test user
        self.user = User.objects.create_user(username='DownloadTestUser',
                                             email='',
                                             password='secret')

        Location.force_initialize()

        # create a public experiment
        self.experiment1 = Experiment(title='Experiment 1',
                                      created_by=self.user,
                                      public_access=Experiment.PUBLIC_ACCESS_FULL)
        self.experiment1.save()

        # create a non-public experiment
        self.experiment2 = Experiment(title='Experiment 2',
                                      created_by=self.user,
                                      public_access=Experiment.PUBLIC_ACCESS_NONE)
        self.experiment2.save()

        # dataset1 belongs to experiment1
        self.dataset1 = Dataset()
        self.dataset1.save()
        self.dataset1.experiments.add(self.experiment1)
        self.dataset1.save()

        # dataset2 belongs to experiment2
        self.dataset2 = Dataset()
        self.dataset2.save()
        self.dataset2.experiments.add(self.experiment2)
        self.dataset2.save()

        # absolute path first
        filename1 = 'testfile.txt'
        filename2 = 'testfile.tiff'
        self.dest1 = abspath(join(settings.FILE_STORE_PATH, '%s/%s/'
                                  % (self.experiment1.id,
                                  self.dataset1.id)))
        self.dest2 = abspath(join(settings.FILE_STORE_PATH,
                                '%s/%s/'
                                  % (self.experiment2.id,
                                  self.dataset2.id)))
        if not exists(self.dest1):
            makedirs(self.dest1)
        if not exists(self.dest2):
            makedirs(self.dest2)

        testfile1 = abspath(join(self.dest1, filename1))
        f = open(testfile1, 'w')
        f.write("Hello World!\n")
        f.close()

        testfile2 = abspath(join(self.dest2, filename2))
        _generate_test_image(testfile2)

        self.datafile1 = self._build_datafile( \
            testfile1, filename1, self.dataset1,
            '%d/%d/%s' % (self.experiment1.id, self.dataset1.id, filename1))
                          
        self.datafile2 = self._build_datafile( \
            testfile2, filename2, self.dataset2,
            '%d/%d/%s' % (self.experiment2.id, self.dataset2.id, filename2))

    def _build_datafile(self, testfile, filename, dataset, url, 
                        protocol='', checksum=None, size=None, mimetype=''):
        filesize, sha512sum = get_size_and_sha512sum(testfile)
        datafile = Dataset_File(dataset=dataset, filename=filename,
                                mimetype=mimetype,
                                size=str(size if size != None else filesize), 
                                sha512sum=(checksum if checksum else sha512sum))
        datafile.save()
        if urlparse.urlparse(url).scheme == '':
            location = Location.get_location('local')
        else:
            location = Location.get_location_for_url(url)
            if not location:
                location = Location.load_location({
                    'name': filename, 'url': urlparse.urljoin(url, '.'), 
                    'type': 'external', 
                    'priority': 10, 'transfer_provider': 'local'})
        replica = Replica(datafile=datafile, protocol=protocol, url=url,
                          location=location)
        replica.verify()
        replica.save()
        return Dataset_File.objects.get(pk=datafile.pk)

    def tearDown(self):
        self.user.delete()
        self.experiment1.delete()
        self.experiment2.delete()
        rmtree(self.dest1)
        rmtree(self.dest2)

    def testView(self):
        client = Client()

        # check view of file1
        response = client.get('/datafile/view/%i/' % self.datafile1.id)
#.........这里部分代码省略.........
开发者ID:crawley,项目名称:mytardis,代码行数:103,代码来源:test_download.py

示例5: DownloadTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class DownloadTestCase(TestCase):

    def setUp(self):
        # create a test user
        self.user = User.objects.create_user(username='DownloadTestUser',
                                             email='',
                                             password='secret')

        # create a public experiment
        self.experiment1 = Experiment(title='Experiment 1',
                                      created_by=self.user,
                                      public=True)
        self.experiment1.save()

        # create a non-public experiment
        self.experiment2 = Experiment(title='Experiment 2',
                                      created_by=self.user,
                                      public=False)
        self.experiment2.save()

        # dataset1 belongs to experiment1
        self.dataset1 = Dataset(experiment=self.experiment1)
        self.dataset1.save()

        # dataset2 belongs to experiment2
        self.dataset2 = Dataset(experiment=self.experiment2)
        self.dataset2.save()

        # absolute path first
        filename = 'testfile.txt'
        self.dest1 = abspath(join(settings.FILE_STORE_PATH, '%s/%s/'
                                  % (self.experiment1.id,
                                  self.dataset1.id)))
        self.dest2 = abspath(join(settings.FILE_STORE_PATH,
                                '%s/%s/'
                                  % (self.experiment2.id,
                                  self.dataset2.id)))
        if not exists(self.dest1):
            makedirs(self.dest1)
        if not exists(self.dest2):
            makedirs(self.dest2)

        testfile1 = abspath(join(self.dest1, filename))
        f = open(testfile1, 'w')
        f.write("Hello World!\n")
        f.close()

        testfile2 = abspath(join(self.dest2, filename))
        f = open(testfile2, 'w')
        f.write("Hello World!\n")
        f.close()

        self.dataset_file1 = Dataset_File(dataset=self.dataset1,
                                          filename=filename,
                                          protocol='tardis',
                                          url='tardis://%s' % filename)
        self.dataset_file1.save()

        self.dataset_file2 = Dataset_File(dataset=self.dataset2,
                                          filename=basename(filename),
                                          protocol='tardis',
                                          url='tardis://%s' % filename)
        self.dataset_file2.save()

    def tearDown(self):
        self.user.delete()
        self.experiment1.delete()
        self.experiment2.delete()
        rmtree(self.dest1)
        rmtree(self.dest2)

    def testDownload(self):
        client = Client()

        # check download for experiment1
        response = client.get('/download/experiment/%i/zip/' % self.experiment1.id)
        self.assertEqual(response['Content-Disposition'],
                         'attachment; filename="experiment%s-complete.zip"'
                         % self.experiment1.id)
        self.assertEqual(response.status_code, 200)

        # check download of file1
        response = client.get('/download/datafile/%i/' % self.dataset_file1.id)

        self.assertEqual(response['Content-Disposition'],
                         'attachment; filename="%s"'
                         % self.dataset_file2.filename)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.content, 'Hello World!\n')

        # requesting file2 should be forbidden...
        response = client.get('/download/datafile/%i/' % self.dataset_file2.id)
        self.assertEqual(response.status_code, 403)

        # check dataset1 download
        response = client.post('/download/datafiles/',
                               {'expid': self.experiment1.id,
                                'dataset': [self.dataset1.id],
                                'datafile': []})
        self.assertEqual(response.status_code, 200)
#.........这里部分代码省略.........
开发者ID:ANSTO,项目名称:mytardis,代码行数:103,代码来源:test_download.py

示例6: UploadTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class UploadTestCase(TestCase):

    def setUp(self):
        from os import path, mkdir
        from tempfile import mkdtemp

        user = 'tardis_user1'
        pwd = 'secret'
        email = ''
        self.user = User.objects.create_user(user, email, pwd)

        self.userProfile = UserProfile(user=self.user)

        self.test_dir = mkdtemp()

        self.exp = Experiment(title='test exp1',
                institution_name='monash', created_by=self.user)
        self.exp.save()

        acl = ExperimentACL(
            pluginId=django_user,
            entityId=str(self.user.id),
            experiment=self.exp,
            canRead=True,
            isOwner=True,
            aclOwnershipType=ExperimentACL.OWNER_OWNED,
            )
        acl.save()

        self.dataset = \
            Dataset(description='dataset description...',
                           experiment=self.exp)
        self.dataset.save()

        self.experiment_path = path.join(settings.FILE_STORE_PATH,
                str(self.dataset.experiment.id))

        self.dataset_path = path.join(self.experiment_path,
                                      str(self.dataset.id))

        if not path.exists(self.experiment_path):
            mkdir(self.experiment_path)
        if not path.exists(self.dataset_path):
            mkdir(self.dataset_path)

        # write test file

        self.filename = 'testfile.txt'

        self.f1 = open(path.join(self.test_dir, self.filename), 'w')
        self.f1.write('Test file 1')
        self.f1.close()

        self.f1_size = path.getsize(path.join(self.test_dir,
                                    self.filename))

        self.f1 = open(path.join(self.test_dir, self.filename), 'r')

    def tearDown(self):
        from shutil import rmtree

        self.f1.close()
        rmtree(self.test_dir)
        rmtree(self.dataset_path)
        rmtree(self.experiment_path)
        self.exp.delete()

    def testFileUpload(self):
        from os import path

        c = Client()
        c.login(username='tardis_user1', password='secret')
        session_id = c.session.session_key

        response = c.post('/upload/' + str(self.dataset.id) + '/',
            {'Filedata': self.f1, 'session_id': session_id})

        test_files_db = \
            Dataset_File.objects.filter(dataset__id=self.dataset.id)

        self.assertTrue(path.exists(path.join(self.dataset_path,
                        self.filename)))
        self.assertTrue(self.dataset.id == 1)
        self.assertTrue(test_files_db[0].url == 'tardis://testfile.txt')

    def testUploadComplete(self):
        from django.http import QueryDict, HttpRequest
        from tardis.tardis_portal.views import upload_complete
        data = [('filesUploaded', '1'), ('speed', 'really fast!'),
                ('allBytesLoaded', '2'), ('errorCount', '0')]
        post = QueryDict('&'.join(['%s=%s' % (k, v) for (k, v) in
                         data]))
        request = HttpRequest()
        request.POST = post
        response = upload_complete(request)
        self.assertTrue('<p>Number: 1</p>' in response.content)
        self.assertTrue('<p>Errors: 0</p>' in response.content)
        self.assertTrue('<p>Bytes: 2</p>' in response.content)
        self.assertTrue('<p>Speed: really fast!</p>'
                        in response.content)
开发者ID:aaryani,项目名称:aa-migration-test1,代码行数:102,代码来源:test_views.py

示例7: ViewTemplateContextsTest

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class ViewTemplateContextsTest(TestCase):

    def setUp(self):
        """
        setting up essential objects, copied from tests above
        """
        Location.force_initialize()
        self.location = Location.get_location('local')

        user = 'tardis_user1'
        pwd = 'secret'
        email = ''
        self.user = User.objects.create_user(user, email, pwd)
        self.userProfile = UserProfile(user=self.user).save()
        self.exp = Experiment(title='test exp1',
                              institution_name='monash', created_by=self.user)
        self.exp.save()
        self.acl = ObjectACL(
            pluginId=django_user,
            entityId=str(self.user.id),
            content_object=self.exp,
            canRead=True,
            isOwner=True,
            aclOwnershipType=ObjectACL.OWNER_OWNED,
        )
        self.acl.save()
        self.dataset = Dataset(description='dataset description...')
        self.dataset.save()
        self.dataset.experiments.add(self.exp)
        self.dataset.save()

        self.dataset_file = Dataset_File(dataset=self.dataset,
                                         size=42, filename="foo",
                                         md5sum="junk")
        self.dataset_file.save()
        self.replica = Replica(datafile=self.dataset_file,
                               url="http://foo",
                               location=self.location,
                               verified=False)
        self.replica.save()

    def tearDown(self):
        self.user.delete()
        self.exp.delete()
        self.dataset.delete()
        self.dataset_file.delete()
        self.acl.delete()

    def testExperimentView(self):
        """
        test some template context parameters for an experiment view
        """
        from tardis.tardis_portal.views import view_experiment
        from tardis.tardis_portal.shortcuts import render_response_index
        from django.http import HttpRequest
        from django.template import Context
        import sys

        # Default behavior
        views_module = flexmock(sys.modules['tardis.tardis_portal.views'])
        request = HttpRequest()
        request.user=self.user
        request.groups=[]
        context = {'organization': ['classic', 'test', 'test2'],
                   'default_organization': 'classic',
                   'default_format': 'zip',
                   'protocol': [['zip', '/download/experiment/1/zip/'],
                                ['tar', '/download/experiment/1/tar/']]}
        views_module.should_call('render_response_index'). \
            with_args(_AnyMatcher(), "tardis_portal/view_experiment.html",
                      _ContextMatcher(context))
        response = view_experiment(request, experiment_id=self.exp.id)
        self.assertEqual(response.status_code, 200)

        # Behavior with USER_AGENT_SENSING enabled and a request.user_agent
        saved_setting = getattr(settings, "USER_AGENT_SENSING", None)
        try:
            setattr(settings, "USER_AGENT_SENSING", True)
            request = HttpRequest()
            request.user=self.user
            request.groups=[]
            mock_agent = _MiniMock(os=_MiniMock(family="Macintosh"))
            setattr(request, 'user_agent', mock_agent);
            context = {'organization': ['classic', 'test', 'test2'],
                       'default_organization': 'classic',
                       'default_format': 'tar',
                       'protocol': [['tar', '/download/experiment/1/tar/']]}
            views_module.should_call('render_response_index'). \
                with_args(_AnyMatcher(), "tardis_portal/view_experiment.html",
                          _ContextMatcher(context))
            response = view_experiment(request, experiment_id=self.exp.id)
            self.assertEqual(response.status_code, 200)
        finally:
            if saved_setting != None:
                setattr(settings, "USER_AGENT_SENSING", saved_setting)
            else:
                delattr(settings, "USER_AGENT_SENSING")


    def testDatasetView(self):
#.........这里部分代码省略.........
开发者ID:crawley,项目名称:mytardis,代码行数:103,代码来源:test_views.py

示例8: ContextualViewTest

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class ContextualViewTest(TestCase):

    def setUp(self):
        """
        setting up essential objects, copied from tests above
        """
        user = 'tardis_user1'
        pwd = 'secret'
        email = ''
        self.user = User.objects.create_user(user, email, pwd)
        self.userProfile = UserProfile(user=self.user).save()
        self.exp = Experiment(title='test exp1',
                              institution_name='monash', created_by=self.user)
        self.exp.save()
        self.acl = ObjectACL(
            pluginId=django_user,
            entityId=str(self.user.id),
            content_object=self.exp,
            canRead=True,
            isOwner=True,
            aclOwnershipType=ObjectACL.OWNER_OWNED,
        )
        self.acl.save()
        self.dataset = Dataset(description='dataset description...')
        self.dataset.save()
        self.dataset.experiments.add(self.exp)
        self.dataset.save()

        self.dataset_file = Dataset_File(dataset=self.dataset,
                                         size=42, filename="foo",
                                         md5sum="junk")
        self.dataset_file.save()

        self.testschema = Schema(namespace="http://test.com/test/schema",
                                 name="Test View",
                                 type=Schema.DATAFILE,
                                 hidden=True)
        self.testschema.save()
        self.dfps = DatafileParameterSet(dataset_file=self.dataset_file,
                                         schema=self.testschema)
        self.dfps.save()

    def tearDown(self):
        self.user.delete()
        self.exp.delete()
        self.dataset.delete()
        self.dataset_file.delete()
        self.testschema.delete()
        self.dfps.delete()
        self.acl.delete()

    def testDetailsDisplay(self):
        """
        test display of view for an existing schema and no display for an undefined one.
        """
        from tardis.tardis_portal.views import display_datafile_details
        request = flexmock(user=self.user, groups=[("testgroup",flexmock())])
        with self.settings(DATAFILE_VIEWS=[("http://test.com/test/schema", "/test/url"),
                                           ("http://does.not.exist", "/false/url")]):
            response = display_datafile_details(request, dataset_file_id=self.dataset_file.id)
            self.assertEqual(response.status_code, 200)
            self.assertTrue("/ajax/parameters/" in response.content)
            self.assertTrue("/test/url" in response.content)
            self.assertFalse("/false/url" in response.content)
开发者ID:crawley,项目名称:mytardis,代码行数:66,代码来源:test_views.py

示例9: DownloadTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class DownloadTestCase(TestCase):

    def setUp(self):
        # create a test user
        self.user = User.objects.create_user(username='DownloadTestUser',
                                             email='',
                                             password='secret')

        # create a public experiment
        self.experiment1 = Experiment(title='Experiment 1',
                                      created_by=self.user,
                                      public_access=Experiment.PUBLIC_ACCESS_FULL)
        self.experiment1.save()

        # create a non-public experiment
        self.experiment2 = Experiment(title='Experiment 2',
                                      created_by=self.user,
                                      public_access=Experiment.PUBLIC_ACCESS_NONE)
        self.experiment2.save()

        # dataset1 belongs to experiment1
        self.dataset1 = Dataset()
        self.dataset1.save()
        self.dataset1.experiments.add(self.experiment1)
        self.dataset1.save()


        # dataset2 belongs to experiment2
        self.dataset2 = Dataset()
        self.dataset2.save()
        self.dataset2.experiments.add(self.experiment2)
        self.dataset2.save()

        # absolute path first
        filename1 = 'testfile.txt'
        filename2 = 'testfile.tiff'
        self.dest1 = abspath(join(settings.FILE_STORE_PATH, '%s/%s/'
                                  % (self.experiment1.id,
                                  self.dataset1.id)))
        self.dest2 = abspath(join(settings.FILE_STORE_PATH,
                                '%s/%s/'
                                  % (self.experiment2.id,
                                  self.dataset2.id)))
        if not exists(self.dest1):
            makedirs(self.dest1)
        if not exists(self.dest2):
            makedirs(self.dest2)

        testfile1 = abspath(join(self.dest1, filename1))
        f = open(testfile1, 'w')
        f.write("Hello World!\n")
        f.close()

        testfile2 = abspath(join(self.dest2, filename2))
        if IMAGEMAGICK_AVAILABLE:
            with Image(filename='logo:') as img:
                img.format = 'tiff'
                img.save(filename=testfile2)
        else:
            # Apparently ImageMagick isn't installed...
            # Write a "fake" TIFF file
            f = open(testfile2, 'w')
            f.write("II\x2a\x00")
            f.close()


        size, sha512sum = get_size_and_sha512sum(testfile1)
        self.dataset_file1 = Dataset_File(dataset=self.dataset1,
                                          filename=filename1,
                                          protocol='',
                                          size=size,
                                          sha512sum=sha512sum,
                                          url='%d/%d/%s'
                                              % (self.experiment1.id,
                                                 self.dataset1.id,
                                                 filename1))
        self.dataset_file1.verify()
        self.dataset_file1.save()

        size, sha512sum = get_size_and_sha512sum(testfile2)
        self.dataset_file2 = Dataset_File(dataset=self.dataset2,
                                          filename=basename(filename2),
                                          protocol='',
                                          size=size,
                                          sha512sum=sha512sum,
                                          url='%d/%d/%s'
                                            % (self.experiment2.id,
                                               self.dataset2.id,
                                               filename2))
        self.dataset_file2.verify()
        self.dataset_file2.save()

    def tearDown(self):
        self.user.delete()
        self.experiment1.delete()
        self.experiment2.delete()
        rmtree(self.dest1)
        rmtree(self.dest2)

    def testView(self):
#.........这里部分代码省略.........
开发者ID:JMSS-IT-11-2012,项目名称:mytardis,代码行数:103,代码来源:test_download.py

示例10: TarDownloadTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class TarDownloadTestCase(TestCase):

    def setUp(self):
        # create user
        self.testuser = User(username='testuser')
        self.testuser.save()

        # create test experiment
        self.exp = Experiment(title='tar download test' * 15,
                              created_by=self.testuser,
                              public_access=Experiment.PUBLIC_ACCESS_FULL)
        self.exp.save()

        # create test dataset
        self.ds = self.exp.datasets.create(
            description="testing tar download dataset")

        datafile_content = "\n".join(['some data %d' % i
                                      for i in range(1000)])
        filesize = len(datafile_content)
        md5sum = hashlib.md5(datafile_content).hexdigest()
        # create test datafiles and datafile objects
        self.dfs = []
        for i in range(20):
            df = self.ds.datafile_set.create(
                filename='testfile%d.txt' % i,
                mimetype='text/plain',
                size=filesize,
                md5sum=md5sum,
                directory='/'.join([
                    'testdir%d' % i
                    for i in range(i, i + 4)
                ]))
            df.file_object = StringIO(datafile_content)
            df.refresh_from_db()
            self.dfs.append(df)

        # mock client
        self.client = Client()

    def tearDown(self):
        # delete created objects and files

        [ds.delete() for ds in self.exp.datasets.all()]
        self.exp.delete()

    def test_tar_experiment_download(self):
        self.assertTrue(all(df.verified for df in self.dfs))
        response = self.client.get(reverse(
            'tardis.tardis_portal.download.streaming_download_experiment',
            args=(self.exp.id, 'tar')))
        with NamedTemporaryFile('w') as tarfile:
            for c in response.streaming_content:
                tarfile.write(c)
            tarfile.flush()
            self.assertEqual(int(response['Content-Length']),
                             os.stat(tarfile.name).st_size)
            tf = TarFile(tarfile.name)
            if settings.EXP_SPACES_TO_UNDERSCORES:
                exp_title = self.exp.title.replace(' ', '_')
            else:
                exp_title = self.exp.title
            exp_title = quote(exp_title,
                              safe=settings.SAFE_FILESYSTEM_CHARACTERS)
            for df in self.dfs:
                full_path = os.path.join(
                    exp_title,
                    quote(self.ds.description,
                          safe=settings.SAFE_FILESYSTEM_CHARACTERS),
                    df.directory, df.filename)
                # docker has a file path limit of ~240 characters
                if os.environ.get('DOCKER_BUILD', 'false') != 'true':
                    tf.extract(full_path, '/tmp')
                    self.assertEqual(
                        os.stat(os.path.join('/tmp', full_path)).st_size,
                        int(df.size))
开发者ID:keithschulze,项目名称:mytardis,代码行数:78,代码来源:test_tar_download.py

示例11: ObjectACLTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]

#.........这里部分代码省略.........
        acl = ObjectACL(
            pluginId=django_user,
            entityId=str(self.user1.id),
            content_object=self.experiment1,
            canRead=True,
            isOwner=True,
            aclOwnershipType=ObjectACL.OWNER_OWNED,
            )
        acl.save()

        # user2 owns experiment2
        acl = ObjectACL(
            pluginId=django_user,
            entityId=str(self.user2.id),
            content_object=self.experiment2,
            canRead=True,
            isOwner=True,
            aclOwnershipType=ObjectACL.OWNER_OWNED,
            )
        acl.save()

        # experiment4 is accessible via location
        acl = ObjectACL(
            pluginId='ip_address',
            entityId='127.0.0.1',
            content_object=self.experiment4,
            canRead=True,
            aclOwnershipType=ObjectACL.SYSTEM_OWNED,
        )
        acl.save()

    def tearDown(self):

        self.experiment1.delete()
        self.experiment2.delete()
        self.experiment3.delete()
        self.experiment4.delete()

        self.user1.delete()
        self.user2.delete()
        self.user3.delete()
        self.user4.delete()

        self.client1.logout()
        self.client2.logout()
        self.client3.logout()
        self.client4.logout()

    def testChangeUserPermissions(self):
        login1 = self.client1.login(username=self.user1.username,
                                   password='secret')
        self.assertTrue(login1)

        login3 = self.client3.login(username=self.user3.username,
                                   password='secret')
        self.assertTrue(login3)

        # user1 should be see experiment1
        response = self.client1.get('/experiment/view/%i/'
                                   % (self.experiment1.id))
        self.assertEqual(response.status_code, 200)

        # user3 should not be able to see experiment1
        response = self.client3.get('/experiment/view/%i/'
                                   % (self.experiment1.id))
        self.assertEqual(response.status_code, 403)
开发者ID:IntersectAustralia,项目名称:mytardis,代码行数:70,代码来源:test_authorisation.py

示例12: ExperimentACLTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class ExperimentACLTestCase(TestCase):
    urls = 'tardis.urls'

    def setUp(self):

        # create a couple of test users
        self.user1 = User.objects.create_user('testuser1', '', 'secret')
        self.user2 = User.objects.create_user('testuser2', '', 'secret')
        self.user3 = User.objects.create_user('testuser3', '', 'secret')
        self.user4 = User.objects.create_user('testuser4', '', 'secret')

        # each user will have their own client
        self.client1 = Client()
        self.client2 = Client()
        self.client3 = Client()
        self.client4 = Client()

        # user1 will own experiment1
        self.experiment1 = Experiment(
            title='Experiment1',
            institution_name='Australian Synchrotron',
            approved=True,
            public=False,
            created_by=self.user1,
            )
        self.experiment1.save()

        # user2 will own experiment2
        self.experiment2 = Experiment(
            title='Experiment2',
            institution_name='Australian Synchrotron',
            approved=True,
            public=False,
            created_by=self.user2,
            )
        self.experiment2.save()

        # experiment3 is public
        self.experiment3 = Experiment(
            title='Experiment3',
            institution_name='Australian Synchrotron',
            approved=True,
            public=True,
            created_by=self.user3,
            )
        self.experiment3.save()

        # experiment4 will be accessible based on location information
        self.experiment4 = Experiment(
            title='Experiment4',
            institution_name='Australian Synchrotron',
            approved=True,
            public=False,
            created_by=self.user1,
            )
        self.experiment4.save()

        # user1 owns experiment1
        acl = ExperimentACL(
            pluginId=django_user,
            entityId=str(self.user1.id),
            experiment=self.experiment1,
            canRead=True,
            isOwner=True,
            aclOwnershipType=ExperimentACL.OWNER_OWNED,
            )
        acl.save()

        # user2 owns experiment2
        acl = ExperimentACL(
            pluginId=django_user,
            entityId=str(self.user2.id),
            experiment=self.experiment2,
            canRead=True,
            isOwner=True,
            aclOwnershipType=ExperimentACL.OWNER_OWNED,
            )
        acl.save()

        # experiment4 is accessible via location
        acl = ExperimentACL(
            pluginId='ip_address',
            entityId='127.0.0.1',
            experiment=self.experiment4,
            canRead=True,
            aclOwnershipType=ExperimentACL.SYSTEM_OWNED,
            )
        acl.save()

    def tearDown(self):
        self.client1.logout()
        self.client2.logout()
        self.client3.logout()
        self.client4.logout()

        self.experiment1.delete()
        self.experiment2.delete()
        self.experiment3.delete()
        self.experiment4.delete()

#.........这里部分代码省略.........
开发者ID:grischa,项目名称:mytardis-mrtardis,代码行数:103,代码来源:test_authorisation.py

示例13: ViewTemplateContextsTest

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class ViewTemplateContextsTest(TestCase):
    def setUp(self):
        """
        setting up essential objects, copied from tests above
        """

        user = "tardis_user1"
        pwd = "secret"
        email = ""
        self.user = User.objects.create_user(user, email, pwd)
        self.userProfile = self.user.userprofile
        self.exp = Experiment(title="test exp1", institution_name="monash", created_by=self.user)
        self.exp.save()
        self.acl = ObjectACL(
            pluginId=django_user,
            entityId=str(self.user.id),
            content_object=self.exp,
            canRead=True,
            isOwner=True,
            aclOwnershipType=ObjectACL.OWNER_OWNED,
        )
        self.acl.save()
        self.dataset = Dataset(description="dataset description...")
        self.dataset.save()
        self.dataset.experiments.add(self.exp)
        self.dataset.save()

        self.datafile = DataFile(dataset=self.dataset, size=42, filename="foo", md5sum="junk")
        self.datafile.save()

    def tearDown(self):
        self.user.delete()
        self.exp.delete()
        self.dataset.delete()
        self.datafile.delete()
        self.acl.delete()

    def testExperimentView(self):
        """
        test some template context parameters for an experiment view
        """
        from tardis.tardis_portal.views import ExperimentView
        from django.http import HttpRequest
        import sys

        # Default behavior
        views_module = flexmock(sys.modules["tardis.tardis_portal.views"])
        request = HttpRequest()
        request.method = "GET"
        request.user = self.user
        request.groups = []
        context = {
            "organization": ["test", "test2"],
            "default_organization": "test",
            "default_format": "tar",
            "protocol": [["tgz", "/download/experiment/1/tgz/"], ["tar", "/download/experiment/1/tar/"]],
        }
        views_module.should_call("render_response_index").with_args(
            _AnyMatcher(), "tardis_portal/view_experiment.html", _ContextMatcher(context)
        )
        view_fn = ExperimentView.as_view()
        response = view_fn(request, experiment_id=self.exp.id)
        self.assertEqual(response.status_code, 200)

        # Behavior with USER_AGENT_SENSING enabled and a request.user_agent
        saved_setting = getattr(settings, "USER_AGENT_SENSING", None)
        try:
            setattr(settings, "USER_AGENT_SENSING", True)
            request = HttpRequest()
            request.method = "GET"
            request.user = self.user
            request.groups = []
            mock_agent = _MiniMock(os=_MiniMock(family="Macintosh"))
            setattr(request, "user_agent", mock_agent)
            context = {
                "organization": ["classic", "test", "test2"],
                "default_organization": "classic",
                "default_format": "tar",
                "protocol": [["tar", "/download/experiment/1/tar/"]],
            }
            views_module.should_call("render_response_index").with_args(
                _AnyMatcher(), "tardis_portal/view_experiment.html", _ContextMatcher(context)
            )
            view_fn = ExperimentView.as_view()
            response = view_fn(request, experiment_id=self.exp.id)
            self.assertEqual(response.status_code, 200)
        finally:
            if saved_setting is not None:
                setattr(settings, "USER_AGENT_SENSING", saved_setting)
            else:
                delattr(settings, "USER_AGENT_SENSING")

    def testDatasetView(self):
        """
        test some context parameters for a dataset view
        """
        from tardis.tardis_portal.views import DatasetView
        from django.http import HttpRequest
        import sys

#.........这里部分代码省略.........
开发者ID:grischa,项目名称:mytardis,代码行数:103,代码来源:test_views.py

示例14: UploadTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class UploadTestCase(TestCase):
    def setUp(self):
        from os import path, mkdir
        from tempfile import mkdtemp

        user = "tardis_user1"
        pwd = "secret"
        email = ""
        self.user = User.objects.create_user(user, email, pwd)

        self.userProfile = self.user.userprofile

        self.test_dir = mkdtemp()

        self.exp = Experiment(title="test exp1", institution_name="monash", created_by=self.user)
        self.exp.save()

        acl = ObjectACL(
            pluginId=django_user,
            entityId=str(self.user.id),
            content_object=self.exp,
            canRead=True,
            isOwner=True,
            aclOwnershipType=ObjectACL.OWNER_OWNED,
        )
        acl.save()

        self.dataset = Dataset(description="dataset description...")
        self.dataset.save()
        self.dataset.experiments.add(self.exp)
        self.dataset.save()

        path_parts = [
            settings.FILE_STORE_PATH,
            "%s-%s" % (quote(self.dataset.description, safe="") or "untitled", self.dataset.id),
        ]
        self.dataset_path = path.join(*path_parts)

        if not path.exists(self.dataset_path):
            mkdir(self.dataset_path)

        # write test file

        self.filename = "testfile.txt"

        self.f1 = open(path.join(self.test_dir, self.filename), "w")
        self.f1.write("Test file 1")
        self.f1.close()

        self.f1_size = path.getsize(path.join(self.test_dir, self.filename))

        self.f1 = open(path.join(self.test_dir, self.filename), "r")

    def tearDown(self):
        from shutil import rmtree

        self.f1.close()
        rmtree(self.test_dir)
        rmtree(self.dataset_path)
        self.exp.delete()

    def testFileUpload(self):
        from os import path

        c = Client()
        c.login(username="tardis_user1", password="secret")
        session_id = c.session.session_key

        c.post("/upload/" + str(self.dataset.id) + "/", {"Filedata": self.f1, "session_id": session_id})

        test_files_db = DataFile.objects.filter(dataset__id=self.dataset.id)
        self.assertTrue(path.exists(path.join(self.dataset_path, self.filename)))
        target_id = Dataset.objects.first().id
        self.assertEqual(self.dataset.id, target_id)
        url = test_files_db[0].file_objects.all()[0].uri
        self.assertEqual(url, path.relpath("%s/testfile.txt" % self.dataset_path, settings.FILE_STORE_PATH))
        self.assertTrue(test_files_db[0].file_objects.all()[0].verified)

    def testUploadComplete(self):
        from django.http import QueryDict, HttpRequest
        from tardis.tardis_portal.views import upload_complete

        data = [("filesUploaded", "1"), ("speed", "really fast!"), ("allBytesLoaded", "2"), ("errorCount", "0")]
        post = QueryDict("&".join(["%s=%s" % (k, v) for (k, v) in data]))
        request = HttpRequest()
        request.POST = post
        response = upload_complete(request)
        self.assertTrue("<p>Number: 1</p>" in response.content)
        self.assertTrue("<p>Errors: 0</p>" in response.content)
        self.assertTrue("<p>Bytes: 2</p>" in response.content)
        self.assertTrue("<p>Speed: really fast!</p>" in response.content)
开发者ID:grischa,项目名称:mytardis,代码行数:93,代码来源:test_views.py

示例15: DownloadTestCase

# 需要导入模块: from tardis.tardis_portal.models import Experiment [as 别名]
# 或者: from tardis.tardis_portal.models.Experiment import delete [as 别名]
class DownloadTestCase(TestCase):

    def setUp(self):
        # create a test user
        self.user = User.objects.create_user(username='DownloadTestUser',
                                             email='',
                                             password='secret')

        # create a public experiment
        self.experiment1 = Experiment(
            title='Experiment 1',
            created_by=self.user,
            public_access=Experiment.PUBLIC_ACCESS_FULL)
        self.experiment1.save()

        # create a non-public experiment
        self.experiment2 = Experiment(
            title='Experiment 2',
            created_by=self.user,
            public_access=Experiment.PUBLIC_ACCESS_NONE)
        self.experiment2.save()

        # dataset1 belongs to experiment1
        self.dataset1 = Dataset(description='dangerous;name')
        self.dataset1.save()
        self.dataset1.experiments.add(self.experiment1)
        self.dataset1.save()

        # dataset2 belongs to experiment2
        self.dataset2 = Dataset(description='terrible\nname')
        self.dataset2.save()
        self.dataset2.experiments.add(self.experiment2)
        self.dataset2.save()

        # absolute path first
        filename1 = 'testfile.txt'
        filename2 = 'testfile.tiff'
        self.dest1 = abspath(join(settings.FILE_STORE_PATH, '%s/%s/'
                                  % (self.experiment1.id,
                                     self.dataset1.id)))
        self.dest2 = abspath(join(settings.FILE_STORE_PATH,
                                  '%s/%s/'
                                  % (self.experiment2.id,
                                     self.dataset2.id)))
        if not exists(self.dest1):
            makedirs(self.dest1)
        if not exists(self.dest2):
            makedirs(self.dest2)

        testfile1 = abspath(join(self.dest1, filename1))
        f = open(testfile1, 'w')
        f.write("Hello World!\n")
        f.close()

        testfile2 = abspath(join(self.dest2, filename2))
        _generate_test_image(testfile2)

        self.datafile1 = self._build_datafile(testfile1, filename1,
                                              self.dataset1)

        self.datafile2 = self._build_datafile(testfile2, filename2,
                                              self.dataset2)

    def _build_datafile(self, testfile, filename, dataset, checksum=None,
                        size=None, mimetype=''):
        filesize, sha512sum = get_size_and_sha512sum(testfile)
        datafile = DataFile(dataset=dataset, filename=filename,
                            mimetype=mimetype,
                            size=size if size is not None else filesize,
                            sha512sum=(checksum if checksum else sha512sum))
        datafile.save()
        dfo = DataFileObject(
            datafile=datafile,
            storage_box=datafile.get_default_storage_box())
        dfo.save()
        with open(testfile, 'r') as sourcefile:
            dfo.file_object = sourcefile
        return DataFile.objects.get(pk=datafile.pk)

    def tearDown(self):
        self.user.delete()
        self.experiment1.delete()
        self.experiment2.delete()
        rmtree(self.dest1)
        rmtree(self.dest2)

    def testView(self):
        client = Client()

        # check view of file1
        response = client.get('/datafile/view/%i/' % self.datafile1.id)

        self.assertEqual(response['Content-Disposition'],
                         'inline; filename="%s"'
                         % self.datafile1.filename)
        self.assertEqual(response.status_code, 200)
        response_content = ""
        for c in response.streaming_content:
            response_content += c
        self.assertEqual(response_content, 'Hello World!\n')
#.........这里部分代码省略.........
开发者ID:keithschulze,项目名称:mytardis,代码行数:103,代码来源:test_download.py


注:本文中的tardis.tardis_portal.models.Experiment.delete方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。