[NEW] first commit for all modules coming from training-tools
This commit is contained in:
1
survey_event_registration_generation/__init__.py
Normal file
1
survey_event_registration_generation/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from . import models
|
18
survey_event_registration_generation/__manifest__.py
Normal file
18
survey_event_registration_generation/__manifest__.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Copyright 2016-2020 Akretion France (<https://www.akretion.com>)
|
||||
# @author: Alexis de Lattre <alexis.delattre@akretion.com>
|
||||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
|
||||
|
||||
{
|
||||
"name": "Survey event registration generation",
|
||||
"version": "16.0.0.0.0",
|
||||
"license": "AGPL-3",
|
||||
"author": "Elabore",
|
||||
"website": "https://www.elabore.coop",
|
||||
"category": "",
|
||||
"depends": ["survey", "survey_base"],
|
||||
"data": [
|
||||
'views/survey_question_views.xml',
|
||||
'views/survey_survey_views.xml',
|
||||
],
|
||||
"installable": True,
|
||||
}
|
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
|
@@ -0,0 +1,2 @@
|
||||
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
|
||||
access_ir_model_fields_survey_user,ir.model.fields.survey.user,base.model_ir_model_fields,survey.group_survey_user,1,0,0,0
|
|
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<odoo>
|
||||
|
||||
<!-- Survey question form -->
|
||||
<record id="survey_question_form" model="ir.ui.view">
|
||||
<field name="model">survey.question</field>
|
||||
<field name="inherit_id" ref="survey.survey_question_form" />
|
||||
<field name="arch" type="xml">
|
||||
<xpath expr="//field[@name='comments_allowed']/.." position="after">
|
||||
<group name="event_registration" string="Event registration">
|
||||
<!-- event registration field, filtered by event_registration_allowed_field_ids (invisible) -->
|
||||
<field name="event_registration_field" widget="selection" />
|
||||
<field name="event_registration_allowed_field_ids" invisible="1" />
|
||||
</group>
|
||||
</xpath>
|
||||
|
||||
</field>
|
||||
</record>
|
||||
</odoo>
|
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<odoo>
|
||||
<!-- Survey form -->
|
||||
<record id="survey_form" model="ir.ui.view">
|
||||
<field name="model">survey.survey</field>
|
||||
<field name="inherit_id" ref="survey.survey_survey_view_form" />
|
||||
<field name="arch" type="xml">
|
||||
<group name="options" position="inside">
|
||||
<group name="event_registration_options" string="Events registrations">
|
||||
<!-- Possibility to generate event registration -->
|
||||
<field name="generate_registration" />
|
||||
</group>
|
||||
</group>
|
||||
</field>
|
||||
</record>
|
||||
</odoo>
|
Reference in New Issue
Block a user