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.

78 lines
2.4 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 (img/pdf)")),
  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 = (
  36. base64.encodebytes(post[answer_tag].read())
  37. if not isinstance(post[answer_tag], str)
  38. else None
  39. )
  40. if answer_tag in post and not isinstance(post[answer_tag], str):
  41. vals.update(
  42. {
  43. "answer_type": "upload_file",
  44. "file": file,
  45. "filename": post[answer_tag].filename,
  46. }
  47. )
  48. if post[answer_tag].content_type == "application/pdf":
  49. vals.update({"file_type": "pdf"})
  50. if "image/" in post[answer_tag].content_type:
  51. vals.update({"file_type": "image"})
  52. else:
  53. vals.update(
  54. {
  55. "answer_type": None,
  56. "file": False,
  57. "filename": False,
  58. "skipped": True,
  59. }
  60. )
  61. old_uil = self.search(
  62. [
  63. ("user_input_id", "=", user_input_id),
  64. ("survey_id", "=", question.survey_id.id),
  65. ("question_id", "=", question.id),
  66. ]
  67. )
  68. if old_uil:
  69. old_uil.write(vals)
  70. else:
  71. old_uil.create(vals)
  72. return True