Пример #1
0
 def setup_models(self):
     self.project = ProjectFactory.create()
     self.relationship = TenureRelationshipFactory.create(
         project=self.project)
     self.attached = ResourceFactory.create(
         project=self.project, content_object=self.relationship)
     self.unattached = ResourceFactory.create(project=self.project)
Пример #2
0
    def test_reverse_ordering(self):
        su = SpatialUnitFactory.create(project=self.prj)
        ResourceFactory.create_from_kwargs([
            {
                'content_object': su,
                'project': self.prj,
                'name': 'A'
            },
            {
                'content_object': su,
                'project': self.prj,
                'name': 'B'
            },
            {
                'content_object': su,
                'project': self.prj,
                'name': 'C'
            },
        ])

        response = self.request(user=self.user,
                                url_kwargs={'location': su.id},
                                get_data={'ordering': '-name'})
        assert response.status_code == 200
        assert len(response.content['results']) == 3
        names = [resource['name'] for resource in response.content['results']]
        assert (names == sorted(names, reverse=True))
 def setup_models(self):
     self.project = ProjectFactory.create()
     self.relationship = TenureRelationshipFactory.create(
         project=self.project)
     self.attached = ResourceFactory.create(
         project=self.project, content_object=self.relationship)
     self.unattached = ResourceFactory.create(project=self.project)
    def test_ordering(self):
        tenure = TenureRelationshipFactory.create(project=self.prj)
        ResourceFactory.create_from_kwargs([
            {
                'content_object': tenure,
                'project': self.prj,
                'name': 'A'
            },
            {
                'content_object': tenure,
                'project': self.prj,
                'name': 'B'
            },
            {
                'content_object': tenure,
                'project': self.prj,
                'name': 'C'
            },
        ])

        response = self.request(user=self.user,
                                url_kwargs={'tenure_rel_id': tenure.id},
                                get_data={'ordering': 'name'})
        assert response.status_code == 200
        assert len(response.content['results']) == 3
        names = [resource['name'] for resource in response.content['results']]
        assert (names == sorted(names))
Пример #5
0
    def test_search_filter(self):
        file = self.get_file('/resources/tests/files/image.jpg', 'rb')
        not_found = self.storage.save('resources/bild.jpg', file.read())
        file.close()
        su = SpatialUnitFactory.create(project=self.prj)
        ResourceFactory.create_from_kwargs([{
            'content_object': su,
            'project': self.prj,
            'file': self.file_name
        }, {
            'content_object': su,
            'project': self.prj,
            'file': self.file_name
        }, {
            'content_object': su,
            'project': self.prj,
            'file': not_found
        }])

        response = self.request(user=self.user,
                                url_kwargs={
                                    'organization': self.prj.organization.slug,
                                    'project': self.prj.slug,
                                    'location': su.id
                                },
                                get_data={'search': 'image'})
        assert response.status_code == 200
        assert len(response.content['results']) == 2
    def test_search_filter(self):
        not_found = self.storage.save('resources/bild.jpg', self.file)
        tenure = TenureRelationshipFactory.create(project=self.prj)
        ResourceFactory.create_from_kwargs([{
            'content_object': tenure,
            'project': self.prj,
            'file': self.file_name
        }, {
            'content_object': tenure,
            'project': self.prj,
            'file': self.file_name
        }, {
            'content_object': tenure,
            'project': self.prj,
            'file': not_found
        }])

        response = self.request(user=self.user,
                                url_kwargs={
                                    'organization': self.prj.organization.slug,
                                    'project': self.prj.slug,
                                    'tenure_rel_id': tenure.id
                                },
                                get_data={'search': 'image'})
        assert response.status_code == 200
        assert len(response.content) == 2
