Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7 from MITLibraries/2_form_validation
Browse files Browse the repository at this point in the history
Validate form inputs
  • Loading branch information
JPrevost committed Oct 27, 2015
2 parents 7077c2c + b72175f commit 58cae00
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
2 changes: 2 additions & 0 deletions magma/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

import os
from flask import Flask
from magma.home import home_page


def create_app():
app = Flask(__name__, instance_relative_config=True)
app.config.update(SECRET_KEY=os.environ.get('SECRET_KEY', 'changeme'))
register_blueprints(app)
return app

Expand Down
23 changes: 18 additions & 5 deletions magma/home/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
except ImportError:
from io import StringIO

from flask import Blueprint, request, render_template, make_response
from flask import (Blueprint, flash, request, render_template, make_response,
session,)
from werkzeug import secure_filename

from magma.upload import process
Expand All @@ -21,14 +22,26 @@
@home_page.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
shp = request.files['data']
fgdc = request.files['metadata']
if not fgdc.filename:
if 'data' in request.files and request.files['data']:
shp = request.files['data']
else:
flash('The shapefile is required to proceed.', 'danger')
return render_template('index.html')

if 'metadata' in request.files and request.files['metadata']:
fgdc = request.files['metadata']
else:
fgdc = StringIO('<metadata/>')

tmp = tempfile.mkdtemp()
filename = os.path.join(tmp, secure_filename(shp.filename))
shp.save(filename)
archive = ZipFile(filename, 'r')
try:
archive = ZipFile(filename, 'r')
except:
flash('File is not a zip file.', 'danger')
return render_template('index.html')

try:
archive.extractall(tmp)
finally:
Expand Down
16 changes: 16 additions & 0 deletions magma/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
<div class="page-header">
<h1>MAGMA <small>Minimally Automated Geospatial Metadata Author</small></h1>
</div>

{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }}" role="alert">
{{ message }}
</div>
{% endfor %}
{% endif %}
{% endwith %}

<form method="POST" enctype="multipart/form-data" class="form-horizontal">
<div class="form-group">
<label for="dataFile" class="col-md-2 control-label">Shapefile <span class="text-danger">*</span></label>
Expand Down Expand Up @@ -50,5 +61,10 @@ <h1>MAGMA <small>Minimally Automated Geospatial Metadata Author</small></h1>
</div>
<script src="static/vendor/jquery/dist/jquery.min.js"></script>
<script src="static/vendor/bootstrap/dist/js/bootstrap.min.js"></script>
<script>
$( "form" ).submit(function( event ) {
$(".alert").remove();
});
</script>
</body>
</html>
36 changes: 36 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import

import pytest

from webtest import TestApp, Upload
from magma.app import create_app

def test_can_load_index(testapp):
r = testapp.get('/')
assert r.status_code == 200
assert 'form' in r

def test_submit_valid_form(testapp):
form = testapp.get('/').form
form['data'] = Upload('tests/fixtures/SDE_DATA_BD_A8GNS_2003.zip')
form['metadata'] = Upload('tests/fixtures/fgdc.xml')
res = form.submit()
assert res.content_type == 'text/xml'

def test_submit_with_shapefile_without_metadata(testapp):
form = testapp.get('/').form
form['data'] = Upload('tests/fixtures/SDE_DATA_BD_A8GNS_2003.zip')
res = form.submit()
assert res.content_type == 'text/xml'

def test_submit_without_shapefile(testapp):
form = testapp.get('/').form
res = form.submit()
assert 'The shapefile is required to proceed' in res

def test_submit_with_invalid_shapefile(testapp):
form = testapp.get('/').form
form['data'] = Upload('tests/fixtures/fgdc.xml')
res = form.submit()
assert 'File is not a zip file' in res

0 comments on commit 58cae00

Please sign in to comment.