diff --git a/survey_input_attachment/models/survey_question.py b/survey_input_attachment/models/survey_question.py index 9c16b41..6f2d02a 100644 --- a/survey_input_attachment/models/survey_question.py +++ b/survey_input_attachment/models/survey_question.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- -from odoo import models, fields, _ +from odoo import models, fields, api, _ +from werkzeug.datastructures import FileStorage TYPES = [ ('free_text', _('Multiple Lines Text Box')), @@ -18,3 +19,15 @@ class SurveyQuestion(models.Model): # question_attachment = fields.Binary(string="Joined attachment") 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 diff --git a/survey_input_attachment/models/survey_user_input_line.py b/survey_input_attachment/models/survey_user_input_line.py index 2b44adc..d028325 100644 --- a/survey_input_attachment/models/survey_user_input_line.py +++ b/survey_input_attachment/models/survey_user_input_line.py @@ -33,11 +33,13 @@ class SurveyUserInputLine(models.Model): 'survey_id': question.survey_id.id, 'skipped': False } + file = False + # import ipdb; ipdb.set_trace() if question.constr_mandatory: - file = base64.encodebytes(post[answer_tag].read()) + file = base64.encodebytes(post[answer_tag].read()) 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({ 'answer_type': 'upload_file', 'file': file,