~osomon/phatch/extract-all-metadata

« back to all changes in this revision

Viewing changes to models.py

  • Committer: stani
  • Date: 2007-06-12 17:18:53 UTC
  • Revision ID: spe.stani.be@gmail.com-20070612171853-cndnqupa2o42iai4
gettext support, major rewrite

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import os, types
6
6
import base, ct
7
7
from odict import odict as Fields
8
 
from translation import _, to_english, translate
9
8
 
10
9
NO_FIELDS              = Fields()
11
10
 
21
20
    email   = 'spe.stani.be@gmail.com'
22
21
    version = '0.1'
23
22
    tags    = []
24
 
    summary = 'Please update your tags!'
 
23
    summary = _('Please update your tags!')
25
24
    
26
25
    def __init__(self, fields = NO_FIELDS):
27
26
        """For the paramters a Gui needs to be written"""
134
133
                    .replace('%(%(','<').replace(')s)s','>')%info
135
134
            except KeyError, variable:
136
135
                raise ValidationError(self.description,
137
 
                _("The variable '%s' doesn't exist.")%variable,
138
 
                _("The defined variables are: %s.")%
139
 
                    (', '.join(info.keys())))
 
136
                "%s '%s' %s."%(_("The variable"),
 
137
                    variable,_("doesn't exist")),
 
138
                "%s: %s."%(_('The defined variables are'),
 
139
                    (', '.join(info.keys()))))
140
140
 
141
141
    def to_python(self,x):
142
142
        return x
146
146
    
147
147
    def get_as_string(self):
148
148
        """For GUI: Translation, but no interpolation here"""
149
 
        return translate(self.value_as_string)
 
149
        return self.value_as_string
150
150
    
151
151
    def set_as_string(self,x):
152
152
        """For GUI: Translation, but no interpolation here"""
153
 
        self.value_as_string  = to_english(x)
 
153
        self.value_as_string    = x
154
154
        
155
155
    def get(self,info=None,value_as_string=None):
156
156
        """For code: Interpolated, but not translated
168
168
            return eval(x)
169
169
        except SyntaxError:
170
170
            raise ValidationError(self.description, 
171
 
            _('Invalid syntax "%s" for integer.')%x)
 
171
            '%s "%s" %s.'%(_('Invalid syntax'),x,_('for integer')))
172
172
        
173
173
class IntegerField(Field):
174
174
    """"""
176
176
    
177
177
    def to_python(self,x):
178
178
        error   = ValidationError(self.description, 
179
 
            _('Invalid literal "%s" for integer.')%x)
 
179
            '%s "%s" %s.'%(_('Invalid literal'),x,_('for integer')))
180
180
        try:
181
181
            return int(round(self.eval(x)))
182
182
        except ValueError:
192
192
        value = super(PositiveIntegerField, self).to_python(x)
193
193
        if value < 0:
194
194
            raise ValidationError(self.description,
195
 
            _('The integer value "%s" is negative, but should be positive.')%x)
 
195
            '%s "%s" %s.'%(('The integer value'),x,
 
196
                _('is negative, but should be positive')))
196
197
        return value
197
198
    
198
199
class PositiveNonZeroIntegerField(PositiveIntegerField):
204
205
        value = super(PositiveNonZeroIntegerField, self).to_python(x)
205
206
        if value == 0:
206
207
            raise ValidationError(self.description,
207
 
                _('The integer value "%s" is zero, but should be non-zero.')%x)
 
208
                '%s "%s" %s.'%(_('The integer value'),x,
 
209
                    _('is zero, but should be non-zero.')))
208
210
        return value
209
211
                
210
212
class DpiField(PositiveNonZeroIntegerField):
222
224
            return float(self.eval(x))
223
225
        except ValueError, message:
224
226
            raise ValidationError(self.description, 
225
 
            _('Invalid literal "%s" for float.')%x)
 
227
            '%s "%s" %s.'%(_('Invalid literal'),x,_('for float')))
226
228
 
227
229
class BooleanField(Field):
228
230
    description             = _('boolean')
229
231
    
 
232
    def to_string(self,x):
 
233
        return super(BooleanField,self).to_string(x).lower()
 
234
    
230
235
    def to_python(self,x):
231
 
        if x.lower() in ['true','1',_('true')]: return True
232
 
        if x.lower() in ['false','0',_('false')]: return False
 
236
        if x.lower() in ['1','true']: return True
 
237
        if x.lower() in ['0','false']: return False
233
238
        raise ValidationError(self.description, 
234
 
            _('Invalid literal "%s" for boolean (%s,%s).')\
235
 
                %(x,_('true'),_('false')))
 
239
            '%s "%s" %s (%s,%s).'%(_('Invalid literal'), x,_('for boolean'),
 
240
                _('true'),_('false')))
236
241
    
237
242
class CharField(Field):
238
 
    description             = 'string'
 
243
    description             = _('string')
239
244
    pass
240
245
    
241
246
class ChoiceField(CharField):
242
 
    description             = 'choice'
 
247
    description             = _('choice')
243
248
    def __init__(self,value,choices,**keyw):
244
249
        super(ChoiceField,self).__init__(value,**keyw)
245
250
        self.choices    = choices
255
260
        super(ImageTypeField,self).__init__(value,ct.IMAGE_EXTENSIONS,**keyw)
256
261
        
257
262
    def set_as_string(self,x):
 
263
        #ignore translation
258
264
        if x and x[0]=='.':
259
265
            x = x[1:]
260
266
        super(ImageTypeField,self).set_as_string(x)