Skip to content

Commit

Permalink
Fix: #799 (#800)
Browse files Browse the repository at this point in the history
* missing resource icon for csv files
* other minor fixes
  • Loading branch information
bjohare authored and ian-ross committed Oct 6, 2016
1 parent 0459973 commit 84a895a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions cadasta/organization/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ def clean_file(self):
mime_type = str(mime.from_buffer(file.read(1024)), 'utf-8')
if mime_type not in self.VALID_IMPORT_MIME_TYPES:
raise ValidationError(_("Invalid file type"))
self.data['mime_type'] = mime_type
return file


Expand Down
2 changes: 1 addition & 1 deletion cadasta/organization/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class TutelaryMeta:
'error_message': messages.PROJ_DOWNLOAD}),
('project.import',
{'description': _("Import data to a project"),
'error_message': messages.PROJ_DOWNLOAD}),
'error_message': messages.PROJ_IMPORT}),
)

def __str__(self):
Expand Down
12 changes: 12 additions & 0 deletions cadasta/organization/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ def setUp(self):
name='Test Project', organization=self.org)
self.user = UserFactory.create()
self.invalid_file_type = '/organization/tests/files/test_invalid.kml'
self.valid_file_type = '/organization/tests/files/test.csv'
self.path = os.path.dirname(settings.BASE_DIR)
self.data = {
'name': 'Test Imports',
Expand All @@ -1039,3 +1040,14 @@ def test_invalid_file_type(self):
project=self.project, user=self.user)
assert form.is_valid() is False
assert form.errors['file'][0] == 'Invalid file type'

def test_set_mime_type(self):
valid_file = open(self.path + self.valid_file_type, 'rb').read()
file = SimpleUploadedFile(
'test.csv', valid_file, 'text/csv')
file_dict = {'file': file}
form = forms.SelectImportForm(
files=file_dict, data=self.data,
project=self.project, user=self.user)
assert form.is_valid() is True
assert form.cleaned_data['mime_type'] == 'text/plain'
9 changes: 6 additions & 3 deletions cadasta/organization/views/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class OrganizationList(PermissionRequiredMixin, generic.ListView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['any_archived'] = self.get_queryset().filter(
archived=True).exists()
archived=True).exists()
return context


Expand Down Expand Up @@ -735,6 +735,9 @@ def get_form_kwargs(self, step=None):
'project': self.get_project()
}

def get_perms_objects(self):
return [self.get_project()]

def get_template_names(self):
return [DATA_IMPORT_TEMPLATES[self.steps.current]]

Expand Down Expand Up @@ -790,8 +793,8 @@ def done(self, form_list, **kwargs):

name = form_data[0]['name']
description = form_data[0]['description']
mime_type = form_data[0]['mime_type']
is_resource = form_data[0]['is_resource']
type = form_data[0]['type']
original_file = form_data[0]['original_file']
file = form_data[0]['file']

Expand Down Expand Up @@ -820,7 +823,7 @@ def done(self, form_list, **kwargs):
url = default_storage.save(file.name, file.read())
resource = Resource(
name=name, description=description, file=url,
original_file=original_file, mime_type=type,
original_file=original_file, mime_type=mime_type,
contributor=self.request.user, project=self.get_project())
resource.save()
ContentObject.objects.create(resource=resource,
Expand Down

0 comments on commit 84a895a

Please sign in to comment.