Пример #7
0
    def setUp(self):
        self.url = reverse('resource-list')

        self.resource_1 = ResourceFactory.create(status=RESOURCE_APPROVED)
        self.like_url = reverse('resource-like',
                                kwargs={'pk': self.resource_1.id})
        self.tried_url = reverse('resource-tried',
                                 kwargs={'pk': self.resource_1.id})

        self.organisation = OrganisationFactory.create()
        self.user = UserFactory.create(
            is_staff=True,
            approved_organisations=[self.organisation],
            password='******')
        self.logged_in_client = APIClient()
        self.logged_in_client.login(username=self.user.email,
                                    password='******')

        ResourceFactory.create(status=RESOURCE_REJECTED)
        ResourceFactory.create(privacy=[self.organisation],
                               status=RESOURCE_APPROVED)

        self.url = reverse('resource-list')
        self.update_url = reverse('resource-detail',
                                  kwargs={'pk': self.resource_1.id})
    def test_archived_resources_not_listed(self):
        ResourceFactory.create(
            project=self.prj, content_object=self.su, archived=True)
        response = self.request(user=self.user)
        assert response.status_code == 200
        assert len(response.content) == 2

        returned_ids = [r['id'] for r in response.content]
        assert all(res.id in returned_ids for res in self.resources)
Пример #9
0
    def test_archived_resources_not_listed(self):
        ResourceFactory.create(
            project=self.prj, content_object=self.party, archived=True)
        response = self.request(user=self.user)
        assert response.status_code == 200
        assert len(response.content) == 2

        returned_ids = [r['id'] for r in response.content]
        assert all(res.id in returned_ids for res in self.resources)
Пример #10
0
    def test_get_most_published_this_week_30_days_old(self):
        ResourceFactory.create_batch(
            size=30,
            organisation=self.organisation,
            created_at=timezone.now() - timedelta(days=30),
            status=RESOURCE_APPROVED,
        )
        organisation = Organisation.get_most_published_this_week()

        self.assertIsNone(organisation)
    def setup_models(self):
        self.user = UserFactory.create()
        assign_policies(self.user)
        self.prj = ProjectFactory.create(slug='test-project', access='public')
        self.su = SpatialUnitFactory.create(project=self.prj)
        self.resources = ResourceFactory.create_batch(
            2, project=self.prj, content_object=self.su)
        ResourceFactory.create(project=self.prj)

        self.file = self.get_file('/resources/tests/files/image.jpg', 'rb')
        self.file_name = self.storage.save('resources/image.jpg', self.file)
    def setup_models(self):
        self.user = UserFactory.create()
        assign_policies(self.user)
        self.prj = ProjectFactory.create(slug='test-project', access='public')
        self.tenure = TenureRelationshipFactory.create(project=self.prj)
        self.resources = ResourceFactory.create_batch(
            2, project=self.prj, content_object=self.tenure)
        ResourceFactory.create(project=self.prj)

        self.file = self.get_file('/resources/tests/files/image.jpg', 'rb')
        self.file_name = self.storage.save('resources/image.jpg', self.file)
Пример #13
0
 def test_get_carousel_resources(self):
     ResourceFactory.create(
         title='first', likes=UserFactory.create_batch(size=10), status=RESOURCE_APPROVED
     )
     second = ResourceFactory.create(
         title='second',
         tried=UserFactory.create_batch(size=5),
         hits=20,
         status=RESOURCE_APPROVED,
     )
     resources = Resource.get_carousel_resources(limit=1)
     self.assertIn(second, resources)
Пример #14
0
 def test_get_related_resources(self):
     resource = ResourceFactory.create(
         status=RESOURCE_APPROVED, tags=TagFactory.create_batch(10)
     )
     tag = resource.tags.all()[0]
     tag_1 = resource.tags.all()[1]
     tag_2 = resource.tags.all()[2]
     resource_2 = ResourceFactory.create(status=RESOURCE_APPROVED, tags=[tag])
     resource_3 = ResourceFactory.create(status=RESOURCE_APPROVED, tags=[tag_1, tag_2])
     resource_4 = ResourceFactory.create(status=RESOURCE_APPROVED, tags=[tag, tag_1, tag_2])
     resources = resource.get_related()
     self.assertEqual(resource_4.id, resources[0].id)
     self.assertEqual(resource_3.id, resources[1].id)
     self.assertEqual(resource_2.id, resources[2].id)
