[NEW] first commit for all modules coming from training-tools
This commit is contained in:
3
survey_event_registration_generation/models/__init__.py
Normal file
3
survey_event_registration_generation/models/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from . import survey_user_input
|
||||
from . import survey_survey
|
||||
from . import survey_question
|
@@ -0,0 +1,38 @@
|
||||
from email.policy import default
|
||||
from odoo import models, fields, api
|
||||
|
||||
|
||||
class SurveyQuestion(models.Model):
|
||||
_inherit = 'survey.question'
|
||||
|
||||
event_registration_allowed_field_ids = fields.Many2many(
|
||||
comodel_name="ir.model.fields",
|
||||
compute="_compute_event_registration_allowed_field_ids",
|
||||
) #fields of event registration, proposed in question, to associate answer to good event registration field, during event registration creation
|
||||
event_registration_field = fields.Many2one(
|
||||
string="Event registration field",
|
||||
comodel_name="ir.model.fields",
|
||||
domain="[('id', 'in', event_registration_allowed_field_ids)]",
|
||||
) #field of event registration selected, used in event registration creation
|
||||
|
||||
@api.depends("question_type")
|
||||
def _compute_event_registration_allowed_field_ids(self):
|
||||
"""propose all event registration fields corresponding to selected question type
|
||||
"""
|
||||
type_mapping = {
|
||||
"char_box": ["char", "text"],
|
||||
"text_box": ["html"],
|
||||
"numerical_box": ["integer", "float", "html", "char"],
|
||||
"date": ["date", "text", "char"],
|
||||
"datetime": ["datetime", "html", "char"],
|
||||
"simple_choice": ["many2one", "html", "char"],
|
||||
"multiple_choice": ["many2many", "html", "char"],
|
||||
}
|
||||
for record in self:
|
||||
search_domain = [
|
||||
("model", "=", "event.registration"),
|
||||
("ttype", "in", type_mapping.get(record.question_type, [])),
|
||||
]
|
||||
|
||||
record.event_registration_allowed_field_ids = self.env["ir.model.fields"].search(search_domain).ids
|
||||
|
12
survey_event_registration_generation/models/survey_survey.py
Normal file
12
survey_event_registration_generation/models/survey_survey.py
Normal file
@@ -0,0 +1,12 @@
|
||||
# Copyright 2023 Tecnativa - David Vidal
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
|
||||
from odoo import fields, models
|
||||
|
||||
|
||||
class SurveySurvey(models.Model):
|
||||
_inherit = "survey.survey"
|
||||
|
||||
generate_registration = fields.Boolean(
|
||||
help="Generate event registration for selected event",
|
||||
)
|
||||
|
@@ -0,0 +1,82 @@
|
||||
|
||||
import logging
|
||||
import textwrap
|
||||
import uuid
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import ValidationError
|
||||
from odoo.tools import float_is_zero
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SurveyUserInput(models.Model):
|
||||
_inherit = 'survey.user_input'
|
||||
|
||||
registration_id = fields.Many2one('event.registration', 'Event registration') #registration created automaticaly on survey post
|
||||
|
||||
|
||||
|
||||
def _prepare_registration(self):
|
||||
"""Extract registration values from the answers"""
|
||||
|
||||
elegible_inputs = self.user_input_line_ids.filtered(
|
||||
lambda x: x.question_id.event_registration_field and not x.skipped
|
||||
)
|
||||
|
||||
vals = {}
|
||||
for line in elegible_inputs:
|
||||
if line.question_id.event_registration_field.ttype == 'many2one':
|
||||
vals[line.question_id.event_registration_field.name] = line.suggested_answer_id.record_reference
|
||||
else:
|
||||
vals[line.question_id.event_registration_field.name] = line[f"value_{line.answer_type}"]
|
||||
|
||||
|
||||
return vals
|
||||
|
||||
def _create_registration_post_process(self, registration):
|
||||
"""After creating the event registration send an internal message with the input link"""
|
||||
registration.message_post_with_view(
|
||||
"mail.message_origin_link",
|
||||
values={"self": registration, "origin": self},
|
||||
subtype_id=self.env.ref("mail.mt_note").id,
|
||||
)
|
||||
|
||||
def _mark_done(self):
|
||||
"""Generate registration when the survey is submitted"""
|
||||
for user_input in self.filtered(
|
||||
lambda r: r.survey_id.generate_registration and not self.registration_id
|
||||
):
|
||||
vals = user_input._prepare_registration()
|
||||
|
||||
# check doublon : if old draft registration already exists : update it
|
||||
email = vals.get('email')
|
||||
event_id = vals.get('event_id')
|
||||
old_registration = False
|
||||
if email and event_id:
|
||||
old_registration = self.env["event.registration"].search([('email','=',email),('event_id','=',event_id)])
|
||||
if old_registration:
|
||||
old_registration = old_registration[0]
|
||||
if old_registration.state == 'draft':
|
||||
registration = old_registration
|
||||
registration.write(vals)
|
||||
registration.message_post_with_view(
|
||||
"mail.message_origin_link",
|
||||
values={"edit":True, "self": registration, "origin": user_input},
|
||||
subtype_id=self.env.ref("mail.mt_note").id,
|
||||
)
|
||||
|
||||
if not old_registration:
|
||||
registration = self.env["event.registration"].create(vals)
|
||||
self._create_registration_post_process(registration)
|
||||
|
||||
self.update({"registration_id": registration.id})
|
||||
res = super()._mark_done()
|
||||
|
||||
# after all, set partner id of registration as the partner of user input
|
||||
for user_input in self:
|
||||
if user_input.registration_id and user_input.partner_id:
|
||||
user_input.registration_id.partner_id = user_input.partner_id
|
||||
return res
|
Reference in New Issue
Block a user