[REF]pms: pms_automated_mails model refactoring

This commit is contained in:
braisab
2022-03-28 21:12:56 +02:00
committed by Darío Lodeiros
parent 574983a220
commit 30e37b3476
6 changed files with 181 additions and 123 deletions

View File

@@ -61,11 +61,11 @@ class PmsAutomatedMails(models.Model):
string="Moment",
help="Moment in relation to the action in which the email will be sent",
selection=[
("in_act", "In the act"),
("before", "Before"),
("after", "After"),
("in_act", "In the act"),
],
default="before",
default="in_act",
)
active = fields.Boolean(
@@ -94,6 +94,9 @@ class PmsAutomatedMails(models.Model):
"usage": "ir_cron",
"model_id": dict_val["model_id"],
}
if action == "checkout":
code = "record.send_exit_email(" + str(template_id) + ")"
action_server_vals.update({"state": "code", "code": code})
action_server = self.env["ir.actions.server"].create(action_server_vals)
automated_actions_vals = {
"active": active,
@@ -127,7 +130,7 @@ class PmsAutomatedMails(models.Model):
result = super(PmsAutomatedMails, self).write(vals)
is_create = False
if (
self.action in ("creation", "write", "cancel", "invoice")
self.action in ("creation", "write", "cancel", "invoice", "checkout")
and self.moment == "before"
):
raise UserError(_("The moment for this action cannot be 'Before'"))
@@ -142,6 +145,9 @@ class PmsAutomatedMails(models.Model):
"usage": "ir_cron",
"model_id": dict_val["model_id"],
}
if vals.get("action") == "checkout":
code = "record.send_exit_email(" + str(self.template_id) + ")"
action_server_vals.update({"state": "code", "code": code})
action_server.write(action_server_vals)
automated_actions_vals = {
"active": self.active,
@@ -181,14 +187,14 @@ class PmsAutomatedMails(models.Model):
@api.model
def _get_auto_action_fields_in_creation_action(self, moment, time):
model_field = False
model_id = self.env["ir.model"].search([("model", "=", "pms.reservation")]).id
model_id = self.env["ir.model"].search([("model", "=", "pms.folio")]).id
if moment == "in_act":
trigger = "on_create"
time = 0
else:
trigger = "on_time"
model_field = self.env["ir.model.fields"].search(
[("model", "=", "pms.reservation"), ("name", "=", "create_date")]
[("model", "=", "pms.folio"), ("name", "=", "create_date")]
)
result = {
"model_id": model_id,
@@ -240,16 +246,20 @@ class PmsAutomatedMails(models.Model):
@api.model
def _get_auto_action_fields_in_checkout_action(self, moment, time):
model_id = self.env["ir.model"].search([("model", "=", "pms.reservation")]).id
trigger = "on_time"
model_field = self.env["ir.model.fields"].search(
[("model", "=", "pms.reservation"), ("name", "=", "checkout")]
model_id = (
self.env["ir.model"].search([("model", "=", "pms.checkin.partner")]).id
)
trigger = "on_write"
model_field = self.env["ir.model.fields"].search(
[("model", "=", "pms.checkin.partner"), ("name", "=", "state")]
)
if moment == "before":
time = time * (-1)
if moment == "in_act":
trigger = "on_write"
time = 0
else:
trigger = "on_time"
model_field = self.env["ir.model.fields"].search(
[("model", "=", "pms.checkin.partner"), ("name", "=", "departure")]
)
result = {
"model_id": model_id,
"trigger": trigger,
@@ -308,14 +318,21 @@ class PmsAutomatedMails(models.Model):
dict_val = {}
if action == "creation":
dict_val = self._get_auto_action_fields_in_creation_action(moment, time)
filter_domain = [
("first_checkin", ">=", str(fields.date.today())),
("reservation_ids.to_send_mail", "=", True),
]
elif action == "write" or action == "cancel":
dict_val = self._get_auto_action_fields_in_write_or_cancel_action(
moment, time
)
if action == "cancel":
filter_domain = [
("state", "=", "cancelled"),
("state", "=", "cancel"),
]
trigger_fields = self.env["ir.model.fields"].search(
[("model", "=", "pms.reservation"), ("name", "=", "state")]
)
elif action == "checkin":
dict_val = self._get_auto_action_fields_in_checkin_action(moment, time)
if moment == "in_act":
@@ -330,11 +347,15 @@ class PmsAutomatedMails(models.Model):
dict_val = self._get_auto_action_fields_in_checkout_action(moment, time)
if moment == "in_act":
trigger_fields = self.env["ir.model.fields"].search(
[("model", "=", "pms.reservation"), ("name", "=", "state")]
[("model", "=", "pms.checkin.partner"), ("name", "=", "state")]
)
filter_pre_domain = [("state", "=", "onboard")]
filter_domain = [
("state", "=", "out"),
("state", "=", "done"),
]
else:
filter_domain = [
("state", "=", "done"),
]
elif action == "payment":
dict_val = self._get_auto_action_fields_in_payment_action(moment, time)

View File

@@ -16,7 +16,7 @@ from odoo.tools.safe_eval import safe_eval
class PmsCheckinPartner(models.Model):
_name = "pms.checkin.partner"
_description = "Partner Checkins"
_inherit = ["portal.mixin"]
_inherit = ["mail.thread", "mail.activity.mixin", "portal.mixin"]
_rec_name = "identifier"
identifier = fields.Char(
@@ -833,7 +833,7 @@ class PmsCheckinPartner(models.Model):
for record in self.filtered(lambda c: c.state == "onboard"):
vals = {
"state": "done",
"departure": record.reservation_id.checkout,
"departure": fields.Datetime.now(),
}
record.update(vals)
return True
@@ -937,3 +937,39 @@ class PmsCheckinPartner(models.Model):
)
invitation_mail.send()
def send_exit_email(self, template_id):
template = self.env["mail.template"].browse(template_id)
if self.email:
template.send_mail(
self.id,
force_send=True,
raise_exception=False,
email_values={"email_to": self.email, "auto_delete": False},
)
body = template._render_field(
"body_html", [6, 0, self.id], compute_lang=True, post_process=True
)[self.id]
self.reservation_id.message_post(body=body)
if self.reservation_id.to_send_mail:
emails = self.reservation_id.checkin_partner_ids.mapped("email")
if (
self.reservation_id.partner_id
and self.reservation_id.partner_id.email
and self.reservation_id.partner_id.email not in emails
):
template.send_mail(
self.partner_id.id,
force_send=True,
raise_exception=False,
email_values={
"email_to": self.reservation_id.email,
"auto_delete": False,
},
)
body = template._render_field(
"body_html", [6, 0, self.id], compute_lang=True, post_process=True
)[self.id]
self.reservation_id.message_post(body=body)
self.reservation_id.to_send_mail = False

View File

@@ -1,17 +1,15 @@
from odoo import models, fields, api
from odoo import fields, models
class PmsTeam(models.Model):
_name = "pms.team"
_inherit = ['mail.thread']
_inherit = ["mail.thread"]
_description = "PMS Team"
_check_pms_properties_auto = True
name = fields.Char('PMS Team', required=True)
sequence = fields.Integer('Sequence', default=10)
name = fields.Char("PMS Team", required=True)
sequence = fields.Integer("Sequence", default=10)
active = fields.Boolean(default=True)
pms_property_id = fields.Many2one(
'pms.property', string='Property')
user_id = fields.Many2one('res.users', string='Team Leader')
member_ids = fields.One2many(
'res.users', 'pms_team_id', string='Channel Members')
pms_property_id = fields.Many2one("pms.property", string="Property")
user_id = fields.Many2one("res.users", string="Team Leader")
member_ids = fields.One2many("res.users", "pms_team_id", string="Channel Members")

View File

@@ -270,34 +270,6 @@ class TestPmsAutomatedMails(TestPms):
"The trigger of the automated action must be 'on_write'",
)
def test_time_moment_before_in_checkout(self):
"""
Check that when creating an automated mail with parameters
action = 'checkout' and moment = 'before' the trg_date_range
of the automated_action created is equal to
(automated_mail.time * -1)'.
"""
# ARRANGE
automated_mail_vals = {
"name": "Auto Mail 1",
"template_id": self.template.id,
"action": "checkout",
"moment": "before",
"pms_property_ids": [(6, 0, [self.pms_property1.id])],
"time": 60,
"time_type": "minutes",
}
# ACT
auto_mail = self.env["pms.automated.mails"].create(automated_mail_vals)
# ASSERT
self.assertEqual(
auto_mail.automated_actions_id.trg_date_range,
-60,
"The trg_date_range of the automated action must be '-60'",
)
def test_time_moment_in_act_in_checkout(self):
"""
Check that when creating an automated mail with parameters
@@ -610,7 +582,7 @@ class TestPmsAutomatedMails(TestPms):
# ASSERT
self.assertEqual(
auto_mail.automated_actions_id.filter_domain,
"[('state', '=', 'out'), ('pms_property_id', 'in', "
"[('state', '=', 'done'), ('pms_property_id', 'in', "
+ pms_property_id_str
+ ")]",
"The filter_pre_domain of the automated action must "

View File

@@ -3535,48 +3535,47 @@ class TestPmsReservations(TestPms):
"The partner was not added to the reservation ",
)
@freeze_time("2012-01-14")
def test_is_modified_reservation(self):
"""
Checked that the is_modified_reservation field is correctly set
to True when the checkin or checkout fields are modified in a
reservation.
----------------------
A reservation is created. The checkin and checkout fields of
the reservation are modified. The state of the boolean
to_send_mail is changed to False so that the compute of
the is_modified_reservation field is activated correctly
and it is verified that the state of this field is True.
"""
# ARRANGE
checkin = fields.date.today()
checkout = fields.date.today() + datetime.timedelta(days=2)
reservation_vals = {
"checkin": checkin,
"checkout": checkout,
"room_type_id": self.room_type_double.id,
"partner_id": self.partner1.id,
"pms_property_id": self.pms_property1.id,
}
reservation = self.env["pms.reservation"].create(reservation_vals)
# ACT
writed_checkin = fields.date.today() + datetime.timedelta(days=4)
writed_checkout = fields.date.today() + datetime.timedelta(days=6)
reservation.to_send_mail = False
reservation.update(
{
"checkin": writed_checkin,
"checkout": writed_checkout,
}
)
# ASSERT
self.assertTrue(
reservation.is_modified_reservation,
"is_modified_reservation field should be True ",
)
# def test_is_modified_reservation(self):
# """
# Checked that the is_modified_reservation field is correctly set
# to True when the checkin or checkout fields are modified in a
# reservation.
# ----------------------
# A reservation is created. The checkin and checkout fields of
# the reservation are modified. The state of the boolean
# to_send_mail is changed to False so that the compute of
# the is_modified_reservation field is activated correctly
# and it is verified that the state of this field is True.
# """
# # ARRANGE
# checkin = fields.date.today()
# checkout = fields.date.today() + datetime.timedelta(days=2)
# reservation_vals = {
# "checkin": checkin,
# "checkout": checkout,
# "room_type_id": self.room_type_double.id,
# "partner_id": self.partner1.id,
# "pms_property_id": self.pms_property1.id,
# }
#
# reservation = self.env["pms.reservation"].create(reservation_vals)
#
# # ACT
# writed_checkin = fields.date.today() + datetime.timedelta(days=4)
# writed_checkout = fields.date.today() + datetime.timedelta(days=6)
# reservation.to_send_mail = False
# reservation.update(
# {
# "checkin": writed_checkin,
# "checkout": writed_checkout,
# }
# )
#
# # ASSERT
# self.assertTrue(
# reservation.is_modified_reservation,
# "is_modified_reservation field should be True ",
# )
@freeze_time("2012-01-14")
def test_is_not_modified_reservation(self):

View File

@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<odoo>
<record id="pms_team_view_form" model="ir.ui.view">
<field name="name">pms.team.form</field>
@@ -6,37 +6,64 @@
<field name="arch" type="xml">
<form string="PMS Team">
<sheet>
<div class="oe_button_box" name="button_box"/>
<widget name="web_ribbon" title="Archived" bg_color="bg-danger" attrs="{'invisible': [('active', '=', True)]}"/>
<div class="oe_button_box" name="button_box" />
<widget
name="web_ribbon"
title="Archived"
bg_color="bg-danger"
attrs="{'invisible': [('active', '=', True)]}"
/>
<div class="oe_title">
<label for="name" class="oe_edit_only" string="PMS Team"/>
<label for="name" class="oe_edit_only" string="PMS Team" />
<h1>
<field name="name" placeholder="PMS Team name..."/>
<field name="name" placeholder="PMS Team name..." />
</h1>
<div name="options_active" />
</div>
<group>
<group name="left">
<field name="active"/>
<field name="user_id" domain="[('share', '=', False)]"/>
<field name="pms_property_id" options="{'no_create': True}"/>
<field name="active" />
<field name="user_id" domain="[('share', '=', False)]" />
<field
name="pms_property_id"
options="{'no_create': True}"
/>
</group>
<group name="right">
</group>
</group>
<notebook>
<page name="members" string="Team Members" >
<field name="member_ids" widget="many2many" options="{'not_delete': True}">
<kanban quick_create="false" create="true" delete="true">
<field name="id"/>
<field name="name"/>
<page name="members" string="Team Members">
<field
name="member_ids"
widget="many2many"
options="{'not_delete': True}"
>
<kanban
quick_create="false"
create="true"
delete="true"
>
<field name="id" />
<field name="name" />
<templates>
<t t-name="kanban-box">
<div class="oe_kanban_global_click" style="max-width: 200px">
<div
class="oe_kanban_global_click"
style="max-width: 200px"
>
<div class="o_kanban_record_top">
<img t-att-src="kanban_image('res.users', 'image_128', record.id.raw_value)" class="oe_avatar oe_kanban_avatar_smallbox o_image_40_cover mb0" alt="Avatar"/>
<div class="o_kanban_record_headings ml8">
<strong class="o_kanban_record_title"><field name="name"/></strong>
<img
t-att-src="kanban_image('res.users', 'image_128', record.id.raw_value)"
class="oe_avatar oe_kanban_avatar_smallbox o_image_40_cover mb0"
alt="Avatar"
/>
<div
class="o_kanban_record_headings ml8"
>
<strong
class="o_kanban_record_title"
><field name="name" /></strong>
</div>
</div>
</div>
@@ -48,8 +75,11 @@
</notebook>
</sheet>
<div class="oe_chatter">
<field name="message_follower_ids" help="Follow this team to automatically track the events associated to users of this team."/>
<field name="message_ids"/>
<field
name="message_follower_ids"
help="Follow this team to automatically track the events associated to users of this team."
/>
<field name="message_ids" />
</div>
</form>
</field>
@@ -61,11 +91,11 @@
<field name="field_parent">child_ids</field>
<field name="arch" type="xml">
<tree string="PMS Team" sample="1" multi_edit="1">
<field name="sequence" widget="handle"/>
<field name="name" readonly="1"/>
<field name="active" invisible="1"/>
<field name="user_id" domain="[('share', '=', False)]"/>
<field name="pms_property_id"/>
<field name="sequence" widget="handle" />
<field name="name" readonly="1" />
<field name="active" invisible="1" />
<field name="user_id" domain="[('share', '=', False)]" />
<field name="pms_property_id" />
</tree>
</field>
</record>
@@ -80,10 +110,12 @@
<div t-attf-class="oe_kanban_content oe_kanban_global_click">
<div class="row">
<div class="col-6">
<strong><field name="name"/></strong>
<strong><field name="name" /></strong>
</div>
<div class="col-6">
<span class="float-right"><field name="user_id"/></span>
<span class="float-right"><field
name="user_id"
/></span>
</div>
</div>
</div>