Пример #15
0
    def setUp(self):
        self.resource_admin = ResourceAdmin(Resource, AdminSite())
        self.user_admin = UserAdmin(User, AdminSite())

        self.organisation = OrganisationFactory.create()
        self.resource = ResourceFactory.create(organisation=self.organisation)
        self.resource_1 = ResourceFactory.create(
            organisation=self.organisation)
        self.resource_2 = ResourceFactory.create()

        self.user_without_organisation = UserFactory.create(is_staff=True)

        self.user_with_organisation = UserFactory.create(is_staff=True)
        self.user_with_organisation.approved_organisations.add(
            self.organisation)
Пример #16
0
    def test_get_most_published_this_week(self):
        self.organisation_2 = OrganisationFactory.create()
        ResourceFactory.create_batch(
            size=30,
            organisation=self.organisation,
            status=RESOURCE_APPROVED,
        )
        ResourceFactory.create_batch(
            size=40,
            organisation=self.organisation_2,
            status=RESOURCE_APPROVED,
        )
        organisation = Organisation.get_most_published_this_week()

        self.assertEqual(self.organisation_2.id, organisation.id)
Пример #17
0
 def setUp(self):
     self.resource_most_tried = ResourceFactory.create(
         tried=[UserFactory.create(),
                UserFactory.create()],
         status=RESOURCE_APPROVED,
     )
     self.resource_most_liked = ResourceFactory.create(
         likes=[
             UserFactory.create(),
             UserFactory.create(),
             UserFactory.create()
         ],
         status=RESOURCE_APPROVED,
     )
     self.user = UserFactory.create()
    def test_ordering(self):
        party = PartyFactory.create(project=self.prj)
        ResourceFactory.create_from_kwargs([
            {'content_object': party, 'project': self.prj, 'name': 'A'},
            {'content_object': party, 'project': self.prj, 'name': 'B'},
            {'content_object': party, 'project': self.prj, 'name': 'C'},
        ])

        response = self.request(
            user=self.user,
            url_kwargs={'party': party.id},
            get_data={'ordering': 'name'})
        assert response.status_code == 200
        assert len(response.content) == 3
        names = [resource['name'] for resource in response.content]
        assert(names == sorted(names))
 def setup_models(self):
     self.prj = ProjectFactory.create()
     self.tenure = TenureRelationshipFactory.create(project=self.prj)
     self.resource = ResourceFactory.create(content_object=self.tenure,
                                            project=self.prj)
     self.user = UserFactory.create()
     assign_policies(self.user)
Пример #20
0
 def setup_models(self):
     self.prj = ProjectFactory.create()
     self.party = PartyFactory.create(project=self.prj)
     self.resource = ResourceFactory.create(content_object=self.party,
                                            project=self.prj)
     self.user = UserFactory.create()
     assign_policies(self.user)
Пример #21
0
    def test_reverse_ordering(self):
        party = PartyFactory.create(project=self.prj)
        ResourceFactory.create_from_kwargs([
            {'content_object': party, 'project': self.prj, 'name': 'A'},
            {'content_object': party, 'project': self.prj, 'name': 'B'},
            {'content_object': party, 'project': self.prj, 'name': 'C'},
        ])

        response = self.request(
            user=self.user,
            url_kwargs={'party': party.id},
            get_data={'ordering': '-name'})
        assert response.status_code == 200
        assert len(response.content) == 3
        names = [resource['name'] for resource in response.content]
        assert(names == sorted(names, reverse=True))
 def test_add_existing_resource(self):
     new_resource = ResourceFactory.create()
     data = {'id': new_resource.id}
     response = self.request(method='POST', user=self.user, post_data=data)
     assert response.status_code == 201
     assert self.su.resources.count() == 3
     assert new_resource in self.su.resources
Пример #23
0
 def test_add_existing_resource(self):
     new_resource = ResourceFactory.create()
     data = {'id': new_resource.id}
     response = self.request(method='POST', user=self.user, post_data=data)
     assert response.status_code == 201
     assert self.su.resources.count() == 3
     assert new_resource in self.su.resources
