Files
tryton/model.py
T

91 lines
3.1 KiB
Python
Raw Normal View History

2017-04-01 00:30:53 +02:00
# This file is part of Tryton. The COPYRIGHT file at the toplevel of this
# repository contains the full copyright notices and license terms.
2020-04-10 18:53:44 +02:00
import functools
2017-04-01 00:30:53 +02:00
from trytond.model import MultiValueMixin, ValueMixin, fields
2020-04-10 18:53:44 +02:00
from trytond.pool import Pool
from trytond.pyson import Eval
2017-04-01 00:30:53 +02:00
from trytond.transaction import Transaction
2020-04-10 18:53:44 +02:00
__all__ = ['CompanyMultiValueMixin', 'CompanyValueMixin',
'set_employee', 'reset_employee', 'employee_field']
2017-04-01 00:30:53 +02:00
class CompanyMultiValueMixin(MultiValueMixin):
2019-09-07 23:45:08 +02:00
__slots__ = ()
2017-04-01 00:30:53 +02:00
def multivalue_records(self, field):
Value = self.multivalue_model(field)
records = super(CompanyMultiValueMixin, self).multivalue_records(field)
if issubclass(Value, CompanyValueMixin):
# Sort to get record with empty company at the end
# and so give priority to record with company filled.
records = sorted(records, key=lambda r: r.company is None)
return records
2017-04-01 00:30:53 +02:00
def get_multivalue(self, name, **pattern):
Value = self.multivalue_model(name)
if issubclass(Value, CompanyValueMixin):
pattern.setdefault('company', Transaction().context.get('company'))
return super(CompanyMultiValueMixin, self).get_multivalue(
name, **pattern)
def set_multivalue(self, name, value, **pattern):
Value = self.multivalue_model(name)
if issubclass(Value, CompanyValueMixin):
pattern.setdefault('company', Transaction().context.get('company'))
return super(CompanyMultiValueMixin, self).set_multivalue(
name, value, **pattern)
class CompanyValueMixin(ValueMixin):
2019-09-07 23:45:08 +02:00
__slots__ = ()
2017-04-01 00:30:53 +02:00
company = fields.Many2One(
'company.company', "Company", select=True, ondelete='CASCADE')
2020-04-10 18:53:44 +02:00
def employee_field(string, states=None, company='company'):
if states is None:
states = ['done', 'cancel', 'cancelled']
2020-04-10 18:53:44 +02:00
return fields.Many2One(
'company.employee', string,
domain=[('company', '=', Eval(company, -1))],
states={
'readonly': Eval('state').in_(states),
},
depends=[company, 'state'])
def set_employee(field, company='company'):
def decorator(func):
@functools.wraps(func)
def wrapper(cls, records, *args, **kwargs):
pool = Pool()
User = pool.get('res.user')
user = User(Transaction().user)
result = func(cls, records, *args, **kwargs)
employee = user.employee
if employee:
emp_company = employee.company
cls.write(
[r for r in records
if not getattr(r, field)
and getattr(r, company) == emp_company], {
field: employee.id,
})
return result
return wrapper
return decorator
def reset_employee(*fields):
def decorator(func):
@functools.wraps(func)
def wrapper(cls, records, *args, **kwargs):
result = func(cls, records, *args, **kwargs)
cls.write(records, {f: None for f in fields})
return result
return wrapper
return decorator