event_event.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class EventType(models.Model):
  5. _inherit = 'event.type'
  6. question_ids = fields.One2many(
  7. 'event.question', 'event_type_id',
  8. string='Questions', copy=True)
  9. class EventEvent(models.Model):
  10. """ Override Event model to add optional questions when buying tickets. """
  11. _inherit = 'event.event'
  12. question_ids = fields.One2many(
  13. 'event.question', 'event_id', 'Questions', copy=True,
  14. compute='_compute_question_ids', readonly=False, store=True)
  15. general_question_ids = fields.One2many('event.question', 'event_id', 'General Questions',
  16. domain=[('once_per_order', '=', True)])
  17. specific_question_ids = fields.One2many('event.question', 'event_id', 'Specific Questions',
  18. domain=[('once_per_order', '=', False)])
  19. @api.depends('event_type_id')
  20. def _compute_question_ids(self):
  21. """ Update event questions from its event type. Depends are set only on
  22. event_type_id itself to emulate an onchange. Changing event type content
  23. itself should not trigger this method.
  24. When synchronizing questions:
  25. * lines that no answer are removed;
  26. * type lines are added;
  27. """
  28. if self._origin.question_ids:
  29. # lines to keep: those with already given answers
  30. questions_tokeep_ids = self.env['event.registration.answer'].search(
  31. [('question_id', 'in', self._origin.question_ids.ids)]
  32. ).question_id.ids
  33. else:
  34. questions_tokeep_ids = []
  35. for event in self:
  36. if not event.event_type_id and not event.question_ids:
  37. event.question_ids = False
  38. continue
  39. if questions_tokeep_ids:
  40. questions_toremove = event._origin.question_ids.filtered(lambda question: question.id not in questions_tokeep_ids)
  41. command = [(3, question.id) for question in questions_toremove]
  42. else:
  43. command = [(5, 0)]
  44. if event.event_type_id.question_ids:
  45. command += [
  46. (0, 0, {
  47. 'title': question.title,
  48. 'question_type': question.question_type,
  49. 'sequence': question.sequence,
  50. 'once_per_order': question.once_per_order,
  51. 'is_mandatory_answer': question.is_mandatory_answer,
  52. 'answer_ids': [(0, 0, {
  53. 'name': answer.name,
  54. 'sequence': answer.sequence
  55. }) for answer in question.answer_ids],
  56. }) for question in event.event_type_id.question_ids
  57. ]
  58. event.question_ids = command