Пример #24
0
 def setup_models(self):
     self.project = ProjectFactory.create(slug='test-project')
     self.su = SpatialUnitFactory.create(project=self.project)
     self.party = PartyFactory.create(project=self.project)
     self.tenure_rel = TenureRelationshipFactory.create(
         spatial_unit=self.su, party=self.party, project=self.project)
     self.resource = ResourceFactory.create(project=self.project)
    def test_reverse_ordering(self):
        su = SpatialUnitFactory.create(project=self.prj)
        ResourceFactory.create_from_kwargs([
            {'content_object': su, 'project': self.prj, 'name': 'A'},
            {'content_object': su, 'project': self.prj, 'name': 'B'},
            {'content_object': su, 'project': self.prj, 'name': 'C'},
        ])

        response = self.request(
            user=self.user,
            url_kwargs={'location': su.id},
            get_data={'ordering': '-name'})
        assert response.status_code == 200
        assert len(response.content) == 3
        names = [resource['name'] for resource in response.content]
        assert(names == sorted(names, reverse=True))
Пример #26
0
    def setup_models(self):
        self.user = UserFactory.create()
        assign_policies(self.user)
        self.project = ProjectFactory.create(slug='test-project')

        QuestionnaireFactory.create(project=self.project)
        content_type = ContentType.objects.get(app_label='party',
                                               model='party')
        create_attrs_schema(project=self.project,
                            dict=individual_party_xform_group,
                            content_type=content_type,
                            errors=[])
        create_attrs_schema(project=self.project,
                            dict=default_party_xform_group,
                            content_type=content_type,
                            errors=[])

        content_type = ContentType.objects.get(app_label='party',
                                               model='tenurerelationship')
        create_attrs_schema(project=self.project,
                            dict=tenure_relationship_xform_group,
                            content_type=content_type,
                            errors=[])

        self.su = SpatialUnitFactory.create(project=self.project, type='CB')
        self.party = PartyFactory.create(project=self.project,
                                         type='IN',
                                         attributes={
                                             'gender': 'm',
                                             'homeowner': 'yes',
                                             'dob': '1951-05-05'
                                         })
        self.tenure_rel = TenureRelationshipFactory.create(
            spatial_unit=self.su,
            party=self.party,
            project=self.project,
            attributes={'notes': 'PBS is the best.'})
        self.resource = ResourceFactory.create(project=self.project)

        self.results = get_fake_es_api_results(self.project, self.su,
                                               self.party, self.tenure_rel,
                                               self.resource)
        self.proj_result = self.results['hits']['hits'][0]
        self.su_result = self.results['hits']['hits'][1]
        self.party_result = self.results['hits']['hits'][2]
        self.tenure_rel_result = self.results['hits']['hits'][3]
        self.resource_result = self.results['hits']['hits'][4]

        self.query = 'searching'
        self.query_body = {
            'query': {
                'simple_query_string': {
                    'default_operator': 'and',
                    'query': self.query,
                }
            }
        }
        self.es_endpoint = '{}/project-{}/_search'.format(
            api_url, self.project.id)
        self.es_body = json.dumps(self.query_body, sort_keys=True)
Пример #27
0
 def setup_models(self):
     self.prj = ProjectFactory.create()
     self.su = SpatialUnitFactory.create(project=self.prj)
     self.resource = ResourceFactory.create(content_object=self.su,
                                            project=self.prj)
     self.user = UserFactory.create()
     assign_policies(self.user)
 def setup_models(self):
     self.prj = ProjectFactory.create()
     self.su = SpatialUnitFactory.create(project=self.prj)
     self.resource = ResourceFactory.create(content_object=self.su,
                                            project=self.prj)
     self.user = UserFactory.create()
     assign_policies(self.user)
Пример #29
0
 def test_user_resources(self):
     ResourceFactory.create(
         privacy=[self.organisation, self.organisation_1],
         status=RESOURCE_APPROVED)
     ResourceFactory.create(privacy=[self.organisation_1],
                            status=RESOURCE_APPROVED)
     ResourceFactory.create_batch(size=2, status=RESOURCE_APPROVED)
     ResourceFactory.create(status=RESOURCE_REJECTED)
     self.assertEqual(Resource.objects.approved(self.user).count(), 3)
 def setup_models(self):
     self.prj = ProjectFactory.create()
     self.tenure = TenureRelationshipFactory.create(
         project=self.prj)
     self.resource = ResourceFactory.create(content_object=self.tenure,
                                            project=self.prj)
     self.user = UserFactory.create()
     assign_policies(self.user)
