Re: Problem with i18n in tests
On Mon, Nov 24, 2008 at 9:25 AM, Florencio Cano <[EMAIL PROTECTED]>wrote: > I've done that because I want to change the default error_message and > I do not know who to do it :(. I've tried error_messages={} in the > field but it is now recognized in Fields in ModelForms only in plain > Forms. So, how can I change the default error_message if I'm using > ModelForms? > That error message is not currently customizable, there is a ticket open noting that: http://code.djangoproject.com/ticket/8913 It looks, though, like you are trying to customize the message to be Spanish -- at any rate the message you are using is very similar to the provided Spanish translation for that message. So I'm curious why you are not just using the Spanish translation? If you really absolutely need to customize the message in the absence of a fix for #8913 and without using a provided translation then the approach you have taken can work if you make your clean code query the database for uniqueness instead of trying to do it in Python. The reason you are seeing the built-in message is that your existing clean routine is not rejecting as duplicate a case that is caught by ModelForm's validate_unique. If you reject in your own clean routine everything that will be caught by the built-in ModelForm validate_unique then you won't see the built-in message. It's not ideal but it can be made to work. Karen --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Problem with i18n in tests
I've done that because I want to change the default error_message and I do not know who to do it :(. I've tried error_messages={} in the field but it is now recognized in Fields in ModelForms only in plain Forms. So, how can I change the default error_message if I'm using ModelForms? > Why are doing your own uniqueness check for the nombre field? You have > specified unique=True for nombre so the default ModelForm validation will > check uniqueness. The ModelForm validation will (essentially) attempt to > get() a Responsable object with that nombre from the DB -- leaving it up to > the DB to find a match and return that one match, if it exists. The check > as you have written it is much less efficient since it requires retrieving > all Responsable objects from the DB and then manipulating them in Python. > Plus, it will not see 'Jose Lopez' as a duplicate of 'José López' -- as far > as Python is concerned these are different strings. --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Problem with i18n in tests
On Sat, Nov 22, 2008 at 6:52 AM, Florencio Cano <[EMAIL PROTECTED]>wrote: > Hi! > I'm testing a Django application where I'm using i18n. > The problem is with a test that says that a word with an accent > (spanish) is the same as a word without an accent. And although I have > established my own error messages it shows other error message when > using a word with accents. > The problem is with the second test shown. > I have these sentences in settings.py: > > TEST_DATABASE_CHARSET='utf8' > TEST_DATABASE_COLLATION='utf8_spanish_ci' > > -- code -- > > class Responsable(models.Model): >user = models.ForeignKey(User, unique=True) >nombre = models.CharField('Nombre', max_length=200, unique=True) >email = models.EmailField('Email', max_length=100, unique=True) >responsable_lopd = models.BooleanField('Responsable LOPD') > >def __unicode__(self): >return self.nombre > > > class ResponsableForm(forms.ModelForm): >def clean_nombre(self): >nombre = self.cleaned_data['nombre'] >if nombre in [u.nombre for u in Responsable.objects.all()]: >raise forms.ValidationError('Ya existe un responsable con > este nombre') >return nombre > >class Meta: >model = Responsable > Why are doing your own uniqueness check for the nombre field? You have specified unique=True for nombre so the default ModelForm validation will check uniqueness. The ModelForm validation will (essentially) attempt to get() a Responsable object with that nombre from the DB -- leaving it up to the DB to find a match and return that one match, if it exists. The check as you have written it is much less efficient since it requires retrieving all Responsable objects from the DB and then manipulating them in Python. Plus, it will not see 'Jose Lopez' as a duplicate of 'José López' -- as far as Python is concerned these are different strings. > > > def test_agregar_responsable_nombre_duplicado(self): >respuesta = self.client.post('/responsables/agregar/', >{ >'username':'pepe', >'password':'qwerty', >'confirma_password':'qwerty', >'nombre':'Pepe Lopez', >'email':'[EMAIL PROTECTED]<[EMAIL PROTECTED]> > ', >'responsable_lopd':False >}) > > >respuesta2 = self.client.post('/responsables/agregar/', >{ >'username':'josele', >'password':'qwerty', >'confirma_password':'qwerty', >'nombre':'Pepe Lopez', >'email':'[EMAIL PROTECTED]<[EMAIL PROTECTED]> > ', >'responsable_lopd':False > }) > >self.assertFormError(respuesta2, 'responsable_form', 'nombre', 'Ya > existe un responsable con este nombre') > > > def test_agregar_responsable_nombre_duplicado_tildes(self): >respuesta = self.client.post('/responsables/agregar/', >{ >'username':'josele', >'password':'qwerty', >'confirma_password':'qwerty', >'nombre':'José López', >'email':'[EMAIL PROTECTED]<[EMAIL PROTECTED]> > ', >'responsable_lopd':False > }) > >respuesta2 = self.client.post('/responsables/agregar/', >{ >'username':'joselito', >'password':'qwerty', >'confirma_password':'qwerty', >'nombre':'Jose Lopez', >'email':'[EMAIL PROTECTED]<[EMAIL PROTECTED]> > ', >'responsable_lopd':False > }) > >self.assertFormError(respuesta2, 'responsable_form', 'nombre', 'Ya > existe un responsable con este nombre') > > > FAIL: test_agregar_responsable_nombre_duplicado_tildes > (gesfich.fichlopd.tests.RegistrarUsuariosTestCase) > -- > Traceback (most recent call last): > File "/home/fcano/GesFichLOPD/gesfich/../gesfich/fichlopd/tests.py", > line 228, in test_agregar_responsable_nombre_duplicado_tildes >self.assertFormError(respuesta2, 'responsable_form', 'nombre', 'Ya > existe un responsable con este nombre') > File "/usr/lib/python2.5/site-packages/django/test/testcases.py", > line 317, in assertFormError >repr(field_errors))) > AssertionError: The field 'nombre' on form 'responsable_form' in > context 0 does not contain the error 'Ya existe un responsable con > este nombre' (actual errors: [u'Responsable with this Nombre already > exists.']) > What you see here is the built-in ModelForm message reporting that the nombre you have specified violates the unique constraint. That is, the check you provided in the field's clean did not find the duplicate, but the DB did (given the utf8_spanish_ci collation you are using, which considers the accented characters to be the same as the non-accented version). Karen --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegrou
Re: Problem with i18n in tests
On Sat, Nov 22, 2008 at 9:52 AM, Florencio Cano <[EMAIL PROTECTED]> wrote: > Hi! > I'm testing a Django application where I'm using i18n. > > [...] > > FAIL: test_agregar_responsable_nombre_duplicado_tildes > (gesfich.fichlopd.tests.RegistrarUsuariosTestCase) > -- > Traceback (most recent call last): > File "/home/fcano/GesFichLOPD/gesfich/../gesfich/fichlopd/tests.py", > line 228, in test_agregar_responsable_nombre_duplicado_tildes >self.assertFormError(respuesta2, 'responsable_form', 'nombre', 'Ya > existe un responsable con este nombre') > File "/usr/lib/python2.5/site-packages/django/test/testcases.py", > line 317, in assertFormError >repr(field_errors))) > AssertionError: The field 'nombre' on form 'responsable_form' in > context 0 does not contain the error 'Ya existe un responsable con > este nombre' (actual errors: [u'Responsable with this Nombre already > exists.']) > Does the settings file being used for the tests have USE_i18N=True? -- Ramiro Morales --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Problem with i18n in tests
Hi! I'm testing a Django application where I'm using i18n. The problem is with a test that says that a word with an accent (spanish) is the same as a word without an accent. And although I have established my own error messages it shows other error message when using a word with accents. The problem is with the second test shown. I have these sentences in settings.py: TEST_DATABASE_CHARSET='utf8' TEST_DATABASE_COLLATION='utf8_spanish_ci' -- code -- class Responsable(models.Model): user = models.ForeignKey(User, unique=True) nombre = models.CharField('Nombre', max_length=200, unique=True) email = models.EmailField('Email', max_length=100, unique=True) responsable_lopd = models.BooleanField('Responsable LOPD') def __unicode__(self): return self.nombre class ResponsableForm(forms.ModelForm): def clean_nombre(self): nombre = self.cleaned_data['nombre'] if nombre in [u.nombre for u in Responsable.objects.all()]: raise forms.ValidationError('Ya existe un responsable con este nombre') return nombre class Meta: model = Responsable def test_agregar_responsable_nombre_duplicado(self): respuesta = self.client.post('/responsables/agregar/', { 'username':'pepe', 'password':'qwerty', 'confirma_password':'qwerty', 'nombre':'Pepe Lopez', 'email':'[EMAIL PROTECTED]', 'responsable_lopd':False }) respuesta2 = self.client.post('/responsables/agregar/', { 'username':'josele', 'password':'qwerty', 'confirma_password':'qwerty', 'nombre':'Pepe Lopez', 'email':'[EMAIL PROTECTED]', 'responsable_lopd':False }) self.assertFormError(respuesta2, 'responsable_form', 'nombre', 'Ya existe un responsable con este nombre') def test_agregar_responsable_nombre_duplicado_tildes(self): respuesta = self.client.post('/responsables/agregar/', { 'username':'josele', 'password':'qwerty', 'confirma_password':'qwerty', 'nombre':'José López', 'email':'[EMAIL PROTECTED]', 'responsable_lopd':False }) respuesta2 = self.client.post('/responsables/agregar/', { 'username':'joselito', 'password':'qwerty', 'confirma_password':'qwerty', 'nombre':'Jose Lopez', 'email':'[EMAIL PROTECTED]', 'responsable_lopd':False }) self.assertFormError(respuesta2, 'responsable_form', 'nombre', 'Ya existe un responsable con este nombre') FAIL: test_agregar_responsable_nombre_duplicado_tildes (gesfich.fichlopd.tests.RegistrarUsuariosTestCase) -- Traceback (most recent call last): File "/home/fcano/GesFichLOPD/gesfich/../gesfich/fichlopd/tests.py", line 228, in test_agregar_responsable_nombre_duplicado_tildes self.assertFormError(respuesta2, 'responsable_form', 'nombre', 'Ya existe un responsable con este nombre') File "/usr/lib/python2.5/site-packages/django/test/testcases.py", line 317, in assertFormError repr(field_errors))) AssertionError: The field 'nombre' on form 'responsable_form' in context 0 does not contain the error 'Ya existe un responsable con este nombre' (actual errors: [u'Responsable with this Nombre already exists.']) -- --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---