Skip to content
This repository has been archived by the owner on Nov 27, 2017. It is now read-only.

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermendes committed Jun 14, 2016
1 parent 563e758 commit bf3ff6c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 33 deletions.
63 changes: 63 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Local
settings.py
pybossa/
3 changes: 1 addition & 2 deletions libcrowds_data/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ def __init__(self, **kwargs):
defaults.update(kwargs)
super(DataBlueprint, self).__init__(**defaults)
self.add_url_rule("/", view_func=index)
self.add_url_rule("/<short_name>/csv_export",
view_func=csv_export)
self.add_url_rule("/<short_name>/csv_export", view_func=csv_export)
29 changes: 14 additions & 15 deletions libcrowds_data/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"""Views modules for libcrowds-data."""

import StringIO
import itertools
from flask import render_template, make_response
from pybossa.core import project_repo, result_repo
from pybossa.util import UnicodeWriter
Expand All @@ -27,27 +28,25 @@ def csv_export(short_name):
project = project_repo.get_by_shortname(short_name)
if project is None: # pragma: no cover
abort(404)
print '1'
si = StringIO.StringIO()
writer = UnicodeWriter(si)
exporter = Exporter()
print '2'
name = exporter._project_name_latin_encoded(project)
print '3'
secure_name = secure_filename('{0}_{1}.csv'.format(name, 'results'))
print '4'
results = result_repo.filter_by(project_id=project.id)
print '5'
if len(results) > 0:
keys = [r.info.keys() for r in results if isinstance(r.info, dict)]
headers = results[0].dictize().keys()
headers += list(set(list(itertools.chain(*keys))))
writer.writerow([h for h in headers])
for row in results:
values = results[0].dictize().items()
if isinstance(row.info, dict):
values.extend([row.info.get(h, '') for h in headers])
writer.writerow(values)
data = []

for r in results:
row = {k: v for k, v in r.dictize().items()}
if isinstance(row['info'], dict): # Explode info
keys = row['info'].keys()
for k in keys:
row['info_{0}'.format(k)] = row['info'][k]
data.append(row)
headers = set(itertools.chain(*[row.keys() for row in data]))
writer.writerow([h for h in headers])
for row in data:
writer.writerow([row.get(h, '') for h in headers])

fn = "filename={0}".format(secure_name)
resp = make_response(si.getvalue())
Expand Down
39 changes: 23 additions & 16 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from default import with_context
from helper import web
from factories import ProjectFactory, TaskFactory, TaskRunFactory
from pybossa.core import result_repo


def setUpPackage():
Expand All @@ -30,31 +31,37 @@ def setUp(self):
self.task = TaskFactory.create(n_answers=1, state='completed')

@with_context
def test_get_data_view(self):
res = self.app.get('/data')
assert res.status_code == 200
def test_get_main_view(self):
res = self.app.get('/data', follow_redirects=True)
assert res.status_code == 200, res.status_code

@with_context
def test_get_csv_export_view(self):
res = self.app.get('/data/project/csv_export', follow_redirects=True)
assert res.status_code == 200, res.status_code

@with_context
def test_csv_file_exported(self):
self.signin(email='owner@a.com', password='1234')
res = self.app.get('/project/csv_eport')
print res.headers
res = self.app.get('/data/project/csv_export', follow_redirects=True)
content = res.headers['Content-Disposition']
content_type = res.headers['Content-Type']
fn = "{0}_results.csv".format(self.project.short_name)
assert fn in content and "text/csv" in content_type

assert fn in content, content
assert "text/csv" in content_type, content_type

@with_context
@patch('libcrowds_data.view.UnicodeWriter.writerow')
@patch('libcrowds_data.view.result_repo.filter_by')
def test_populated_results_written_to_csv(self, mock_writer, mock_filter):
TaskRunFactory.create(project=self.project, task=self.task,
info={'n': 1})
res = self.app.get('/project/csv_eport')
print mock_writer.call_args_list
def test_correct_data_written_to_csv(self, mock_writer):
TaskRunFactory.create(project=self.project, task=self.task)
result = result_repo.filter_by(project_id=self.project.id)[0]
result.info = {'n': 42}
result_repo.update(result)
res = self.app.get('/data/project/csv_export', follow_redirects=True)
expected_headers = ['info', 'task_id', 'created', 'last_version',
'task_run_ids', 'project_id', 'id', 'info_n']
expected_row = result.dictize().values() + [42]
headers = mock_writer.call_args_list[0][0][0]
row = mock_writer.call_args_list[1][0][0]

assert headers == ['task_id']
assert row == [self.task_id]
assert sorted(headers) == sorted(expected_headers)
assert sorted(row) == sorted(expected_row)

0 comments on commit bf3ff6c

Please sign in to comment.