Пример #31
0
    def setup_models(self):
        self.user = UserFactory.create()
        assign_policies(self.user)
        self.project = ProjectFactory.create(slug='test-project')

        QuestionnaireFactory.create(project=self.project)
        content_type = ContentType.objects.get(
            app_label='party', model='party')
        create_attrs_schema(
            project=self.project, dict=individual_party_xform_group,
            content_type=content_type, errors=[])
        create_attrs_schema(
            project=self.project, dict=default_party_xform_group,
            content_type=content_type, errors=[])

        content_type = ContentType.objects.get(
            app_label='party', model='tenurerelationship')
        create_attrs_schema(
            project=self.project, dict=tenure_relationship_xform_group,
            content_type=content_type, errors=[])

        self.su = SpatialUnitFactory.create(project=self.project, type='CB')
        self.party = PartyFactory.create(
            project=self.project,
            type='IN',
            attributes={
                'gender': 'm',
                'homeowner': 'yes',
                'dob': '1951-05-05'
            })
        self.tenure_rel = TenureRelationshipFactory.create(
            spatial_unit=self.su, party=self.party, project=self.project,
            attributes={'notes': 'PBS is the best.'})
        self.resource = ResourceFactory.create(project=self.project)

        self.results = get_fake_es_api_results(
            self.project, self.su, self.party, self.tenure_rel, self.resource)
        self.proj_result = self.results['hits']['hits'][0]
        self.su_result = self.results['hits']['hits'][1]
        self.party_result = self.results['hits']['hits'][2]
        self.tenure_rel_result = self.results['hits']['hits'][3]
        self.resource_result = self.results['hits']['hits'][4]

        self.query = 'searching'
        self.query_body = {
            'query': {
                'simple_query_string': {
                    'default_operator': 'and',
                    'query': self.query,
                }
            },
            'from': 10,
            'size': 20,
            'sort': {'_score': {'order': 'desc'}},
        }
        self.es_endpoint = '{}/project-{}/_search/'.format(api_url,
                                                           self.project.id)
        self.es_body = json.dumps(self.query_body, sort_keys=True)
Пример #32
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ResourceExporter(project)

        ResourceFactory.create(project=project, original_file='res.png')
        ResourceFactory.create(project=project, original_file='res.png')
        ResourceFactory.create(project=project, original_file='resources.xlsx')
        deleted = ResourceFactory.create(project=project,
                                         original_file='image1.jpg',
                                         archived=True)

        t = round(time.time() * 1000)
        path, mime = exporter.make_download('res-test-' + str(t))
        assert path == os.path.join(settings.MEDIA_ROOT,
                                    'temp/res-test-{}.zip'.format(t))
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 4
            assert 'res.png' in testzip.namelist()
            assert 'res_1.png' in testzip.namelist()
            assert 'resources_1.xlsx' in testzip.namelist()
            assert 'resources.xlsx' in testzip.namelist()
            assert deleted.original_file not in testzip.namelist()
 def test_delete_resource_with_invalid_resource(self):
     new_resource = ResourceFactory.create(project=self.prj)
     response = self.request(method='DELETE', user=self.user, url_kwargs={
         'resource': new_resource.id
         })
     assert response.status_code == 404
     assert response.content['detail'] == "Not found."
     assert len(self.su.resources) == 1
     assert Resource.objects.filter(
         id=self.resource.id, project=self.prj).exists()
