[NEW] first commit for all modules coming from training-tools

This commit is contained in:
clementthomas
2023-11-21 12:54:02 +01:00
parent 823f04a7b6
commit 61e01e4be0
94 changed files with 3534 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
from . import survey_question
from . import survey_survey
from . import survey_user_input

View File

@@ -0,0 +1,80 @@
# Copyright 2022 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, fields, models
class SurveyQuestion(models.Model):
_inherit = "survey.question"
allowed_field_ids = fields.Many2many(
comodel_name="ir.model.fields",
compute="_compute_allowed_field_ids",
)
res_partner_field = fields.Many2one(
string="Contact field",
comodel_name="ir.model.fields",
domain="[('id', 'in', allowed_field_ids)]",
)
@api.depends("question_type")
def _compute_allowed_field_ids(self):
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:
record.allowed_field_ids = (
self.env["ir.model.fields"]
.search(
[
("model", "=", "res.partner"),
("ttype", "in", type_mapping.get(record.question_type, [])),
]
)
.ids
)
class SurveyQuestionAnswer(models.Model):
_inherit = "survey.question.answer"
@api.model
def default_get(self, fields):
result = super().default_get(fields)
if (
not result.get("res_partner_field")
or "res_partner_field_resource_ref" not in fields
):
return result
partner_field = self.env["ir.model.fields"].browse(result["res_partner_field"])
# Otherwise we'll just use the value (char, text)
if partner_field.ttype not in {"many2one", "many2many"}:
return result
res = self.env[partner_field.relation].search([], limit=1)
if res:
result["res_partner_field_resource_ref"] = "%s,%s" % (
partner_field.relation,
res.id,
)
return result
@api.model
def _selection_res_partner_field_resource_ref(self):
return [(model.model, model.name) for model in self.env["ir.model"].search([])]
res_partner_field = fields.Many2one(related="question_id.res_partner_field")
res_partner_field_resource_ref = fields.Reference(
string="Contact field value",
selection="_selection_res_partner_field_resource_ref",
)
@api.onchange("res_partner_field_resource_ref")
def _onchange_res_partner_field_resource_ref(self):
"""Set the default value as the product name, although we can change it"""
if self.res_partner_field_resource_ref:
self.value = self.res_partner_field_resource_ref.display_name or ""

View File

@@ -0,0 +1,11 @@
# 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_contact = fields.Boolean(
help="Generate contacts for anonymous survey users",
)

View File

@@ -0,0 +1,77 @@
# Copyright 2022 Tecnativa - David Vidal
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import models
class SurveyUserInput(models.Model):
_inherit = "survey.user_input"
def _prepare_partner(self):
"""Extract partner values from the answers"""
self.ensure_one()
elegible_inputs = self.user_input_line_ids.filtered(
lambda x: x.question_id.res_partner_field and not x.skipped
)
basic_inputs = elegible_inputs.filtered(
lambda x: x.answer_type not in {"suggestion"}
and x.question_id.res_partner_field.name != "comment"
)
vals = {
line.question_id.res_partner_field.name: line[f"value_{line.answer_type}"]
for line in basic_inputs
}
for line in elegible_inputs - basic_inputs:
field_name = line.question_id.res_partner_field.name
if line.question_id.res_partner_field.ttype == "many2one":
vals[
field_name
] = line.suggested_answer_id.res_partner_field_resource_ref.id
elif line.question_id.res_partner_field.ttype == "many2many":
vals.setdefault(field_name, [])
vals[field_name] += [
(4, line.suggested_answer_id.res_partner_field_resource_ref.id)
]
# We'll use the comment field to add any other infos
elif field_name == "comment":
vals.setdefault("comment", "")
value = (
line.suggested_answer_id.value
if line.answer_type == "suggestion"
else line[f"value_{line.answer_type}"]
)
vals["comment"] += f"\n{line.question_id.title}: {value}"
else:
if line.question_id.question_type == "multiple_choice":
if not vals.get(field_name):
vals[field_name] = line.suggested_answer_id.value
else:
vals[field_name] += line.suggested_answer_id.value
else:
vals[field_name] = line.suggested_answer_id.value
return vals
def _create_contact_post_process(self, partner):
"""After creating the lead send an internal message with the input link"""
partner.message_post_with_view(
"mail.message_origin_link",
values={"self": partner, "origin": self.survey_id},
subtype_id=self.env.ref("mail.mt_note").id,
)
def _mark_done(self):
"""Generate the contact when the survey is submitted"""
for user_input in self.filtered(
lambda r: r.survey_id.generate_contact# and not self.partner_id #uncomment to avoid contact generation several times
):
vals = user_input._prepare_partner()
partner = False
email = vals.get("email")
if email:
partner = self.env["res.partner"].search(
[("email", "=", email)], limit=1
)
if not partner:
partner = self.env["res.partner"].create(vals)
self._create_contact_post_process(partner)
self.update({"partner_id": partner.id, "email": partner.email})
return super()._mark_done()