Skip to content

Commit

Permalink
Fixes: #884
Browse files Browse the repository at this point in the history
  • Loading branch information
bohare committed Nov 4, 2016
1 parent 1a8e6e6 commit 22fd4ca
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
19 changes: 14 additions & 5 deletions cadasta/organization/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cadasta/organization/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
2 changes: 2 additions & 0 deletions cadasta/organization/tests/test_views_default_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion cadasta/organization/views/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
6 changes: 2 additions & 4 deletions cadasta/templates/organization/project_select_import.html
Original file line number Diff line number Diff line change
@@ -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 %}

Expand Down Expand Up @@ -62,7 +62,6 @@ <h3>{% trans "Import file" %}</h3>
{% trans "Include import file in project resources" %}
</label>
</div>
{{ form.mime_type }}
</div>
</div>
</div>
Expand All @@ -79,4 +78,3 @@ <h3>{% trans "Import file" %}</h3>
{% endif %}

{% endblock %}

0 comments on commit 22fd4ca

Please sign in to comment.