diff --git a/cadasta/organization/forms.py b/cadasta/organization/forms.py index d034b4041..cbdb8bf99 100644 --- a/cadasta/organization/forms.py +++ b/cadasta/organization/forms.py @@ -515,13 +515,22 @@ def clean_file(self): if file.size > self.MAX_FILE_SIZE: raise ValidationError( _('File too large, max size 512kb')) - mime = magic.Magic(mime=True) - 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 + def clean_mime_type(self): + file = self.cleaned_data.get("file", False) + if file: + mime = magic.Magic(mime=True) + mime_type = str(mime.from_buffer(file.read(1024)), 'utf-8') + if mime_type not in self.VALID_IMPORT_MIME_TYPES: + self.add_error('file', _("Invalid file type")) + return mime_type + + def clean_original_file(self): + file = self.cleaned_data.get("file", False) + if file: + return file.name + class MapAttributesForm(forms.Form): attributes = forms.MultipleChoiceField(required=False) diff --git a/cadasta/organization/tests/test_forms.py b/cadasta/organization/tests/test_forms.py index 45df844d3..623deda38 100644 --- a/cadasta/organization/tests/test_forms.py +++ b/cadasta/organization/tests/test_forms.py @@ -1056,3 +1056,4 @@ def test_set_mime_type(self): project=self.project, user=self.user) assert form.is_valid() is True assert form.cleaned_data['mime_type'] == 'text/plain' + assert form.cleaned_data['original_file'] == 'test.csv' diff --git a/cadasta/organization/tests/test_views_default_projects.py b/cadasta/organization/tests/test_views_default_projects.py index f10180002..ee4bda46a 100644 --- a/cadasta/organization/tests/test_views_default_projects.py +++ b/cadasta/organization/tests/test_views_default_projects.py @@ -1507,6 +1507,8 @@ def test_full_flow_valid(self): resource = Resource.objects.filter(project_id=proj.pk).first() assert resource.file.url == '/media/s3/uploads/resources/test.csv' + assert resource.mime_type == 'text/plain' + assert resource.original_file == 'test.csv' def test_full_flow_invalid_value(self): self.client.force_login(self.user) diff --git a/cadasta/organization/views/default.py b/cadasta/organization/views/default.py index c5fdbb680..9ab64ea10 100644 --- a/cadasta/organization/views/default.py +++ b/cadasta/organization/views/default.py @@ -812,7 +812,6 @@ def render_next_step(self, form, **kwargs): def done(self, form_list, **kwargs): form_data = [form.cleaned_data for form in form_list] - name = form_data[0]['name'] description = form_data[0]['description'] mime_type = form_data[0]['mime_type'] diff --git a/cadasta/templates/organization/project_select_import.html b/cadasta/templates/organization/project_select_import.html index 9aeaf36fc..9eaa9da9f 100644 --- a/cadasta/templates/organization/project_select_import.html +++ b/cadasta/templates/organization/project_select_import.html @@ -1,9 +1,9 @@ -{% extends "organization/project_import_wrapper.html" %} +{% extends "organization/project_import_wrapper.html" %} {% load i18n %} {% load widget_tweaks %} -{% block extra_script %} {{ form.media }} {% endblock %} +{% block extra_script %} {{ form.media }} {% endblock %} {% block step_content %} @@ -62,7 +62,6 @@