Пример #34
0
 def test_delete_resource_with_invalid_resource(self):
     new_resource = ResourceFactory.create(project=self.prj)
     response = self.request(method='DELETE',
                             user=self.user,
                             url_kwargs={'resource': new_resource.id})
     assert response.status_code == 404
     assert response.content['detail'] == "Not found."
     assert len(self.su.resources) == 1
     assert Resource.objects.filter(id=self.resource.id,
                                    project=self.prj).exists()
    def test_search_filter(self):
        not_found = self.storage.save('resources/bild.jpg', self.file)
        su = SpatialUnitFactory.create(project=self.prj)
        ResourceFactory.create_from_kwargs([
            {'content_object': su, 'project': self.prj,
                'file': self.file_name},
            {'content_object': su, 'project': self.prj,
                'file': self.file_name},
            {'content_object': su, 'project': self.prj,
                'file': not_found}
        ])

        response = self.request(
            user=self.user,
            url_kwargs={'organization': self.prj.organization.slug,
                        'project': self.prj.slug,
                        'location': su.id},
            get_data={'search': 'image'})
        assert response.status_code == 200
        assert len(response.content) == 2
Пример #36
0
 def setUp(self):
     super().setUp()
     PolicyFactory.load_policies()
     test_objs = load_test_data(get_test_data())
     self.org = test_objs['organizations'][0]
     self.prj = test_objs['projects'][1]
     OrganizationRole.objects.create(organization=self.org,
                                     user=UserFactory.create(
                                         username='******',
                                         password='******'),
                                     admin=True)
     ResourceFactory.create_batch(2,
                                  content_object=self.prj,
                                  project=self.prj)
     su = SpatialUnitFactory(geometry=GEOSGeometry(
         '{"type": "Polygon",'
         '"coordinates": [['
         '[-5.1031494140625000,'
         ' 8.1299292850467957], '
         '[-5.0482177734375000, '
         '7.6837733211111425], '
         '[-4.6746826171875000, '
         '7.8252894725496338], '
         '[-4.8641967773437491, '
         '8.2278005261522775], '
         '[-5.1031494140625000, '
         '8.1299292850467957]]]}'),
                             project=self.prj,
                             type='MI',
                             attributes={})
     ResourceFactory.create(content_object=su, project=self.prj)
     party = PartyFactory.create(project=test_objs['projects'][1])
     tenure = TenureRelationshipFactory.create(
         project=self.prj,
         party=party,
         spatial_unit=su,
         tenure_type=TenureRelationshipType.objects.create(
             id='CR', label='Customary Rights'))
     ResourceFactory.create(content_object=su, project=self.prj)
     ResourceFactory.create(content_object=party, project=self.prj)
     ResourceFactory.create(content_object=tenure, project=self.prj)
Пример #37
0
    def test_detach_party_resources(self):
        project = ProjectFactory.create()
        party = PartyFactory.create(project=project)
        resource = ResourceFactory.create(project=project)
        resource.content_objects.create(content_object=party)

        assert ContentObject.objects.filter(object_id=party.id,
                                            resource=resource).exists()
        assert resource in party.resources

        party.delete()
        assert not ContentObject.objects.filter(object_id=party.id,
                                                resource=resource).exists()
Пример #38
0
    def test_detach_spatial_unit_resources(self):
        project = ProjectFactory.create()
        su = SpatialUnitFactory.create(project=project)
        resource = ResourceFactory.create(project=project)
        resource.content_objects.create(
          content_object=su)
        assert ContentObject.objects.filter(
            object_id=su.id,
            resource=resource,).exists()
        assert resource in su.resources

        su.delete()
        assert not ContentObject.objects.filter(
            object_id=su.id, resource=resource).exists()
Пример #39
0
 def setup_models(self):
     self.project = ProjectFactory.create(slug='test-project')
     self.location = SpatialUnitFactory.create(
         project=self.project,
         geometry='SRID=4326;POINT(0 0)',
     )
     self.party1 = PartyFactory.create(project=self.project)
     self.party2 = PartyFactory.create(project=self.project)
     self.tenure_rel = TenureRelationshipFactory.create(
         project=self.project,
         spatial_unit=self.location,
         party=self.party1,
     )
     self.resource = ResourceFactory.create(project=self.project)
