diff --git a/cadasta/resources/serializers.py b/cadasta/resources/serializers.py index d6199b5fd..be99dd50e 100644 --- a/cadasta/resources/serializers.py +++ b/cadasta/resources/serializers.py @@ -12,8 +12,9 @@ class ResourceSerializer(serializers.ModelSerializer): class Meta: model = Resource fields = ('id', 'name', 'description', 'file', 'original_file', - 'archived',) + 'archived', 'mime_type', ) read_only_fields = ('id', ) + extra_kwargs = {'mime_type': {'required': False}} def is_valid(self, raise_exception=False): data = self.initial_data diff --git a/cadasta/resources/tests/test_models.py b/cadasta/resources/tests/test_models.py index 8ee9b1643..4bcba874f 100644 --- a/cadasta/resources/tests/test_models.py +++ b/cadasta/resources/tests/test_models.py @@ -201,6 +201,18 @@ def test_create_thumbnail(self): settings.MEDIA_ROOT, 's3/uploads/resources/thumb_test-128x128.jpg') ) + def test_create_no_thumbnail_non_images(self): + file = self.get_file('/resources/tests/files/text.txt', 'rb') + file_name = self.storage.save('resources/text.txt', file) + contributor = UserFactory.create() + resource = ResourceFactory.create(file=file_name, + mime_type='text/plain', + contributor=contributor) + resource.save() + assert os.path.isfile(os.path.join( + settings.MEDIA_ROOT, 's3/uploads/resources/text-128x128.txt') + ) is False + def test_create_spatial_resource(self): file = self.get_file('/resources/tests/files/deramola.xml', 'rb') file_name = self.storage.save('resources/deramola.xml', file) diff --git a/cadasta/resources/tests/test_serializers.py b/cadasta/resources/tests/test_serializers.py index 7f1e07d17..16eb6584d 100644 --- a/cadasta/resources/tests/test_serializers.py +++ b/cadasta/resources/tests/test_serializers.py @@ -1,5 +1,7 @@ import pytest +import os from django.test import TestCase +from django.conf import settings from rest_framework.serializers import ValidationError from core.tests.utils.cases import UserTestCase, FileStorageTestCase @@ -26,7 +28,7 @@ def test_serialize_fields(self): def test_create_project_resource(self): file = self.get_file('/resources/tests/files/image.jpg', 'rb') - file_name = self.storage.save('image.jpg', file) + file_name = self.storage.save('resources/image.jpg', file) project = ProjectFactory.create() user = UserFactory.create() @@ -34,19 +36,50 @@ def test_create_project_resource(self): 'name': 'New resource', 'description': '', 'file': file_name, - 'original_file': 'image.png' + 'original_file': 'image.png', + 'mime_type': 'image/jpeg' } serializer = ResourceSerializer( data=data, context={'content_object': project, 'contributor': user, 'project_id': project.id}) - serializer.is_valid() + serializer.is_valid(raise_exception=True) + serializer.save() + + assert project.resources.count() == 1 + assert project.resources.first().name == data['name'] + assert project.resources.first().contributor == user + assert os.path.isfile(os.path.join( + settings.MEDIA_ROOT, 's3/uploads/resources/image-128x128.jpg') + ) + + def test_create_project_resource_without_mime_type(self): + file = self.get_file('/resources/tests/files/text.txt', 'rb') + file_name = self.storage.save('resources/text.txt', file) + + project = ProjectFactory.create() + user = UserFactory.create() + data = { + 'name': 'New resource', + 'description': '', + 'file': file_name, + 'original_file': 'text.txt' + } + serializer = ResourceSerializer( + data=data, + context={'content_object': project, + 'contributor': user, + 'project_id': project.id}) + serializer.is_valid(raise_exception=True) serializer.save() assert project.resources.count() == 1 assert project.resources.first().name == data['name'] assert project.resources.first().contributor == user + assert os.path.isfile(os.path.join( + settings.MEDIA_ROOT, 's3/uploads/resources/text-128x128.txt') + ) is False def test_assign_existing_resource(self): project = ProjectFactory.create()