1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from dateutil import relativedelta
from odoo import fields, models, api
class LeadTest(models.Model):
_name = "base.automation.lead.test"
_description = "Automated Rule Test"
name = fields.Char(string='Subject', required=True)
user_id = fields.Many2one('res.users', string='Responsible')
state = fields.Selection([('draft', 'New'), ('cancel', 'Cancelled'), ('open', 'In Progress'),
('pending', 'Pending'), ('done', 'Closed')],
string="Status", readonly=True, default='draft')
active = fields.Boolean(default=True)
partner_id = fields.Many2one('res.partner', string='Partner')
date_automation_last = fields.Datetime(string='Last Automation', readonly=True)
employee = fields.Boolean(compute='_compute_employee_deadline', store=True)
line_ids = fields.One2many('base.automation.line.test', 'lead_id')
priority = fields.Boolean()
deadline = fields.Boolean(compute='_compute_employee_deadline', store=True)
is_assigned_to_admin = fields.Boolean(string='Assigned to admin user')
stage_id = fields.Many2one(
'test_base_automation.stage', string='Stage',
compute='_compute_stage_id', readonly=False, store=True)
@api.depends('state')
def _compute_stage_id(self):
Stage = self.env['test_base_automation.stage']
for task in self:
if not task.stage_id and task.state == 'draft':
task.stage_id = (
Stage.search([('name', 'ilike', 'new')], limit=1)
or Stage.create({'name': 'New'})
)
@api.depends('partner_id.employee', 'priority')
def _compute_employee_deadline(self):
# this method computes two fields on purpose; don't split it
for record in self:
record.employee = record.partner_id.employee
if not record.priority:
record.deadline = False
else:
record.deadline = record.create_date + relativedelta.relativedelta(days=3)
def write(self, vals):
result = super().write(vals)
# force recomputation of field 'deadline' via 'employee': the action
# based on 'deadline' must be triggered
self.mapped('employee')
return result
class LeadThreadTest(models.Model):
_name = "base.automation.lead.thread.test"
_description = "Automated Rule Test With Thread"
_inherit = ['base.automation.lead.test', 'mail.thread']
class LineTest(models.Model):
_name = "base.automation.line.test"
_description = "Automated Rule Line Test"
name = fields.Char()
lead_id = fields.Many2one('base.automation.lead.test', ondelete='cascade')
user_id = fields.Many2one('res.users')
class ModelWithAccess(models.Model):
_name = "base.automation.link.test"
_description = "Automated Rule Link Test"
name = fields.Char()
linked_id = fields.Many2one('base.automation.linked.test', ondelete='cascade')
class ModelWithoutAccess(models.Model):
_name = "base.automation.linked.test"
_description = "Automated Rule Linked Test"
name = fields.Char()
another_field = fields.Char()
class Project(models.Model):
_name = _description = 'test_base_automation.project'
name = fields.Char()
task_ids = fields.One2many('test_base_automation.task', 'project_id')
stage_id = fields.Many2one('test_base_automation.stage')
tag_ids = fields.Many2many('test_base_automation.tag')
priority = fields.Selection([('0', 'Low'), ('1', 'Normal'), ('2', 'High')], default='1')
user_ids = fields.Many2many('res.users')
class Task(models.Model):
_name = _description = 'test_base_automation.task'
name = fields.Char()
parent_id = fields.Many2one('test_base_automation.task')
project_id = fields.Many2one(
'test_base_automation.project',
compute='_compute_project_id', recursive=True, store=True, readonly=False,
)
@api.depends('parent_id.project_id')
def _compute_project_id(self):
for task in self:
if not task.project_id:
task.project_id = task.parent_id.project_id
class Stage(models.Model):
_name = _description = 'test_base_automation.stage'
name = fields.Char()
class Tag(models.Model):
_name = _description = 'test_base_automation.tag'
name = fields.Char()
class LeadThread(models.Model):
_inherit = ["base.automation.lead.test", "mail.thread"]
_name = "base.automation.lead.thread.test"
_description = "Threaded Lead Test"
class ModelWithCharRecName(models.Model):
_name = "base.automation.model.with.recname.char"
_description = "Model with Char as _rec_name"
_rec_name = "description"
description = fields.Char()
user_id = fields.Many2one('res.users', string='Responsible')
class ModelWithRecName(models.Model):
_name = "base.automation.model.with.recname.m2o"
_description = "Model with Many2one as _rec_name and name_create"
_rec_name = "user_id"
user_id = fields.Many2one("base.automation.model.with.recname.char", string='Responsible')
def name_create(self, name):
name = name.strip()
user = self.env["base.automation.model.with.recname.char"].search([('description', '=ilike', name)], limit=1)
if user:
user_id = user.id
else:
user_id, _user_name = self.env["base.automation.model.with.recname.char"].name_create(name)
record = self.create({'user_id': user_id})
return record.id, record.display_name
|