Пример #40
0
    def test_detach_tenure_relationship_resources(self):
        project = ProjectFactory.create()
        tenure = TenureRelationshipFactory.create(project=project)
        resource = ResourceFactory.create(project=project)
        resource.content_objects.create(
            content_object=tenure)
        assert ContentObject.objects.filter(
            object_id=tenure.id,
            resource=resource,).exists()
        assert resource in tenure.resources

        tenure.delete()
        assert not ContentObject.objects.filter(
            object_id=tenure.id, resource=resource).exists()
Пример #41
0
    def test_detach_spatial_unit_resources(self):
        project = ProjectFactory.create()
        su = SpatialUnitFactory.create(project=project)
        resource = ResourceFactory.create(project=project)
        resource.content_objects.create(content_object=su)
        assert ContentObject.objects.filter(
            object_id=su.id,
            resource=resource,
        ).exists()
        assert resource in su.resources

        su.delete()
        assert not ContentObject.objects.filter(object_id=su.id,
                                                resource=resource).exists()
Пример #42
0
    def test_detach_tenure_relationship_resources(self):
        project = ProjectFactory.create()
        tenure = TenureRelationshipFactory.create(project=project)
        resource = ResourceFactory.create(project=project)
        resource.content_objects.create(content_object=tenure)
        assert ContentObject.objects.filter(
            object_id=tenure.id,
            resource=resource,
        ).exists()
        assert resource in tenure.resources

        tenure.delete()
        assert not ContentObject.objects.filter(object_id=tenure.id,
                                                resource=resource).exists()
Пример #43
0
    def test_detach_party_resources(self):
        project = ProjectFactory.create()
        party = PartyFactory.create(project=project)
        resource = ResourceFactory.create(project=project)
        resource.content_objects.create(
            content_object=party)

        assert ContentObject.objects.filter(
            object_id=party.id, resource=resource).exists()
        assert resource in party.resources

        party.delete()
        assert not ContentObject.objects.filter(
            object_id=party.id, resource=resource).exists()
Пример #44
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ResourceExporter(project)
        res = ResourceFactory.create(project=project)

        t = round(time.time() * 1000)
        path, mime = exporter.make_download("res-test-" + str(t))
        assert path == os.path.join(settings.MEDIA_ROOT, "temp/res-test-{}.zip".format(t))
        assert mime == "application/zip"

        with ZipFile(path, "r") as testzip:
            assert len(testzip.namelist()) == 2
            assert res.original_file in testzip.namelist()
            assert "resources.xlsx" in testzip.namelist()
Пример #45
0
    def test_make_download(self):
        ensure_dirs()
        project = ProjectFactory.create()
        exporter = ResourceExporter(project)
        res = ResourceFactory.create(project=project)

        t = round(time.time() * 1000)
        path, mime = exporter.make_download('res-test-' + str(t))
        assert path == os.path.join(settings.MEDIA_ROOT,
                                    'temp/res-test-{}.zip'.format(t))
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 2
            assert res.original_file in testzip.namelist()
            assert 'resources.xlsx' in testzip.namelist()
Пример #46
0
    def test_detach_deferred_spatial_unit_resources(self):
        project = ProjectFactory.create()
        su = SpatialUnitFactory.create(project=project)
        resource = ResourceFactory.create(project=project)
        resource.content_objects.create(
          content_object=su)
        assert ContentObject.objects.filter(
            object_id=su.id,
            resource=resource,).exists()

        su_deferred = SpatialUnit.objects.all().defer('attributes')[0]
        assert resource in su_deferred.resources

        su_deferred.delete()
        assert not ContentObject.objects.filter(
            object_id=su.id, resource=resource).exists()
        assert SpatialUnit.objects.all().count() == 0
Пример #47
0
    def test_detach_deferred_party_resources(self):
        project = ProjectFactory.create()
        party = PartyFactory.create(project=project)
        resource = ResourceFactory.create(project=project)
        resource.content_objects.create(
            content_object=party)

        assert ContentObject.objects.filter(
            object_id=party.id, resource=resource).exists()

        party_deferred = Party.objects.all().defer('attributes')[0]
        assert resource in party_deferred.resources

        party_deferred.delete()
        assert not ContentObject.objects.filter(
            object_id=party.id, resource=resource).exists()
        assert Party.objects.all().count() == 0
