|
| 1 | +from odoo import _, api, fields, models |
| 2 | +from odoo.exceptions import ValidationError |
| 3 | + |
| 4 | + |
| 5 | +class EventQuestion(models.Model): |
| 6 | + _inherit = "event.question" |
| 7 | + |
| 8 | + is_shuttle = fields.Boolean( |
| 9 | + compute="_compute_is_shuttle", store=True, readonly=False |
| 10 | + ) |
| 11 | + |
| 12 | + is_accomodation = fields.Boolean( |
| 13 | + compute="_compute_is_accomodation", store=True, readonly=False |
| 14 | + ) |
| 15 | + |
| 16 | + @api.depends("question_type") |
| 17 | + def _compute_is_shuttle(self): |
| 18 | + for record in self: |
| 19 | + if record.question_type != "simple_choice" or record.is_accomodation: |
| 20 | + record.is_shuttle = False |
| 21 | + |
| 22 | + @api.depends("question_type") |
| 23 | + def _compute_is_accomodation(self): |
| 24 | + for record in self: |
| 25 | + if record.question_type != "simple_choice" or record.is_shuttle: |
| 26 | + record.is_accomodation = False |
| 27 | + |
| 28 | + @api.constrains("is_shuttle", "is_accomodation") |
| 29 | + def _check_shuttle_accomodation(self): |
| 30 | + for record in self: |
| 31 | + if record.is_shuttle and record.is_accomodation: |
| 32 | + raise ValidationError( |
| 33 | + _("Question cannot be both for shuttle and accomodation") |
| 34 | + ) |
| 35 | + |
| 36 | + def _check_accomodation_answers(self, allow_empty=False): |
| 37 | + for question in self.filtered("is_accomodation"): |
| 38 | + flags = question.answer_ids.mapped("is_positive_accomodation_answer") |
| 39 | + if not flags and allow_empty: |
| 40 | + continue |
| 41 | + |
| 42 | + has_positive_accomodation_answer = any(flags) |
| 43 | + if not has_positive_accomodation_answer: |
| 44 | + raise ValidationError( |
| 45 | + _( |
| 46 | + 'Accomodation "%s" question should have positive answer', |
| 47 | + question.title, |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + def _check_accomodation_category(self): |
| 52 | + questions = self.filtered("is_accomodation") |
| 53 | + |
| 54 | + for company in questions.mapped("event_id.company_id"): |
| 55 | + if not company.accomodation_category: |
| 56 | + raise ValidationError(_("Accomodation category is not set in settings")) |
| 57 | + |
| 58 | + def action_generate_ticket_answers(self): |
| 59 | + self._check_shuttle_accomodation() |
| 60 | + |
| 61 | + if self.is_shuttle: |
| 62 | + w = self.env["generate.shuttle.ticket.answers"].create( |
| 63 | + { |
| 64 | + "question": self.id, |
| 65 | + } |
| 66 | + ) |
| 67 | + |
| 68 | + return { |
| 69 | + "type": "ir.actions.act_window", |
| 70 | + "res_model": w._name, |
| 71 | + "res_id": w.id, |
| 72 | + "view_mode": "form", |
| 73 | + "target": "new", |
| 74 | + } |
| 75 | + |
| 76 | + elif self.is_accomodation: |
| 77 | + EQA = self.env["event.question.answer"].sudo() |
| 78 | + |
| 79 | + has_positive_accomodation_answer = any( |
| 80 | + self.answer_ids.mapped("is_positive_accomodation_answer") |
| 81 | + ) |
| 82 | + if not has_positive_accomodation_answer: |
| 83 | + EQA.create( |
| 84 | + { |
| 85 | + "name": _("Yes"), |
| 86 | + "question_id": self.id, |
| 87 | + "is_positive_accomodation_answer": True, |
| 88 | + } |
| 89 | + ) |
| 90 | + |
| 91 | + else: |
| 92 | + raise NotImplementedError() |
| 93 | + |
| 94 | + @api.model_create_multi |
| 95 | + def create(self, vals_list): |
| 96 | + records = super().create(vals_list) |
| 97 | + records._check_accomodation_category() |
| 98 | + records._check_accomodation_answers(allow_empty=True) |
| 99 | + return records |
| 100 | + |
| 101 | + def write(self, vals): |
| 102 | + res = super().write(vals) |
| 103 | + self._check_accomodation_category() |
| 104 | + self._check_accomodation_answers(allow_empty=True) |
| 105 | + return res |
0 commit comments