File: one2one.py

package info (click to toggle)
tryton-server 2.2.4-1%2Bdeb7u2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,500 kB
  • sloc: python: 27,407; xml: 4,424; sql: 510; sh: 127; makefile: 79
file content (50 lines) | stat: -rw-r--r-- 1,623 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
#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.fields.many2many import Many2Many
from trytond.transaction import Transaction
from trytond.pool import Pool


class One2One(Many2Many):
    '''
    Define one2one field (``int``).
    '''
    _type = 'one2one'

    def get(self, ids, model, name, values=None):
        '''
        Return target record.

        :param ids: a list of ids
        :param model: a string with the name of the model
        :param name: a string with the name of the field
        :param values: a dictionary with the read values
        :return: a dictionary with ids as key and target id as value
        '''
        res = super(One2One, self).get(ids, model, name, values=values)
        for i, vals in res.iteritems():
            res[i] = vals[0] if vals else False
        return res

    def set(self, ids, model, name, value):
        '''
        Set the values.

        :param ids: A list of ids
        :param model: A string with the name of the model
        :param name: A string with the name of the field
        :param value: The id to link
        '''
        pool = Pool()
        relation_obj = pool.get(self.relation_name)
        relation_ids = relation_obj.search([
            (self.origin, 'in', ids),
            ])
        relation_obj.delete(relation_ids)
        if value:
            for record_id in ids:
                relation_obj.create({
                    self.origin: record_id,
                    self.target: value,
                    })