Browse Source

[IMP] survey_input_attachmeent

12.0
Rémi FRANÇOIS 5 years ago
parent
commit
eadfc8b76b
  1. 15
      survey_input_attachment/models/survey_question.py
  2. 8
      survey_input_attachment/models/survey_user_input_line.py

15
survey_input_attachment/models/survey_question.py

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from odoo import models, fields, _
from odoo import models, fields, api, _
from werkzeug.datastructures import FileStorage
TYPES = [ TYPES = [
('free_text', _('Multiple Lines Text Box')), ('free_text', _('Multiple Lines Text Box')),
@ -18,3 +19,15 @@ class SurveyQuestion(models.Model):
# question_attachment = fields.Binary(string="Joined attachment") # question_attachment = fields.Binary(string="Joined attachment")
type = fields.Selection(selection=TYPES) type = fields.Selection(selection=TYPES)
@api.multi
def validate_upload_file(self, post, answer_tag):
self.ensure_one()
errors = {}
# Empty answer to mandatory question
if self.constr_mandatory and not isinstance(post[answer_tag], FileStorage):
errors.update({answer_tag: self.constr_error_msg})
# Bad file type
if isinstance(post[answer_tag], FileStorage) and post[answer_tag].content_type != 'application/pdf' and 'image/' not in post[answer_tag].content_type:
errors.update({answer_tag: _("The file you joined is not an image nor a PDF file.")})
return errors

8
survey_input_attachment/models/survey_user_input_line.py

@ -33,11 +33,13 @@ class SurveyUserInputLine(models.Model):
'survey_id': question.survey_id.id, 'survey_id': question.survey_id.id,
'skipped': False 'skipped': False
} }
file = False
# import ipdb; ipdb.set_trace()
if question.constr_mandatory: if question.constr_mandatory:
file = base64.encodebytes(post[answer_tag].read())
file = base64.encodebytes(post[answer_tag].read())
else: else:
file = base64.encodebytes(post[answer_tag].read()) if post[answer_tag] else None
if answer_tag in post:
file = base64.encodebytes(post[answer_tag].read()) if not isinstance(post[answer_tag], str) else None
if answer_tag in post and not isinstance(post[answer_tag], str):
vals.update({ vals.update({
'answer_type': 'upload_file', 'answer_type': 'upload_file',
'file': file, 'file': file,

Loading…
Cancel
Save