[IMP] code comment in several modules

This commit is contained in:
clementthomas
2023-10-04 16:55:29 +02:00
parent 915e2bf583
commit e24614f571
17 changed files with 65 additions and 123 deletions

View File

@@ -4,7 +4,16 @@ from odoo import models, fields, api
class EventEvent(models.Model):
_inherit = 'event.event'
def get_events_visible_in_survey(self, product_id=False):
def get_events_visible_in_survey(self, product_id=None):
"""Search events in stage visible in survey.
Optionnaly filtered by product present in ticket.
Args:
product_id (product.product, optional): to filter only events with tickets using this product. Defaults to None.
Returns:
event.event: Events
"""
if product_id:
event_tickets = self.env['event.event.ticket'].search([('product_id','=',product_id)])
return self.env['event.event'].search([('stage_id.visible_in_survey','=',True),('event_ticket_ids','in',event_tickets.id)])

View File

@@ -4,4 +4,4 @@ from odoo import models, fields, api
class EventStage(models.Model):
_inherit = 'event.stage'
visible_in_survey = fields.Boolean('Visible in surveys')
visible_in_survey = fields.Boolean('Visible in surveys') #if checked, only events on this stage are visible in surveys

View File

@@ -5,6 +5,11 @@ class ProductProduct(models.Model):
_inherit = 'product.product'
def get_event_products_visible_in_survey(self):
"""Search products used in tickets of events visibles in surveys
Returns:
product.product: products
"""
events = self.env['event.event'].get_events_visible_in_survey()
products = set()
for event in events:

View File

