Skip to content

Commit

Permalink
Merge pull request #256 from vchendrix/post_m4_bugfixes
Browse files Browse the repository at this point in the history
Post m4 bugfixes
  • Loading branch information
vchendrix authored Feb 8, 2017
2 parents 4ba304b + 6956b30 commit 5f8f93c
Show file tree
Hide file tree
Showing 26 changed files with 176 additions and 103 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
.menv/
.vagrant/
.venv/
/archive_api/fixtures/ngt-dev-archive-api-cleaned-with-datasets.json
/archive_api/fixtures/ngt-dev-archiveapi.json
/archive_api/fixtures/ngt-dev-users.json
/static
ORNL_Archive_Backup_20160916/
__pycache__/
archive_api/fixtures/*.txt
archive_api_import.json
archive_api_import_with_datasets.json
archives
Expand All @@ -24,6 +22,7 @@ local.py
main.yml
ngee_tropics.sql
ngt-archive.log
ngt-dev*.json
ngt_archive.egg-info/
ngt_archive.log
ngt_archive/__pycache__/
Expand Down
2 changes: 1 addition & 1 deletion archive_api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Migration(migrations.Migration):
('modified_date', models.DateTimeField(auto_now=True)),
('cdiac_import', models.BooleanField(default=False)),
('archive',
archive_api.models.DatasetArchiveField(null=True, storage=archive_api.models.DatasetArchiveStorage(),
models.FileField(null=True, storage=archive_api.models.DatasetArchiveStorage(),
upload_to=archive_api.models.get_upload_path)),
],
options={
Expand Down
49 changes: 0 additions & 49 deletions archive_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,6 @@
from django.db import models
from django.db import transaction
from django.conf import settings
from rest_framework.exceptions import ValidationError


class DatasetArchiveField( models.FileField):

CONTENT_TYPES = ["application/zip", "application/x-bzip2",
"application/gzip", "application/x-lzip", "application/x-lzma",
"application/x-xz", "application/x-compress",
"application/x-compress", "application/x-7z-compressed",
"application/x-gtar", "application/x-rar-compressed"]

def __init__(self, *args, **kwargs):
"""
Override the parent constructor to set the allowed content_types

:param args:
:param kwargs:
"""

self.content_types = self.CONTENT_TYPES
super(DatasetArchiveField,self).__init__(*args,**kwargs)

def clean(self, *args, **kwargs):
"""
Override parent method to add checking for content type

Parent method converts the value's type and run validation. Validation errors
from to_python and validate are propagated. The correct value is
returned if no error is raised.
"""
data = super(DatasetArchiveField,self).clean(*args,**kwargs)

try:
content_type = mimetypes.guess_type(data.name)
if content_type[0] not in self.content_types:
raise ValidationError('Filetype {} not supported. Allowed types: {}'.format(content_type[0],
", ".join(
self.content_types)))
except AttributeError:
raise ValidationError('Filetype unknown. Allowed types: {}'.format(", ".join(self.content_types)))

def save(self, **kwargs):
"""
Override parent to call the custom clean method

:param kwargs:
:return:
"""
return super(DatasetArchiveField, self).save(**kwargs)


class DatasetArchiveStorage(FileSystemStorage):
Expand Down
File renamed without changes.
39 changes: 35 additions & 4 deletions archive_api/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest import mock
from unittest.mock import PropertyMock

import os
import shutil
from django.contrib.auth.models import User
from django.core import mail
Expand All @@ -13,7 +14,7 @@
from rest_framework import status
from rest_framework.test import APITestCase

from archive_api.models import DatasetArchiveField, DataSetDownloadLog, DataSet
from archive_api.models import DataSetDownloadLog, DataSet
from ngt_archive import settings


Expand Down Expand Up @@ -79,14 +80,11 @@ def test_options(self):
self.login_user("auser")
response = self.client.options('/api/v1/datasets/')
self.assertContains(response, "detail_routes")
self.assertContains(response, "allowed_mime_types")
self.assertContains(response, "upload")
self.assertContains(response, "submit")
self.assertContains(response, "approve")
self.assertContains(response, "unapprove")
self.assertContains(response, "unsubmit")
for mime_type in DatasetArchiveField.CONTENT_TYPES:
self.assertContains(response, mime_type)

def test_client_unnamed(self):

Expand Down Expand Up @@ -862,6 +860,39 @@ def test_issue_117(self, mock_file_size):
self.assertContains(response, "Uploaded file size is 3001.7 MB. Max upload size is 2048.0 MB",
status_code=status.HTTP_400_BAD_REQUEST)

def test_issue_253(self):
"""Error uploading files > 2.5 MB #253"""

self.login_user("auser")

# Write a 3 MB file
with open('{}/bigfile.dat'.format(dirname(__file__)), 'wb') as out:
out.seek((1024 * 1024 * 3) - 1)
out.write(b"0")

with open('{}/bigfile.dat'.format(dirname(__file__)), 'rb') as fp:
response = self.client.post('/api/v1/datasets/1/upload/', {'attachment': fp})

self.assertContains(response, '"success":true',
status_code=status.HTTP_201_CREATED)
self.assertContains(response, "uploaded",
status_code=status.HTTP_201_CREATED)

response = self.client.get( '/api/v1/datasets/1/archive/')
self.assertContains(response, '')
self.assertTrue("X-Sendfile" in response)
self.assertTrue(
response["X-Sendfile"].find("archives/0000/0000/NGT0000/0.0/NGT0000_0.0") > -1)
self.assertTrue("Content-Disposition" in response)
self.assertEqual("attachment; filename=NGT0000_0.0_Data_Set_1.dat",
response['Content-Disposition'])

try:
os.remove('{}/bigfile.dat'.format(dirname(__file__)))
except:
pass


class SiteClientTestCase(APITestCase):
fixtures = ('test_auth.json', 'test_archive_api.json',)

Expand Down
Loading

0 comments on commit 5f8f93c

Please sign in to comment.