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.

66 lines
2.1 KiB

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