Пример #48
0
    def test_detach_deferred_tenure_relationship_resources(self):
        project = ProjectFactory.create()
        tenure = TenureRelationshipFactory.create(project=project)
        resource = ResourceFactory.create(project=project)
        resource.content_objects.create(
            content_object=tenure)
        assert ContentObject.objects.filter(
            object_id=tenure.id,
            resource=resource,).exists()
        tenure_deferred = TenureRelationship.objects.all(
                                                    ).defer('attributes')[0]

        assert resource in tenure_deferred.resources

        tenure_deferred.delete()
        assert not ContentObject.objects.filter(
            object_id=tenure.id, resource=resource).exists()
        assert TenureRelationship.objects.all().count() == 0
Пример #49
0
    def test_get_all_download(self):
        ensure_dirs()
        data = {"type": "all"}
        user = UserFactory.create()
        project = ProjectFactory.create()
        geometry = "SRID=4326;POINT (30 10)"
        SpatialUnitFactory.create(project=project, geometry=geometry)
        res = ResourceFactory.create(project=project)

        form = forms.DownloadForm(project, user, data=data)
        assert form.is_valid() is True
        path, mime = form.get_file()
        assert "{}-{}".format(project.id, user.id) in path
        assert mime == "application/zip"

        with ZipFile(path, "r") as testzip:
            assert len(testzip.namelist()) == 4
            assert res.original_file in testzip.namelist()
            assert "resources.xlsx" in testzip.namelist()
            assert "data.xlsx" in testzip.namelist()
            assert "data-shp.zip" in testzip.namelist()
Пример #50
0
    def test_get_all_download(self):
        ensure_dirs()
        data = {'type': 'all'}
        user = UserFactory.create()
        project = ProjectFactory.create()
        geometry = 'SRID=4326;POINT (30 10)'
        SpatialUnitFactory.create(project=project, geometry=geometry)
        res = ResourceFactory.create(project=project)

        form = forms.DownloadForm(project, user, data=data)
        assert form.is_valid() is True
        path, mime = form.get_file()
        assert '{}-{}'.format(project.id, user.id) in path
        assert mime == 'application/zip'

        with ZipFile(path, 'r') as testzip:
            assert len(testzip.namelist()) == 4
            assert res.original_file in testzip.namelist()
            assert 'resources.xlsx' in testzip.namelist()
            assert 'data.xlsx' in testzip.namelist()
            assert 'data-shp.zip' in testzip.namelist()
Пример #51
0
    def test_pack_resource_data(self):
        project = ProjectFactory.create()
        exporter = ResourceExporter(project)

        res = ResourceFactory.create(project=project)
        loc = SpatialUnitFactory.create(project=project)
        loc2 = SpatialUnitFactory.create(project=project)
        par = PartyFactory.create(project=project)
        rel = TenureRelationshipFactory.create(project=project)

        ContentObject.objects.create(resource=res, content_object=loc)
        ContentObject.objects.create(resource=res, content_object=loc2)
        ContentObject.objects.create(resource=res, content_object=par)
        ContentObject.objects.create(resource=res, content_object=rel)

        packed = exporter.pack_resource_data(res)
        assert packed[0] == res.id
        assert packed[1] == res.name
        assert packed[2] == res.description
        assert packed[3] == res.original_file
        assert loc.id in packed[4]
        assert loc2.id in packed[4]
        assert par.id in packed[5]
        assert rel.id in packed[6]
Пример #52
0
 def setup_models(self):
     self.project = ProjectFactory.create()
     self.party = PartyFactory.create(project=self.project)
     self.attached = ResourceFactory.create(project=self.project,
                                            content_object=self.party)
     self.unattached = ResourceFactory.create(project=self.project)
 def setup_models(self):
     self.project = ProjectFactory.create()
     self.location = SpatialUnitFactory.create(project=self.project)
     self.attached = ResourceFactory.create(project=self.project,
                                            content_object=self.location)
     self.unattached = ResourceFactory.create(project=self.project)