-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #857: Imported resources saved to wrong location
Fix #834: Limit CSV file size
- Loading branch information
bohare
committed
Oct 28, 2016
1 parent
22b4e7d
commit b8bdb96
Showing
6 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
cadasta/resources/migrations/0005_fix_import_resource_urls.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from __future__ import unicode_literals | ||
|
||
import os | ||
|
||
from django.db import migrations | ||
|
||
|
||
def fix_imported_resource_urls(apps, schema_editor): | ||
Resource = apps.get_model("resources", "Resource") | ||
resources = Resource.objects.filter(mime_type='text/csv') | ||
for resource in resources: | ||
url = resource.file.url | ||
if url.endswith('.csv'): | ||
parts = list(os.path.split(url)) | ||
if 'resources' not in parts: | ||
parts.insert(-1, 'resources') | ||
url = '/'.join(parts) | ||
resource.file = url | ||
resource.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('resources', '0004_add_ordering_for_resources'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython( | ||
fix_imported_resource_urls, migrations.RunPython.noop), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from core.tests.utils.cases import UserTestCase | ||
from core.util import random_id | ||
from django.apps import apps | ||
from django.core.management import call_command | ||
from django.db import connection | ||
from django.db.migrations.loader import MigrationLoader | ||
from django.test import TestCase | ||
|
||
|
||
class MigrationTestCase(UserTestCase, TestCase): | ||
|
||
@property | ||
def app(self): | ||
return apps.get_containing_app_config(type(self).__module__).name | ||
|
||
migrate_from = None | ||
migrate_to = None | ||
|
||
def setUp(self): | ||
super(UserTestCase, self).setUp() | ||
|
||
assert self.migrate_from and self.migrate_to | ||
"TestCase '{}' must define migrate_from and migrate_to " | ||
"properties".format(type(self).__name__) | ||
|
||
# get the application state pre-migration | ||
apps_before = self._get_apps_for_migration( | ||
[(self.app, self.migrate_from)]) | ||
|
||
# Reverse to migrate_from | ||
call_command('migrate', self.app, self.migrate_from) | ||
|
||
# setup pre-migration test data | ||
self.setUpBeforeMigration(apps_before) | ||
|
||
# Run the migration to test | ||
call_command('migrate', self.app, self.migrate_to) | ||
|
||
# get application state post-migration | ||
self.apps_after = self._get_apps_for_migration( | ||
[(self.app, self.migrate_to)]) | ||
|
||
def setUpBeforeMigration(self, apps): | ||
pass | ||
|
||
def _get_apps_for_migration(self, migration_states): | ||
loader = MigrationLoader(connection) | ||
full_names = [] | ||
for app_name, migration_name in migration_states: | ||
if migration_name != 'zero': | ||
migration_name = loader.get_migration_by_prefix( | ||
app_name, migration_name).name | ||
full_names.append((app_name, migration_name)) | ||
state = loader.project_state(full_names) | ||
return state.apps | ||
|
||
|
||
class TestFixImportedResourceUrls(MigrationTestCase): | ||
|
||
migrate_from = '0004_add_ordering_for_resources' | ||
migrate_to = '0005_fix_import_resource_urls' | ||
|
||
def setUpBeforeMigration(self, apps_before): | ||
Resource = apps_before.get_model('resources', 'Resource') | ||
User = apps_before.get_model('accounts', 'User') | ||
Organization = apps_before.get_model('organization', 'Organization') | ||
Project = apps_before.get_model('organization', 'Project') | ||
|
||
user = User.objects.create(username='testuser') | ||
org = Organization.objects.create(name='Test Org') | ||
project = Project.objects.create(name='Test Proj', organization=org) | ||
|
||
base_path = ( | ||
'https://s3-us-west-2.amazonaws.com/cadasta-platformprod-bucket/' | ||
) | ||
|
||
# cannot call custom save methods on models in migrations | ||
# as models are serialized from migration scripts | ||
# so custom methods are not available | ||
# docs.djangoproject.com/en/1.9/topics/migrations/#historical-models | ||
for f in range(10): | ||
file_name = base_path + 'test_' + str(f) + '.csv' | ||
resource_name = 'test-resource-' + str(f) | ||
Resource.objects.create( | ||
id=random_id(), name=resource_name, file=file_name, | ||
mime_type='text/csv', contributor=user, project=project | ||
) | ||
Resource.objects.create( | ||
id=random_id(), file=base_path + 'test.jpg', mime_type='image/jpg', | ||
contributor=user, project=project | ||
) | ||
|
||
def test_migration(self): | ||
Resource = self.apps_after.get_model('resources', 'Resource') | ||
resources = Resource.objects.filter(mime_type='text/csv') | ||
assert len(resources) == 10 | ||
base_path = ( | ||
'https://s3-us-west-2.amazonaws.com/cadasta-platformprod-bucket/' | ||
) | ||
resource = Resource.objects.get(name='test-resource-0') | ||
assert resource.file.url == base_path + 'resources/test_0.csv' |