Skip to content

Commit

Permalink
Adds mime_type field to resource serializer (#1363)
Browse files Browse the repository at this point in the history
* Adds mime_type field to resource serializer

* Add tests
  • Loading branch information
oliverroick authored and amplifi committed Apr 10, 2017
1 parent e527879 commit 21e15a1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cadasta/resources/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions cadasta/resources/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 36 additions & 3 deletions cadasta/resources/tests/test_serializers.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,27 +28,58 @@ 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()
data = {
'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()
Expand Down

0 comments on commit 21e15a1

Please sign in to comment.