Skip to content

Commit

Permalink
Fixes #669, #670 and #671 (#680)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjohare authored and ian-ross committed Sep 13, 2016
1 parent 57385fc commit 1b5e565
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
14 changes: 14 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "airbnb",
"globals": {
"describe": true,
"it": true,
"expect": true,
"sinon": true,
"assert": true,
"beforeEach": true,
"afterEach": true
}
}


3 changes: 3 additions & 0 deletions cadasta/config/settings/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@
MIME_LOOKUPS = {
'application/pdf': 'pdf',
'audio/mpeg3': 'mp3',
'audio/mpeg': 'mp3',
'audio/mp3': 'mp3',
'audio/x-mpeg-3': 'mp3',
'video/mpeg': 'mp3',
'video/x-mpeg': 'mp3',
Expand All @@ -272,6 +274,7 @@
'application/vnd.openxmlformats-'
'officedocument.spreadsheetml.sheet': 'xlsx',
'text/xml': 'xml',
'application/xml': 'xml',
'image/jpeg': 'jpg',
'image/png': 'png',
'image/gif': 'gif',
Expand Down
16 changes: 12 additions & 4 deletions cadasta/resources/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.contrib.gis.db.models import GeometryCollectionField
from django.contrib.gis.gdal.error import GDALException
from django.contrib.postgres.fields import JSONField
from django.db import models
from django.dispatch import receiver
Expand All @@ -25,6 +26,8 @@

content_types = models.Q(app_label='organization', model='project')

GPX_MIME_TYPES = ('application/xml', 'text/xml', 'application/gpx+xml')


@permissioned_model
class Resource(RandomIDModel):
Expand Down Expand Up @@ -145,7 +148,7 @@ def create_thumbnails(sender, instance, created, **kwargs):
@receiver(models.signals.post_save, sender=Resource)
def create_spatial_resource(sender, instance, created, **kwargs):
if created or instance._original_url != instance.file.url:
if 'xml' in instance.mime_type:
if instance.mime_type in GPX_MIME_TYPES:
io.ensure_dirs()
file_name = instance.file.url.split('/')[-1]
write_path = os.path.join(settings.MEDIA_ROOT,
Expand All @@ -157,9 +160,14 @@ def create_spatial_resource(sender, instance, created, **kwargs):
# of gpx mime type is not reliable
mime = magic.Magic(mime=True)
mime_type = str(mime.from_file(write_path), 'utf-8')
if mime_type in ('application/xml', 'text/xml'):
processor = GPXProcessor(write_path)
layers = processor.get_layers()
if mime_type in GPX_MIME_TYPES:
try:
processor = GPXProcessor(write_path)
layers = processor.get_layers()
except GDALException:
raise InvalidGPXFile(
_('Invalid GPX file')
)
for layer in layers.keys():
if len(layers[layer]) > 0:
SpatialResource.objects.create(
Expand Down
36 changes: 36 additions & 0 deletions cadasta/resources/tests/files/invalidgpx.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version='1.0'?>
<test_standard_questionnaire
id="test_standard_questionnaire" version="20160727122110">
<start>2016-07-07T16:38:20.310-04</start>
<end>2016-07-07T16:39:23.673-04</end>
<today>2016-07-07</today>
<deviceid>00:bb:3a:44:d0:fb</deviceid>
<title />
<party_type>IN</party_type>
<party_name>Bilbo Baggins</party_name>
<location_geometry>40.6890612 -73.9925067 0.0 0.0;</location_geometry>
<location_type>MI</location_type>
<location_photo>test_image_one.png</location_photo>
<party_photo>test_image_two.png</party_photo>
<tenure_type>LH</tenure_type>
<location_attributes>
<name>Middle Earth</name>
</location_attributes>
<party_attributes_default>
<notes>Party attribute default notes.</notes>
</party_attributes_default>
<party_attributes_individual>
<gender>f</gender>
<homeowner>no</homeowner>
<dob>2016-07-07</dob>
</party_attributes_individual>
<party_relationship_attributes>
<notes>Party relationship notes.</notes>
</party_relationship_attributes>
<tenure_relationship_attributes>
<notes>Tenure relationship notes.</notes>
</tenure_relationship_attributes>
<meta>
<instanceID>uuid:b3f225d3-0fac-4a0b-80c7-60e6db4cc0ad</instanceID>
</meta>
</test_standard_questionnaire>
26 changes: 22 additions & 4 deletions cadasta/resources/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import os

import pytest

from buckets.test.storage import FakeS3Storage
from core.tests.utils.cases import UserTestCase
from core.tests.utils.files import make_dirs # noqa
from django.conf import settings
from django.test import TestCase
from django.utils.translation import gettext as _

from ..exceptions import InvalidGPXFile
from ..models import (ContentObject, Resource, create_spatial_resource,
create_thumbnails)

from core.tests.utils.cases import UserTestCase
from core.tests.utils.files import make_dirs # noqa
from .factories import ResourceFactory, SpatialResourceFactory
from .utils import clear_temp # noqa

Expand All @@ -20,6 +21,7 @@
@pytest.mark.usefixtures('make_dirs')
@pytest.mark.usefixtures('clear_temp')
class ResourceTest(UserTestCase, TestCase):

def test_file_name_property(self):
resource = Resource(file='http://example.com/dir/filename.txt')
assert resource.file_name == 'filename.txt'
Expand Down Expand Up @@ -208,7 +210,23 @@ def test_invalid_gpx_mime_type(self):
)
with pytest.raises(InvalidGPXFile) as e:
create_spatial_resource(Resource, resource, True)
assert str(e) == 'Invalid GPX mime type: audio/mpeg'

assert str(e.value) == _('Invalid GPX mime type: audio/mpeg')

def test_invalid_gpx_file(self):
storage = FakeS3Storage()
file = open(path +
'/resources/tests/files/invalidgpx.xml', 'rb').read()
file_name = storage.save('resources/invalidgpx.xml', file)
resource = ResourceFactory.build(
file=file_name, mime_type='application/xml')
assert os.path.isfile(os.path.join(
settings.MEDIA_ROOT, 's3/uploads/resources/invalidgpx.xml')
)
with pytest.raises(InvalidGPXFile)as e:
create_spatial_resource(Resource, resource, True)
assert str(e.value) == _('Invalid GPX file')
assert Resource.objects.all().count() == 0


class SpatialResourceTest(UserTestCase, TestCase):
Expand Down

0 comments on commit 1b5e565

Please sign in to comment.