1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4from odoo import api, fields, models
5
6
7class MailTestSimple(models.Model):
8    """ A very simple model only inheriting from mail.thread when only
9    communication history is necessary. """
10    _description = 'Simple Chatter Model'
11    _name = 'mail.test.simple'
12    _inherit = ['mail.thread']
13
14    name = fields.Char()
15    email_from = fields.Char()
16
17
18class MailTestGateway(models.Model):
19    """ A very simple model only inheriting from mail.thread to test pure mass
20    mailing features and base performances. """
21    _description = 'Simple Chatter Model for Mail Gateway'
22    _name = 'mail.test.gateway'
23    _inherit = ['mail.thread.blacklist']
24    _primary_email = 'email_from'
25
26    name = fields.Char()
27    email_from = fields.Char()
28    custom_field = fields.Char()
29
30
31class MailTestStandard(models.Model):
32    """ This model can be used in tests when automatic subscription and simple
33    tracking is necessary. Most features are present in a simple way. """
34    _description = 'Standard Chatter Model'
35    _name = 'mail.test.track'
36    _inherit = ['mail.thread']
37
38    name = fields.Char()
39    email_from = fields.Char()
40    user_id = fields.Many2one('res.users', 'Responsible', tracking=True)
41    container_id = fields.Many2one('mail.test.container', tracking=True)
42    company_id = fields.Many2one('res.company')
43
44
45class MailTestActivity(models.Model):
46    """ This model can be used to test activities in addition to simple chatter
47    features. """
48    _description = 'Activity Model'
49    _name = 'mail.test.activity'
50    _inherit = ['mail.thread', 'mail.activity.mixin']
51
52    name = fields.Char()
53    date = fields.Date()
54    email_from = fields.Char()
55    active = fields.Boolean(default=True)
56
57    def action_start(self, action_summary):
58        return self.activity_schedule(
59            'test_mail.mail_act_test_todo',
60            summary=action_summary
61        )
62
63    def action_close(self, action_feedback):
64        self.activity_feedback(['test_mail.mail_act_test_todo'], feedback=action_feedback)
65
66
67class MailTestTicket(models.Model):
68    """ This model can be used in tests when complex chatter features are
69    required like modeling tasks or tickets. """
70    _description = 'Ticket-like model'
71    _name = 'mail.test.ticket'
72    _inherit = ['mail.thread']
73
74    name = fields.Char()
75    email_from = fields.Char(tracking=True)
76    count = fields.Integer(default=1)
77    datetime = fields.Datetime(default=fields.Datetime.now)
78    mail_template = fields.Many2one('mail.template', 'Template')
79    customer_id = fields.Many2one('res.partner', 'Customer', tracking=2)
80    user_id = fields.Many2one('res.users', 'Responsible', tracking=1)
81    container_id = fields.Many2one('mail.test.container', tracking=True)
82
83    def _track_template(self, changes):
84        res = super(MailTestTicket, self)._track_template(changes)
85        record = self[0]
86        if 'customer_id' in changes and record.mail_template:
87            res['customer_id'] = (record.mail_template, {'composition_mode': 'mass_mail'})
88        elif 'datetime' in changes:
89            res['datetime'] = ('test_mail.mail_test_ticket_tracking_view', {'composition_mode': 'mass_mail'})
90        return res
91
92    def _creation_subtype(self):
93        if self.container_id:
94            return self.env.ref('test_mail.st_mail_test_ticket_container_upd')
95        return super(MailTestTicket, self)._creation_subtype()
96
97    def _track_subtype(self, init_values):
98        self.ensure_one()
99        if 'container_id' in init_values and self.container_id:
100            return self.env.ref('test_mail.st_mail_test_ticket_container_upd')
101        return super(MailTestTicket, self)._track_subtype(init_values)
102
103
104class MailTestContainer(models.Model):
105    """ This model can be used in tests when container records like projects
106    or teams are required. """
107    _description = 'Project-like model with alias'
108    _name = 'mail.test.container'
109    _mail_post_access = 'read'
110    _inherit = ['mail.thread', 'mail.alias.mixin']
111
112    name = fields.Char()
113    description = fields.Text()
114    customer_id = fields.Many2one('res.partner', 'Customer')
115    alias_id = fields.Many2one(
116        'mail.alias', 'Alias',
117        delegate=True)
118
119    def _alias_get_creation_values(self):
120        values = super(MailTestContainer, self)._alias_get_creation_values()
121        values['alias_model_id'] = self.env['ir.model']._get('mail.test.container').id
122        if self.id:
123            values['alias_force_thread_id'] = self.id
124            values['alias_parent_thread_id'] = self.id
125        return values
126