File: res.py

package info (click to toggle)
tryton-modules-company 7.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 636 kB
  • sloc: python: 1,113; xml: 349; makefile: 11; sh: 3
file content (285 lines) | stat: -rw-r--r-- 10,236 bytes parent folder | download
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# 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 sql import Null

from trytond.cache import Cache
from trytond.model import ModelSQL, fields
from trytond.pool import Pool, PoolMeta
from trytond.pyson import Eval
from trytond.transaction import Transaction


class UserCompany(ModelSQL):
    "User - Company"
    __name__ = 'res.user-company.company'

    user = fields.Many2One(
        'res.user', "User", ondelete='CASCADE', required=True)
    company = fields.Many2One(
        'company.company', "Company",
        ondelete='CASCADE', required=True)

    @classmethod
    def create(cls, vlist):
        pool = Pool()
        User = pool.get('res.user')
        records = super().create(vlist)
        User._get_companies_cache.clear()
        return records

    @classmethod
    def write(cls, *args):
        pool = Pool()
        User = pool.get('res.user')
        super().write(*args)
        User._get_companies_cache.clear()

    @classmethod
    def delete(cls, records):
        pool = Pool()
        User = pool.get('res.user')
        super().delete(records)
        User._get_companies_cache.clear()


class UserEmployee(ModelSQL):
    'User - Employee'
    __name__ = 'res.user-company.employee'
    user = fields.Many2One(
        'res.user', "User", ondelete='CASCADE', required=True)
    employee = fields.Many2One(
        'company.employee', "Employee", ondelete='CASCADE', required=True)

    @classmethod
    def create(cls, vlist):
        pool = Pool()
        User = pool.get('res.user')
        records = super().create(vlist)
        User._get_employees_cache.clear()
        return records

    @classmethod
    def write(cls, *args):
        pool = Pool()
        User = pool.get('res.user')
        super().write(*args)
        User._get_employees_cache.clear()

    @classmethod
    def delete(cls, records):
        pool = Pool()
        User = pool.get('res.user')
        super().delete(records)
        User._get_employees_cache.clear()