@@ -5,8 +5,9 @@ class SurveyQuestion(models.Model):
_inherit = 'survey.question'
question_type = fields.Selection(
selection_add=[('event_product', 'Event product'),('event', 'Event')])
selection_add=[('event_product', 'Event product'),('event', 'Event')]) #event_product : List product used in tickets of visible events
#event : List events visible in surveys
event_product_question_id = fields.Many2one(
'survey.question', string="Event product question", copy=False, compute="_compute_event_product_question_id",
@@ -15,20 +16,22 @@ class SurveyQuestion(models.Model):
'&', ('question_type', '=', 'event_product'), \
'|', \
('sequence', '<', sequence), \
'&', ('sequence', '=', sequence), ('id', '<', id)]")
'&', ('sequence', '=', sequence), ('id', '<', id)]") #event product question, used by event question, to filter list of events
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"],
@@ -40,28 +43,21 @@ class SurveyQuestion(models.Model):
}
for record in self:
if record.question_type == "event":
record.event_registration_allowed_field_ids = (
self.env["ir.model.fields"]
.search(
record.event_registration_allowed_field_ids = self.env["ir.model.fields"].search(
[
("model", "=", "event.registration"),
("name", "=", "event_id"),
]
)
.ids
)
record.event_registration_allowed_field_ids = (
self.env["ir.model.fields"]
.search(
).ids
record.event_registration_allowed_field_ids = self.env["ir.model.fields"].search(
[
("model", "=", "event.registration"),
("ttype", "in", type_mapping.get(record.question_type, [])),
]
)
.ids
)
).ids
@api.depends('question_type')
def _compute_event_product_question_id(self):
""" Used as an 'onchange' : Reset the event product question if user change question type
@@ -70,42 +66,3 @@ class SurveyQuestion(models.Model):
if not question.question_type == 'event' or question.event_product_question_id is None:
question.event_product_question_id = False
class SurveyQuestionAnswer(models.Model):
_inherit = "survey.question.answer"
@api.model
def default_get(self, fields):
result = super().default_get(fields)
if (
not result.get("event_registration_field")
or "event_registration_field_resource_ref" not in fields
):
return result
registration_field = self.env["ir.model.fields"].browse(result["event_registration_field"])
# Otherwise we'll just use the value (char, text)
if registration_field.ttype not in {"many2one", "many2many"}:
return result
res = self.env[registration_field.relation].search([], limit=1)
if res:
result["event_registration_field_resource_ref"] = "%s,%s" % (
registration_field.relation,
res.id,
)
return result
@api.model
def _selection_event_registration_field_resource_ref(self):
return [(model.model, model.name) for model in self.env["ir.model"].search([])]
event_registration_field = fields.Many2one(related="question_id.event_registration_field")
event_registration_field_resource_ref = fields.Reference(
string="Event registration field value",
selection="_selection_event_registration_field_resource_ref",
)
@api.onchange("event_registration_field_resource_ref")
def _onchange_event_registration_field_resource_ref(self):
"""Set the default value as the display_name, although we can change it"""
if self.event_registration_field_resource_ref:
self.value = self.event_registration_field_resource_ref.display_name or ""

View File

@@ -7,7 +7,7 @@ class SurveySurvey(models.Model):
_inherit = "survey.survey"
generate_registration = fields.Boolean(
help="Generate registration for selected event",
help="Generate event registration for selected event",
)

View File

@@ -15,12 +15,14 @@ _logger = logging.getLogger(__name__)
class SurveyUserInput(models.Model):
_inherit = 'survey.user_input'
registration_id = fields.Many2one('event.registration', 'Event registration')
registration_id = fields.Many2one('event.registration', 'Event registration') #registration created automaticaly on survey post
event_id = fields.Many2one('event.event', 'Événement', compute='compute_event_id', store=True)
event_id = fields.Many2one('event.event', 'Événement', compute='compute_event_id', store=True) #related event - answer of "event" question
@api.depends('user_input_line_ids')
def compute_event_id(self):
"""set event_id as answer of "event" question
"""
for user_input in self:
for user_input_line in user_input.user_input_line_ids:
if user_input_line.answer_type == 'event':
@@ -77,56 +79,23 @@ class SurveyUserInput(models.Model):
def _prepare_registration(self):
"""Extract registration values from the answers"""
"""TODO: simplifier le code"""
elegible_inputs = self.user_input_line_ids.filtered(
lambda x: x.question_id.event_registration_field and not x.skipped
)
basic_inputs = elegible_inputs.filtered(
lambda x: x.answer_type not in {"suggestion"}
and x.question_id.event_registration_field.name != "comment"
)
vals = {}
for line in basic_inputs:
for line in elegible_inputs:
if line.question_id.event_registration_field.ttype == 'many2one':
vals[line.question_id.event_registration_field.name] = line[f"value_{line.answer_type}"].id
else:
vals[line.question_id.event_registration_field.name] = line[f"value_{line.answer_type}"]
for line in elegible_inputs - basic_inputs:
field_name = line.question_id.event_registration_field.name
if line.question_id.event_registration_field.ttype == "many2one":
vals[
field_name
] = line.suggested_answer_id.event_registration_field_resource_ref.id
elif line.question_id.event_registration_field.ttype == "many2many":
vals.setdefault(field_name, [])
vals[field_name] += [
(4, line.suggested_answer_id.event_registration_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_registration_post_process(self, registration):
"""After creating the lead send an internal message with the input link"""
"""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},

View File

@@ -13,12 +13,14 @@ class SurveyUserInputLine(models.Model):
_inherit = 'survey.user_input.line'
answer_type = fields.Selection(
selection_add=[('event_product', 'Event product'),('event', 'Event')])
selection_add=[('event_product', 'Event product'),('event', 'Event')]) #event_product if answer is a ticket Product, event if answer is an event
value_event = fields.Many2one('event.event', string="Event")
value_event_product = fields.Many2one('product.product', string="Event product")
value_event = fields.Many2one('event.event', string="Event") #selected event
value_event_product = fields.Many2one('product.product', string="Event product") #selected product
def _compute_display_name(self):
"""display event product name, or event name, depending on answer type
"""
super()._compute_display_name()
for line in self:
if line.answer_type == 'event_product':