Skip to content

Commit

Permalink
fixes on file answer types
Browse files Browse the repository at this point in the history
  • Loading branch information
antsmc2 committed Nov 17, 2017
1 parent 18018ec commit 9ddb724
Showing 1 changed file with 74 additions and 13 deletions.
87 changes: 74 additions & 13 deletions survey/models/interviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import Max
from survey.utils.logger import glogger as logger
from survey.models.base import BaseModel
from survey.models.access_channels import InterviewerAccess, ODKAccess, USSDAccess
from survey.models.locations import Point
Expand Down Expand Up @@ -103,19 +104,22 @@ def _save_record(record):
map(lambda (q_id, answer): _save_answer(interview, q_id, answer), survey_parameters.items())

def _save_answer(interview, q_id, answer):
question = question_map.get(q_id, None)
if question and answer:
answer_class = Answer.get_class(question.answer_type)
if question.answer_type in [AudioAnswer.choice_name(), ImageAnswer.choice_name(),
VideoAnswer.choice_name()]:
answer = media_files.get(answer, None)
try:
old_answer = answer_class.objects.get(interview=interview, question=question)
old_answer.update(answer)
except answer_class.DoesNotExist:
answer_class.create(interview, question, answer)
except Exception, ex:
print 'exception: %s' % unicode(ex)
try:
question = question_map.get(q_id, None)
if question and answer:
answer_class = Answer.get_class(question.answer_type)
if question.answer_type in [AudioAnswer.choice_name(), ImageAnswer.choice_name(),
VideoAnswer.choice_name()]:
answer = media_files.get(answer, None)
try:
old_answer = answer_class.objects.get(interview=interview, question=question)
old_answer.update(answer)
except answer_class.DoesNotExist:
answer_class.create(interview, question, answer)
except Exception, ex:
logger.error('error saving %s, desc: %s' % (q_id, str(ex)))
except Exception, ex:
logger.error('error saving %s, desc: %s' % (q_id, str(ex)))
map(_save_record, answers)
return interviews

Expand Down Expand Up @@ -812,6 +816,25 @@ def validate_test_value(cls, value):
class AudioAnswer(Answer):
value = models.FileField(upload_to=settings.ANSWER_UPLOADS, null=True)

@classmethod
def create(cls, interview, question, answer, as_text=None, as_value=None):
try:
# answer is a file object
as_value = answer.name
as_text = answer.name
except BaseException:
as_text = ''
as_value = ''
pass
return cls.objects.create(
question=question,
value=answer,
question_type=question.__class__.type_name(),
interview=interview,
identifier=question.identifier,
as_text=as_text,
as_value=as_value)

class Meta:
app_label = 'survey'
abstract = False
Expand All @@ -827,6 +850,25 @@ def to_text(self):
class VideoAnswer(Answer):
value = models.FileField(upload_to=settings.ANSWER_UPLOADS, null=True)

@classmethod
def create(cls, interview, question, answer, as_text=None, as_value=None):
try:
# answer is a file object
as_value = answer.name
as_text = answer.name
except BaseException:
as_text = ''
as_value = ''
pass
return cls.objects.create(
question=question,
value=answer,
question_type=question.__class__.type_name(),
interview=interview,
identifier=question.identifier,
as_text=as_text,
as_value=as_value)

class Meta:
app_label = 'survey'
abstract = False
Expand All @@ -842,6 +884,25 @@ def to_text(self):
class ImageAnswer(Answer):
value = models.FileField(upload_to=settings.ANSWER_UPLOADS, null=True)

@classmethod
def create(cls, interview, question, answer, as_text=None, as_value=None):
try:
# answer is a file object
as_value = answer.name
as_text = answer.name
except BaseException:
as_text = ''
as_value = ''
pass
return cls.objects.create(
question=question,
value=answer,
question_type=question.__class__.type_name(),
interview=interview,
identifier=question.identifier,
as_text=as_text,
as_value=as_value)

class Meta:
app_label = 'survey'
abstract = False
Expand Down

0 comments on commit 9ddb724

Please sign in to comment.