class User(metaclass=PoolMeta):
    __name__ = 'res.user'

    companies = fields.Many2Many(
        'res.user-company.company', 'user', 'company', "Companies",
        help="The companies that the user has access to.")
    company = fields.Many2One(
        'company.company', "Current Company",
        domain=[
            ('id', 'in', Eval('companies', [])),
            ],
        help="Select the company to work for.")
    employees = fields.Many2Many('res.user-company.employee', 'user',
        'employee', 'Employees',
        domain=[
            ('company', 'in', Eval('companies', [])),
            ],
        help="Add employees to grant the user access to them.")
    employee = fields.Many2One('company.employee', 'Current Employee',
        domain=[
            ('company', '=', Eval('company', -1)),
            ('id', 'in', Eval('employees', [])),
            ],
        help="Select the employee to make the user behave as such.")
    company_filter = fields.Selection([
            ('one', "Current"),
            ('all', "All"),
            ], "Company Filter",
        help="Define records of which companies are shown.")
    _get_companies_cache = Cache(__name__ + '.get_companies', context=False)
    _get_employees_cache = Cache(__name__ + '.get_employees', context=False)

    @classmethod
    def __setup__(cls):
        super(User, cls).__setup__()
        cls._context_fields.insert(0, 'company')
        cls._context_fields.insert(0, 'employee')
        cls._context_fields.insert(0, 'company_filter')

    @classmethod
    def __register__(cls, module):
        pool = Pool()
        UserCompany = pool.get('res.user-company.company')
        transaction = Transaction()
        table = cls.__table__()
        user_company = UserCompany.__table__()

        super().__register__(module)

        table_h = cls.__table_handler__(module)
        cursor = transaction.connection.cursor()

        # Migration from 5.8: remove main_company
        if table_h.column_exist('main_company'):
            cursor.execute(*user_company.insert(
                    [user_company.user, user_company.company],
                    table.select(
                        table.id, table.main_company,
                        where=table.main_company != Null)))
            cursor.execute(*user_company.insert(
                    [user_company.user, user_company.company],
                    table.select(
                        table.id, table.company,
                        where=(table.company != Null)
                        & (table.company != table.main_company))))
            table_h.drop_column('main_company')

    @classmethod
    def default_companies(cls):
        company = Transaction().context.get('company')
        return [company] if company else []

    @classmethod
    def default_company(cls):
        return Transaction().context.get('company')

    @classmethod
    def default_company_filter(cls):
        return 'one'

    def get_status_bar(self, name):
        def same_company(record):
            return record.company == self.company
        status = super(User, self).get_status_bar(name)
        if (self.employee
                and len(list(filter(same_company, self.employees))) > 1):
            status += ' - %s' % self.employee.rec_name
        if self.company:
            if len(self.companies) > 1:
                status += ' - %s' % self.company.rec_name
            status += ' [%s]' % self.company.currency.code
        return status

    @fields.depends('company', 'employees')
    def on_change_company(self):
        Employee = Pool().get('company.employee')
        self.employee = None
        if self.company and self.employees:
            employees = Employee.search([
                    ('id', 'in', [e.id for e in self.employees]),
                    ('company', '=', self.company.id),
                    ])
            if employees:
                self.employee = employees[0]

    @classmethod
    def _get_preferences(cls, user, context_only=False):
        res = super(User, cls)._get_preferences(user,
            context_only=context_only)
        if not context_only:
            res['companies'] = [c.id for c in user.companies]
            res['employees'] = [e.id for e in user.employees]
        return res

    @classmethod
    def get_companies(cls):
        '''
        Return an ordered tuple of company ids for the user
        '''
        transaction = Transaction()
        user_id = transaction.user
        companies = cls._get_companies_cache.get(user_id)
        if companies is not None:
            return companies
        with transaction.set_user(0):
            user = cls(user_id)
        if user.company_filter == 'one':
            companies = [user.company.id] if user.company else []
        elif user.company_filter == 'all':
            companies = [c.id for c in user.companies]
        else:
            companies = []
        companies = tuple(companies)
        cls._get_companies_cache.set(user_id, companies)
        return companies

    @classmethod
    def get_employees(cls):
        '''
        Return an ordered tuple of employee ids for the user
        '''
        transaction = Transaction()
        user_id = transaction.user
        employees = cls._get_employees_cache.get(user_id)
        if employees is not None:
            return employees
        with transaction.set_user(0):
            user = cls(user_id)
        if user.company_filter == 'one':
            employees = [user.employee.id] if user.employee else []
        elif user.company_filter == 'all':
            employees = [e.id for e in user.employees]
        else:
            employees = []
        employees = tuple(employees)
        cls._get_employees_cache.set(user_id, employees)
        return employees

    @classmethod
    def read(cls, ids, fields_names):
        user_id = Transaction().user
        if user_id == 0 and 'user' in Transaction().context:
            user_id = Transaction().context['user']
        result = super(User, cls).read(ids, fields_names)
        if (fields_names
                and ((
                        'company' in fields_names
                        and 'company' in Transaction().context)
                    or ('employee' in fields_names
                        and 'employee' in Transaction().context))):
            values = None
            if int(user_id) in ids:
                for vals in result:
                    if vals['id'] == int(user_id):
                        values = vals
                        break
            if values:
                if ('company' in fields_names
                        and 'company' in Transaction().context):
                    companies = values.get('companies')
                    if not companies:
                        companies = cls.read([user_id],
                            ['companies'])[0]['companies']
                    company_id = Transaction().context['company']
                    if ((company_id and company_id in companies)
                            or not company_id
                            or Transaction().user == 0):
                        values['company'] = company_id
                    else:
                        values['company'] = None
                if ('employee' in fields_names
                        and 'employee' in Transaction().context):
                    employees = values.get('employees')
                    if not employees:
                        employees = cls.read([user_id],
                            ['employees'])[0]['employees']
                    employee_id = Transaction().context['employee']
                    if ((employee_id and employee_id in employees)
                            or not employee_id
                            or Transaction().user == 0):
                        values['employee'] = employee_id
                    else:
                        values['employee'] = None
        return result

    @classmethod
    def write(cls, *args):
        super().write(*args)
        cls._get_companies_cache.clear()
        cls._get_employees_cache.clear()