~crass/tryton/server

« back to all changes in this revision

Viewing changes to trytond/model/fields/selection.py

  • Committer: Mathias Behrle
  • Date: 2013-11-24 16:28:54 UTC
  • Revision ID: git-v1:182d6cce169eab1682eeacbad4323efa1136a1a0
MergingĀ upstreamĀ versionĀ 3.0.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#This file is part of Tryton.  The COPYRIGHT file at the top level of
2
2
#this repository contains the full copyright notices and license terms.
 
3
from sql import Column
 
4
from sql.conditionals import Case
3
5
 
4
 
from trytond.model.fields.field import Field
 
6
from ...config import CONFIG
 
7
from .field import Field, SQLType
5
8
 
6
9
 
7
10
class Selection(Field):
14
17
            selection_change_with=None, translate=True, help='',
15
18
            required=False, readonly=False, domain=None, states=None,
16
19
            select=False, on_change=None, on_change_with=None, depends=None,
17
 
            order_field=None, context=None, loading='eager'):
 
20
            context=None, loading='eager'):
18
21
        '''
19
22
        :param selection: A list or a function name that returns a list.
20
23
            The list must be a list of tuples. First member is the value
24
27
        super(Selection, self).__init__(string=string, help=help,
25
28
            required=required, readonly=readonly, domain=domain, states=states,
26
29
            select=select, on_change=on_change, on_change_with=on_change_with,
27
 
            depends=depends, order_field=order_field, context=context,
28
 
            loading=loading)
 
30
            depends=depends, context=context, loading=loading)
29
31
        if hasattr(selection, 'copy'):
30
32
            self.selection = selection.copy()
31
33
        else:
34
36
        self.sort = sort
35
37
        self.translate_selection = translate
36
38
    __init__.__doc__ += Field.__init__.__doc__
 
39
 
 
40
    def sql_type(self):
 
41
        db_type = CONFIG['db_type']
 
42
        if db_type == 'mysql':
 
43
            return SQLType('CHAR', 'VARCHAR(255)')
 
44
        return SQLType('VARCHAR', 'VARCHAR')
 
45
 
 
46
    def convert_order(self, name, tables, Model):
 
47
        if getattr(Model, 'order_%s' % name, None):
 
48
            return super(Selection, self).convert_order(name, tables, Model)
 
49
 
 
50
        table, _ = tables[None]
 
51
        selections = Model.fields_get([name])[name]['selection']
 
52
        if not isinstance(selections, (tuple, list)):
 
53
            selections = getattr(Model, selections)()
 
54
        column = Column(table, name)
 
55
        whens = []
 
56
        for key, value in selections:
 
57
            whens.append((column == key, value))
 
58
        return [Case(*whens, else_=column)]