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

Revision 1187, 9.2 KB (checked in by erikj, 6 months ago)

first functional expanded search

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.ne, operator.lt, operator.le, operator.gt, operator.ge)
49_text_operators = (operator.eq, operator.ne)
50
51_sqlalchemy_to_python_type_ = {
52
53    sqlalchemy.types.Boolean: lambda f: {
54        'python_type': bool,
55        'editable': True,
56        'nullable': True,
57        'delegate': delegates.BoolDelegate,
58        'from_string': bool_from_string,
59        'operators' : (operator.eq,),
60    },
61
62    sqlalchemy.types.BOOLEAN: lambda f: {
63        'python_type': bool,
64        'editable': True,
65        'nullable': True,
66        'delegate': delegates.BoolDelegate,
67        'from_string': bool_from_string,
68        'operators' : (operator.eq,),
69    },
70
71    sqlalchemy.types.Date: lambda f: {
72        'python_type': datetime.date,
73        'format': constants.camelot_date_format,
74        'editable': True,
75        'min': None,
76        'max': None,
77        'nullable': True,
78        'delegate': delegates.DateDelegate,
79        'from_string': date_from_string,
80        'operators' : _numerical_operators,
81    },
82
83    sqlalchemy.types.Time : lambda f: {
84            'python_type': datetime.time,
85            'editable': True,
86            'nullable': True,
87            'widget': 'time',
88            'delegate': delegates.TimeDelegate,
89            'format': constants.camelot_time_format,
90            'nullable': True,
91            'from_string': time_from_string,
92            'operators': _numerical_operators,
93    },
94
95    sqlalchemy.types.DateTime : lambda f: {
96        'python_type': datetime.datetime,
97        'editable': True,
98        'nullable': True,
99        'widget': 'time',
100        'format': constants.camelot_datetime_format,
101        'nullable': True,
102        'delegate': delegates.DateTimeDelegate,
103        'from_string': datetime_from_string,
104        'operators': _numerical_operators,
105    },
106
107    sqlalchemy.types.Float: lambda f: {
108        'python_type': float,
109        'precision': f.precision if not isinstance(f.precision, tuple) else f.precision[1],
110        'editable': True,
111        'minimum': constants.camelot_minfloat,
112        'maximum': constants.camelot_maxfloat,
113        'nullable': True,
114        'delegate': delegates.FloatDelegate,
115        'from_string': float_from_string,
116        'operators': _numerical_operators,
117    },
118
119    sqlalchemy.types.Integer: lambda f: {
120        'python_type': int,
121        'editable': True,
122        'minimum': constants.camelot_minint,
123        'maximum': constants.camelot_maxint,
124        'nullable': True,
125        'delegate': delegates.IntegerDelegate,
126        'from_string': int_from_string,
127        'to_string': unicode,
128        'widget': 'int',
129        'operators': _numerical_operators,
130    },
131
132    sqlalchemy.types.INT: lambda f: {
133        'python_type': int,
134        'editable': True,
135        'minimum': constants.camelot_minint,
136        'maximum': constants.camelot_maxint,
137        'nullable': True,
138        'delegate': delegates.IntegerDelegate,
139        'from_string': int_from_string,
140        'widget': 'int',
141        'operators': _numerical_operators,
142    },
143
144    sqlalchemy.types.String: lambda f: {
145        'python_type': str,
146        'length': f.length,
147        'delegate': delegates.PlainTextDelegate,
148        'editable': True,
149        'nullable': True,
150        'widget': 'str',
151        'from_string': string_from_string,
152        'operators' : _text_operators,
153    },
154
155    sqlalchemy.types.TEXT: lambda f: {
156        'python_type': str,
157        'length': f.length,
158        'delegate': delegates.PlainTextDelegate,
159        'editable': True,
160        'nullable': True,
161        'widget': 'str',
162        'from_string': string_from_string,
163        'operators' : _text_operators,
164    },
165
166    sqlalchemy.types.Unicode: lambda f: {
167        'python_type': str,
168        'length': f.length,
169        'delegate': delegates.PlainTextDelegate,
170        'editable': True,
171        'nullable': True,
172        'widget': 'str',
173        'from_string': string_from_string,
174        'operators' : _text_operators,
175    },
176
177    camelot.types.Image: lambda f: {
178        'python_type': str,
179        'editable': True,
180        'nullable': True,
181        'delegate': delegates.ImageDelegate,
182        'storage': f.storage,
183        'preview_width': 100,
184        'preview_height': 100,
185        'operators' : _text_operators,
186    },
187
188    camelot.types.Code: lambda f: {
189        'python_type': str,
190        'editable': True,
191        'delegate': delegates.CodeDelegate,
192        'nullable': True,
193        'parts': f.parts,
194        'operators' : _text_operators,
195    },
196
197    camelot.types.IPAddress: lambda f: {
198        'python_type': str,
199        'editable': True,
200        'nullable': True,
201        'parts': f.parts,
202        'delegate': delegates.CodeDelegate,
203        'widget': 'code',
204        'operators' : _text_operators,
205    },
206
207    camelot.types.VirtualAddress: lambda f: {
208        'python_type': str,
209        'editable': True,
210        'nullable': True,
211        'delegate': delegates.VirtualAddressDelegate,
212        'operators' : _text_operators,
213    },
214
215    camelot.types.RichText: lambda f: {
216        'python_type': str,
217        'editable': True,
218        'nullable': True,
219        'delegate': delegates.RichTextDelegate,
220        'from_string': string_from_string,
221        'operators' : _text_operators,
222    },
223
224    camelot.types.Color: lambda f: {
225        'delegate': delegates.ColorDelegate,
226        'python_type': str,
227        'editable': True,
228        'nullable': True,
229        'widget': 'color',
230        'operators' : _text_operators,
231    },
232
233    camelot.types.Rating: lambda f: {
234        'delegate': delegates.StarDelegate,
235        'editable': True,
236        'nullable': True,
237        'python_type': int,
238        'widget': 'star',
239        'from_string': int_from_string,
240        'operators' : _numerical_operators,
241    },
242
243    camelot.types.Enumeration: lambda f: {
244        'delegate': delegates.EnumerationDelegate,
245        'python_type': str,
246        'choices': [(v, enumeration_to_string(v)) for v in f.choices],
247        'from_string': lambda s:dict((enumeration_to_string(v), v) for v in f.choices)[s],
248        'editable': True,
249        'nullable': False,
250        'widget': 'combobox',
251        'operators' : _numerical_operators,
252    },
253   
254    camelot.types.Language: lambda f: {
255        'delegate': delegates.EnumerationDelegate,
256        'python_type': str,
257        'choices': f.choices,
258        'from_string': string_from_string,
259        'editable': True,
260        'nullable': False,
261        'widget': 'combobox',
262    },   
263
264    camelot.types.File : lambda f: {
265        'python_type': str,
266        'editable': True,
267        'delegate': delegates.FileDelegate,
268        'storage': f.storage,
269        'operators' : _text_operators,
270    },
271}
272
273#
274# Generate a restructured text table out of the prevous data structure
275#
276
277class DummyField(object):
278    def __init__(self):
279        self.length = 20
280        self.parts = ['AAA', '99']
281        self.choices = ['planned', 'canceled']
282        self.precision = 2
283        self.storage = None
284
285row_separator = '+' + '-'*20 + '+' + '-'*30 + '+' + '-'*70 + '+'
286row_format = """| %-18s | %-28s | %-68s |"""
287
288doc = """Field types handled through introspection :
289
290""" + row_separator + """
291""" + row_format%('**Field type**', '**Default delegate**', '**Default editor**') + """
292""" + row_separator + """
293"""
294
295field_types = _sqlalchemy_to_python_type_.keys()
296field_types.sort(lambda x, y: cmp(x.__name__, y.__name__))
297
298for field_type in field_types:
299    field_attributes = _sqlalchemy_to_python_type_[field_type](DummyField())
300    delegate = field_attributes['delegate']
301    row = row_format%(field_type.__name__, delegate.__name__, '.. image:: ../_static/editors/%s_editable.png'%(delegate.editor.__name__))
302    doc += row + """
303""" + row_separator + """
304"""
305
306doc += """
307"""
308
309__doc__ = doc
Note: See TracBrowser for help on using the browser.