Odoo modules related to surveys
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.9 KiB

  1. # -*- coding: utf-8 -*-
  2. import base64
  3. from odoo import models, fields, api, _
  4. from .survey_question import TYPES
  5. FILE_TYPES = [
  6. ('image', _('Image')),
  7. ('pdf', _('PDF file')),
  8. ]
  9. class SurveyUserInputLine(models.Model):
  10. _inherit = 'survey.user_input_line'
  11. answer_type = fields.Selection(selection=TYPES)
  12. file = fields.Binary(string='Uploaded file')
  13. filename = fields.Char(string='Uploaded file name')
  14. file_type = fields.Selection(selection=FILE_TYPES, string="File type")
  15. @api.model
  16. def save_line_upload_file(self, user_input_id, question, post, answer_tag):
  17. vals = {
  18. 'user_input_id': user_input_id,
  19. 'question_id': question.id,
  20. 'survey_id': question.survey_id.id,
  21. 'skipped': False
  22. }
  23. if question.constr_mandatory:
  24. file = base64.encodebytes(post[answer_tag].read())
  25. else:
  26. file = base64.encodebytes(post[answer_tag].read()) if post[answer_tag] else None
  27. if answer_tag in post:
  28. vals.update({
  29. 'answer_type': 'upload_file',
  30. 'file': file,
  31. 'filename': post[answer_tag].filename,
  32. })
  33. if post[answer_tag].content_type == 'application/pdf':
  34. vals.update({'file_type': 'pdf'})
  35. if 'image/' in post[answer_tag].content_type:
  36. vals.update({'file_type': 'image'})
  37. else:
  38. vals.update({
  39. 'answer_type': None,
  40. 'file': False,
  41. 'filename': False,
  42. 'skipped': True,
  43. })
  44. old_uil = self.search([
  45. ('user_input_id', '=', user_input_id),
  46. ('survey_id', '=', question.survey_id.id),
  47. ('question_id', '=', question.id)
  48. ])
  49. if old_uil:
  50. old_uil.write(vals)
  51. else:
  52. old_uil.create(vals)
  53. return True