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.

68 lines
2.2 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. file = False
  31. # import ipdb; ipdb.set_trace()
  32. if question.constr_mandatory:
  33. file = base64.encodebytes(post[answer_tag].read())
  34. else:
  35. file = base64.encodebytes(post[answer_tag].read()) if not isinstance(post[answer_tag], str) else None
  36. if answer_tag in post and not isinstance(post[answer_tag], str):
  37. vals.update({
  38. 'answer_type': 'upload_file',
  39. 'file': file,
  40. 'filename': post[answer_tag].filename,
  41. })
  42. if post[answer_tag].content_type == 'application/pdf':
  43. vals.update({'file_type': 'pdf'})
  44. if 'image/' in post[answer_tag].content_type:
  45. vals.update({'file_type': 'image'})
  46. else:
  47. vals.update({
  48. 'answer_type': None,
  49. 'file': False,
  50. 'filename': False,
  51. 'skipped': True,
  52. })
  53. old_uil = self.search([
  54. ('user_input_id', '=', user_input_id),
  55. ('survey_id', '=', question.survey_id.id),
  56. ('question_id', '=', question.id)
  57. ])
  58. if old_uil:
  59. old_uil.write(vals)
  60. else:
  61. old_uil.create(vals)
  62. return True