Files
tryton/model.py
T
Cédric Krier da0f75986f Sort multivalue records to get empty company at the end
The MultiValueMixin should give priority to records with company filled because
they are more specific and it is expected by the implementations.
We can not rely on the default order because ValueMixin is not necessary a
subclass of ModelSQL.

issue6464
review37361002
2017-04-29 10:39:06 +02:00

38 lines
1.5 KiB
Python

# This file is part of Tryton. The COPYRIGHT file at the toplevel of this
# repository contains the full copyright notices and license terms.
from trytond.model import MultiValueMixin, ValueMixin, fields
from trytond.transaction import Transaction
__all__ = ['CompanyMultiValueMixin', 'CompanyValueMixin']
class CompanyMultiValueMixin(MultiValueMixin):
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, **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):
company = fields.Many2One(
'company.company', "Company", select=True, ondelete='CASCADE')