...
 
Commits (2)
......@@ -37,6 +37,17 @@ class ExportCsvService(object):
return ExportCsvService.regular_export_csv(questionnaire)
raise ValueError("Incorrect export_type parameter!")
@staticmethod
def filter_options(id, options, single=False):
if not isinstance(options, list):
options = list(options)
results = list(filter(lambda o: str(o.id) == str(id), options))
if len(results) == 0:
return None
if single and len(results) == 1:
return results[0]
return results
@staticmethod
def questionnaire_to_csv(questionnaire):
uuid = questionnaire.id
......@@ -98,15 +109,6 @@ class ExportCsvService(object):
matrix_ids.update(flatten(list(val.values())))
matrix_options = MatrixOption.objects.filter(id__in=matrix_ids)
# Helper to find matrix option offline
def filter_options(id, options=matrix_options, single=False):
if not isinstance(options, list):
options = list(options)
results = list(filter(lambda o: str(o.id) == str(id), options))
if single and len(results) == 1:
return results[0]
return results
idx = 0
for s in sessions:
sheets = {}
......@@ -127,13 +129,14 @@ class ExportCsvService(object):
options = list(filter(lambda o: str(o.id) in ids, list(matrix_options)))
resp = {}
for key in reversed(matrix.keys()):
row = filter_options(key, options=options, single=True)
if response.input.id in multi_matrixes:
values = filter(lambda o: str(o.id) in matrix[key], list(options))
resp[row.value] = ', '.join([x.value for x in values])
else:
value = filter_options(matrix[key], options=options, single=True)
resp[row.value] = value.value
row = ExportCsvService.filter_options(key, options, single=True)
if row:
if response.input.id in multi_matrixes:
values = filter(lambda o: str(o.id) in matrix[key], list(options))
resp[row.value] = ', '.join([x.value for x in values])
else:
value = ExportCsvService.filter_options(matrix[key], options, single=True)
resp[row.value] = value.value
for row, val in resp.items():
key = '{} - {}'.format(response.input.id, row)
sheets[response.sheet.session][key] = val
......@@ -148,15 +151,6 @@ class ExportCsvService(object):
@staticmethod
def drawbutton_to_csv(questionnaire, drawbutton, responses,
filename, matrix_options, multi_matrixes):
# Helper to find matrix option offline
def filter_options(id, options=matrix_options, single=False):
if not isinstance(options, list):
options = list(options)
results = list(filter(lambda o: str(o.id) == str(id), options))
if single and len(results) == 1:
return results[0]
return results
headers = [_('Session ID'), _('Created at'), _('Finished at'), _('Questionnaire name'),
_('Field name'), drawbutton.content]
......@@ -202,13 +196,14 @@ class ExportCsvService(object):
options = filter(lambda o: str(o.id) in ids, list(matrix_options))
resp = {}
for key in reversed(matrix.keys()):
row = filter_options(key, options=options, single=True)
if response.input.id in multi_matrixes:
values = filter(lambda o: str(o.id) in matrix[key], list(options))
resp[row.value] = ', '.join([x.value for x in values])
else:
value = filter_options(matrix[key], options=options, single=True)
resp[row.value] = value.value
row = ExportCsvService.filter_options(key, options, single=True)
if row:
if response.input.id in multi_matrixes:
values = filter(lambda o: str(o.id) in matrix[key], list(options))
resp[row.value] = ', '.join([x.value for x in values])
else:
value = ExportCsvService.filter_options(matrix[key], options, single=True)
resp[row.value] = value.value
for row, val in resp.items():
key = '{} - {}'.format(response.input.id, row)
sheets[response.sheet.session][wkt][key] = val
......
......@@ -6,14 +6,12 @@ import json
import os
import uuid
from django.conf import settings
from django.contrib.gis.db import models
from django.contrib.gis.geos import GEOSException, GEOSGeometry
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.core.exceptions import ObjectDoesNotExist
from django.core.files.base import ContentFile
from django.utils.translation import ugettext_lazy as _
from django.core.validators import FileExtensionValidator
from django.conf import settings
from six import string_types
from base_page.helpers import flatten
from geoforms.models import Input, InputOption, MatrixOption, Questionnaire
......@@ -179,7 +177,9 @@ class Response(models.Model):
elif self.input.widget == 'userimage' and self.value_image:
return self.value_image.name
return self.value_text
return self.value_text.replace('\r', '').replace('\n', '')\
if isinstance(self.value_text, string_types)\
else self.value_text
def __str__(self):
return str(self.id)