This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ir.py
150 lines (125 loc) · 4.89 KB
/
ir.py
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
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.model import (
EvalEnvironment, ModelSQL, ModelView, dualmethod, fields)
from trytond.pool import Pool, PoolMeta
from trytond.tools import timezone as tz
from trytond.transaction import Transaction
class Sequence(metaclass=PoolMeta):
__name__ = 'ir.sequence'
company = fields.Many2One(
'company.company', "Company",
help="Restricts the sequence usage to the company.")
@classmethod
def __setup__(cls):
super(Sequence, cls).__setup__()
cls._order.insert(0, ('company', 'ASC'))
@staticmethod
def default_company():
return Transaction().context.get('company')
class SequenceStrict(Sequence):
__name__ = 'ir.sequence.strict'
class Date(metaclass=PoolMeta):
__name__ = 'ir.date'
@classmethod
def today(cls, timezone=None):
pool = Pool()
Company = pool.get('company.company')
company_id = Transaction().context.get('company')
if timezone is None and company_id:
company = Company(company_id)
if company.timezone:
timezone = tz.ZoneInfo(company.timezone)
return super(Date, cls).today(timezone=timezone)
class Rule(metaclass=PoolMeta):
__name__ = 'ir.rule'
@classmethod
def __setup__(cls):
super().__setup__()
cls.domain.help += (
'\n- "employee" from the current user'
'\n- "employees" as list of ids from the current user'
'\n- "companies" as list of ids from the current user')
@classmethod
def _get_cache_key(cls):
key = super(Rule, cls)._get_cache_key()
# XXX Use company from context instead of browse to prevent infinite
# loop, but the cache is cleared when User is written.
context = Transaction().context
return key + (
context.get('company'),
context.get('employee'),
context.get('company_filter'),
)
@classmethod
def _get_context(cls):
pool = Pool()
User = pool.get('res.user')
Employee = pool.get('company.employee')
context = super()._get_context()
# Use root to avoid infinite loop when accessing user attributes
user_id = Transaction().user
with Transaction().set_user(0):
user = User(user_id)
if user.employee:
with Transaction().set_context(
_check_access=False, _datetime=None):
context['employee'] = EvalEnvironment(
Employee(user.employee.id), Employee)
if user.company_filter == 'one':
context['companies'] = [user.company.id] if user.company else []
context['employees'] = [user.employee.id] if user.employee else []
elif user.company_filter == 'all':
context['companies'] = [c.id for c in user.companies]
context['employees'] = [e.id for e in user.employees]
else:
context['companies'] = []
context['employees'] = []
return context
class Cron(metaclass=PoolMeta):
__name__ = "ir.cron"
companies = fields.Many2Many('ir.cron-company.company', 'cron', 'company',
'Companies', help='Companies registered for this cron.')
@dualmethod
@ModelView.button
def run_once(cls, crons):
for cron in crons:
if not cron.companies:
super(Cron, cls).run_once([cron])
else:
for company in cron.companies:
with Transaction().set_context(company=company.id):
super(Cron, cls).run_once([cron])
@staticmethod
def default_companies():
Company = Pool().get('company.company')
return list(map(int, Company.search([])))
class CronCompany(ModelSQL):
'Cron - Company'
__name__ = 'ir.cron-company.company'
_table = 'cron_company_rel'
cron = fields.Many2One(
'ir.cron', "Cron", ondelete='CASCADE', required=True)
company = fields.Many2One(
'company.company', "Company", ondelete='CASCADE', required=True)
class EmailTemplate(metaclass=PoolMeta):
__name__ = 'ir.email.template'
@classmethod
def email_models(cls):
return super().email_models() + ['company.employee']
@classmethod
def _get_address(cls, record):
pool = Pool()
Employee = pool.get('company.employee')
address = super()._get_address(record)
if isinstance(record, Employee):
address = cls._get_address(record.party)
return address
@classmethod
def _get_language(cls, record):
pool = Pool()
Employee = pool.get('company.employee')
language = super()._get_language(record)
if isinstance(record, Employee):
language = cls._get_language(record.party)
return language