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
|
# This file is part of Tryton. The COPYRIGHT file at the toplevel of this
# repository contains the full copyright notices and license terms.
import functools
from trytond.model import MultiValueMixin, ValueMixin, fields
from trytond.pool import Pool
from trytond.pyson import Eval
from trytond.transaction import Transaction
__all__ = ['CompanyMultiValueMixin', 'CompanyValueMixin',
'set_employee', 'reset_employee', 'employee_field']
class CompanyMultiValueMixin(MultiValueMixin):
__slots__ = ()
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
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, save=True, **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, save=save, **pattern)
class CompanyValueMixin(ValueMixin):
__slots__ = ()
company = fields.Many2One('company.company', "Company", ondelete='CASCADE')
def employee_field(string, states=None, company='company'):
if states is None:
states = ['done', 'cancel', 'cancelled']
return fields.Many2One(
'company.employee', string,
domain=[('company', '=', Eval(company, -1))],
states={
'readonly': Eval('state').in_(states),
})
def set_employee(field, company='company', when='after'):
def decorator(func):
@functools.wraps(func)
def wrapper(cls, records, *args, **kwargs):
pool = Pool()
User = pool.get('res.user')
user = User(Transaction().user)
if when == 'after':
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,
})
if when == 'before':
result = func(cls, records, *args, **kwargs)
return result
return wrapper
assert when in {'before', 'after'}
return decorator
def reset_employee(*fields, when='after'):
def decorator(func):
@functools.wraps(func)
def wrapper(cls, records, *args, **kwargs):
if when == 'after':
result = func(cls, records, *args, **kwargs)
cls.write(records, {f: None for f in fields})
if when == 'before':
result = func(cls, records, *args, **kwargs)
return result
return wrapper
assert when in {'before', 'after'}
return decorator
|