root/trunk/camelot/view/field_attributes.py @ 1186

Revision 1186, 8.7 KB (checked in by erikj, 6 months ago)

prepare expanded search and sorting of query proxy models

Line 
1#  ============================================================================
2#
3#  Copyright (C) 2007-2008 Conceptive Engineering bvba. All rights reserved.
4#  www.conceptive.be / project-camelot@conceptive.be
5#
6#  This file is part of the Camelot Library.
7#
8#  This file may be used under the terms of the GNU General Public
9#  License version 2.0 as published by the Free Software Foundation
10#  and appearing in the file LICENSE.GPL included in the packaging of
11#  this file.  Please review the following information to ensure GNU
12#  General Public Licensing requirements will be met:
13#  http://www.trolltech.com/products/qt/opensource.html
14#
15#  If you are unsure which license is appropriate for your use, please
16#  review the following information:
17#  http://www.trolltech.com/products/qt/licensing.html or contact
18#  project-camelot@conceptive.be.
19#
20#  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
21#  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22#
23#  For use of this library in commercial applications, please contact
24#  project-camelot@conceptive.be
25#
26#  ============================================================================
27
28"""Default field attributes for various sqlalchemy column types"""
29
30import sqlalchemy.types
31import camelot.types
32import datetime
33import operator
34
35from controls import delegates
36from camelot.core import constants
37from camelot.view.utils import (
38    bool_from_string,
39    date_from_string,
40    time_from_string,
41    datetime_from_string,
42    int_from_string,
43    float_from_string,
44    string_from_string,
45    enumeration_to_string,
46)
47
48_numerical_operators = (operator.eq, operator.lt, operator.le, operator.gt, operator.ge)
49
50_sqlalchemy_to_python_type_ = {
51
52    sqlalchemy.types.Boolean: lambda f: {
53        'python_type': bool,
54        'editable': True,
55        'nullable': True,
56        'delegate': delegates.BoolDelegate,
57        'from_string': bool_from_string,
58        'operators' : (operator.eq,),
59    },
60
61    sqlalchemy.types.BOOLEAN: lambda f: {
62        'python_type': bool,
63        'editable': True,
64        'nullable': True,
65        'delegate': delegates.BoolDelegate,
66        'from_string': bool_from_string,
67        'operators' : (operator.eq,),
68    },
69
70    sqlalchemy.types.Date: lambda f: {
71        'python_type': datetime.date,
72        'format': constants.camelot_date_format,
73        'editable': True,
74        'min': None,
75        'max': None,
76        'nullable': True,
77        'delegate': delegates.DateDelegate,
78        'from_string': date_from_string,
79        'operators' : _numerical_operators,
80    },
81
82    sqlalchemy.types.Time : lambda f: {
83            'python_type': datetime.time,
84            'editable': True,
85            'nullable': True,
86            'widget': 'time',
87            'delegate': delegates.TimeDelegate,
88            'format': constants.camelot_time_format,
89            'nullable': True,
90            'from_string': time_from_string,
91            'operators': _numerical_operators,
92    },
93
94    sqlalchemy.types.DateTime : lambda f: {
95        'python_type': datetime.datetime,
96        'editable': True,
97        'nullable': True,
98        'widget': 'time',
99        'format': constants.camelot_datetime_format,
100        'nullable': True,
101        'delegate': delegates.DateTimeDelegate,
102        'from_string': datetime_from_string,
103        'operators': _numerical_operators,
104    },
105
106    sqlalchemy.types.Float: lambda f: {
107        'python_type': float,
108        'precision': f.precision if not isinstance(f.precision, tuple) else f.precision[1],
109        'editable': True,
110        'minimum': constants.camelot_minfloat,
111        'maximum': constants.camelot_maxfloat,
112        'nullable': True,
113        'delegate': delegates.FloatDelegate,
114        'from_string': float_from_string,
115        'operators': _numerical_operators,
116    },
117
118    sqlalchemy.types.Integer: lambda f: {
119        'python_type': int,
120        'editable': True,
121        'minimum': constants.camelot_minint,
122        'maximum': constants.camelot_maxint,
123        'nullable': True,
124        'delegate': delegates.IntegerDelegate,
125        'from_string': int_from_string,
126        'to_string': unicode,
127        'widget': 'int',
128        'operators': _numerical_operators,
129    },
130
131    sqlalchemy.types.INT: lambda f: {
132        'python_type': int,
133        'editable': True,
134        'minimum': constants.camelot_minint,
135        'maximum': constants.camelot_maxint,
136        'nullable': True,
137        'delegate': delegates.IntegerDelegate,
138        'from_string': int_from_string,
139        'widget': 'int',
140        'operators': _numerical_operators,
141    },
142
143    sqlalchemy.types.String: lambda f: {
144        'python_type': str,
145        'length': f.length,
146        'delegate': delegates.PlainTextDelegate,
147        'editable': True,
148        'nullable': True,
149        'widget': 'str',
150        'from_string': string_from_string,
151    },
152
153    sqlalchemy.types.TEXT: lambda f: {
154        'python_type': str,
155        'length': f.length,
156        'delegate': delegates.PlainTextDelegate,
157        'editable': True,
158        'nullable': True,
159        'widget': 'str',
160        'from_string': string_from_string,
161    },
162
163    sqlalchemy.types.Unicode: lambda f: {
164        'python_type': str,
165        'length': f.length,
166        'delegate': delegates.PlainTextDelegate,
167        'editable': True,
168        'nullable': True,
169        'widget': 'str',
170        'from_string': string_from_string,
171    },
172
173    camelot.types.Image: lambda f: {
174        'python_type': str,
175        'editable': True,
176        'nullable': True,
177        'delegate': delegates.ImageDelegate,
178        'storage': f.storage,
179        'preview_width': 100,
180        'preview_height': 100
181    },
182
183    camelot.types.Code: lambda f: {
184        'python_type': str,
185        'editable': True,
186        'delegate': delegates.CodeDelegate,
187        'nullable': True,
188        'parts': f.parts
189    },
190
191    camelot.types.IPAddress: lambda f: {
192        'python_type': str,
193        'editable': True,
194        'nullable': True,
195        'parts': f.parts,
196        'delegate': delegates.CodeDelegate,
197        'widget': 'code'
198    },
199
200    camelot.types.VirtualAddress: lambda f: {
201        'python_type': str,
202        'editable': True,
203        'nullable': True,
204        'delegate': delegates.VirtualAddressDelegate
205    },
206
207    camelot.types.RichText: lambda f: {
208        'python_type': str,
209        'editable': True,
210        'nullable': True,
211        'delegate': delegates.RichTextDelegate,
212        'from_string': string_from_string,
213    },
214
215    camelot.types.Color: lambda f: {
216        'delegate': delegates.ColorDelegate,
217        'python_type': str,
218        'editable': True,
219        'nullable': True,
220        'widget': 'color'
221    },
222
223    camelot.types.Rating: lambda f: {
224        'delegate': delegates.StarDelegate,
225        'editable': True,
226        'nullable': True,
227        'python_type': int,
228        'widget': 'star',
229        'from_string': int_from_string
230    },
231
232    camelot.types.Enumeration: lambda f: {
233        'delegate': delegates.EnumerationDelegate,
234        'python_type': str,
235        'choices': [(v, enumeration_to_string(v)) for v in f.choices],
236        'from_string': lambda s:dict((enumeration_to_string(v), v) for v in f.choices)[s],
237        'editable': True,
238        'nullable': False,
239        'widget': 'combobox',
240    },
241   
242    camelot.types.Language: lambda f: {
243        'delegate': delegates.EnumerationDelegate,
244        'python_type': str,
245        'choices': f.choices,
246        'from_string': string_from_string,
247        'editable': True,
248        'nullable': False,
249        'widget': 'combobox',
250    },   
251
252    camelot.types.File : lambda f: {
253        'python_type': str,
254        'editable': True,
255        'delegate': delegates.FileDelegate,
256        'storage': f.storage
257    },
258}
259
260#
261# Generate a restructured text table out of the prevous data structure
262#
263
264class DummyField(object):
265    def __init__(self):
266        self.length = 20
267        self.parts = ['AAA', '99']
268        self.choices = ['planned', 'canceled']
269        self.precision = 2
270        self.storage = None
271
272row_separator = '+' + '-'*20 + '+' + '-'*30 + '+' + '-'*70 + '+'
273row_format = """| %-18s | %-28s | %-68s |"""
274
275doc = """Field types handled through introspection :
276
277""" + row_separator + """
278""" + row_format%('**Field type**', '**Default delegate**', '**Default editor**') + """
279""" + row_separator + """
280"""
281
282field_types = _sqlalchemy_to_python_type_.keys()
283field_types.sort(lambda x, y: cmp(x.__name__, y.__name__))
284
285for field_type in field_types:
286    field_attributes = _sqlalchemy_to_python_type_[field_type](DummyField())
287    delegate = field_attributes['delegate']
288    row = row_format%(field_type.__name__, delegate.__name__, '.. image:: ../_static/editors/%s_editable.png'%(delegate.editor.__name__))
289    doc += row + """
290""" + row_separator + """
291"""
292
293doc += """
294"""
295
296__doc__ = doc
Note: See TracBrowser for help on using the browser.