elpaso / qgis-formawarevaluerelationwidget

A Form-Aware Value Relation Widget plugin for QGIS
6 stars 8 forks source link

UnicodeEncodeError #6

Open mterente opened 8 years ago

mterente commented 8 years ago

Hello Allesandro,

I am using the widget on a table that has Unicode characters in Value column and I get this error:

2016-05-05T16:00:04 1   Traceback (most recent call last):
              File "C:/Users/mihai.terente/.qgis2/python/plugins\FormAwareValueRelationWidget\FormAwareValueRelationWidget.py", line 297, in initWidget
                self.populateWidget(editor)
              File "C:/Users/mihai.terente/.qgis2/python/plugins\FormAwareValueRelationWidget\FormAwareValueRelationWidget.py", line 359, in populateWidget
                cache = [(str(f.attributes()[self.key_index]), str(f.attributes()[self.value_index])) for f in self.mCache]
            UnicodeEncodeError: 'ascii' codec can't encode character u'\u0218' in position 5: ordinal not in range(128)

The character '\u0218' is the letter 'Ș', a very common one in Romanian. However, the error is not critical, since the values are correctly saved.

Could you look, please?

Thank you, Mihai

mterente commented 8 years ago

Hello Allesandro,

Looking at the closed issue #2 I could apply the same solution on line 359 so that now it is cache = [(unicode(f.attributes()[self.key_index]), unicode(f.attributes()[self.value_index])) for f in self.mCache] and your excellent plugin is now working normally.

Thanks again for providing it, Mihai

mterente commented 8 years ago

Well... Not quite... The above mentioned solution works if I open the layer form. If I use relations, and the form is embedded within the form of another layer, the error comes back:

2016-05-05T16:55:58 1   Traceback (most recent call last):
              File "C:/Users/mihai.terente/.qgis2/python/plugins\FormAwareValueRelationWidget\FormAwareValueRelationWidget.py", line 297, in initWidget
                self.populateWidget(editor)
              File "C:/Users/mihai.terente/.qgis2/python/plugins\FormAwareValueRelationWidget\FormAwareValueRelationWidget.py", line 359, in populateWidget
                cache = [(unicode(f.attributes()[self.key_index]), unicode(f.attributes()[self.value_index])) for f in self.mCache]
            UnicodeEncodeError: 'ascii' codec can't encode character u'\u0218' in position 5: ordinal not in range(128)

and now it has a critical behaviour: it removes the value that has the Unicode character from the attribute table.

elpaso commented 8 years ago

I would be great if you could attach a minimal project where I can reproduce the issue.

mterente commented 8 years ago

I did an upgrade to QGIS 2.14.2 and the problem solved. I am unable to reproduce it now.

However, I'm still using the modified line no 359 with cache = [(unicode(f.attributes()[self.key_index]), unicode(f.attributes()[self.value_index])) for f in self.mCache]

All the best, Mihai

Frederikssund commented 8 years ago

You only get the error when opening the Attribute window:

2016-05-20T10:06:31 1   Traceback (most recent call last):
              File "C:/Users/bvtho/.qgis2/python/plugins\FormAwareValueRelationWidget\FormAwareValueRelationWidget.py", line 297, in initWidget
                self.populateWidget(editor)
              File "C:/Users/bvtho/.qgis2/python/plugins\FormAwareValueRelationWidget\FormAwareValueRelationWidget.py", line 359, in populateWidget
                cache = [(str(f.attributes()[self.key_index]), str(f.attributes()[self.value_index])) for f in self.mCache]
            UnicodeEncodeError: 'ascii' codec can't encode character u'\xe6' in position 0: ordinal not in range(128)

I've attached a small project to reproduce the error: formvaluerelation.zip

The bug can be eradicated by replacing "str" function calls to a "unicode" function call. You should probably take a look at the entire codebase for the plugin. There is around 10-12 calls to "str" that might cause trouble at some point

And thank you for an awesome plugin!! It should be a core function ASAP

Regards Bo Victor Thomsen

Frederikssund commented 8 years ago

I've just made an experiment: I've replaced all occurences of the "str" function calls in module "FormAwareValueRelationWidget.py" to "unicode" calls.

This apparently works and the python error when opening the attribute window has disappeared

Regards Bo Victor Thomsen

Frederikssund commented 8 years ago

Bzz ! Why does this GitHub repository contain an elder version (ver. 1.3) than the plugin distributed by QGIS.org (ver. 1.4) ?

elpaso commented 8 years ago

I probably forgot to merge from the deleted_bug branch. Done.

elpaso commented 8 years ago

Can you make a PR with your fixes?

Frederikssund commented 8 years ago

Done !

Regards Bo Victor Thomsen

Fra: Alessandro Pasotti [mailto:notifications@github.com] Sendt: 20. maj 2016 13:34 Til: elpaso/qgis-formawarevaluerelationwidget Cc: GIS-gruppen (Funktionspostkasse); Comment Emne: Re: [elpaso/qgis-formawarevaluerelationwidget] UnicodeEncodeError (#6)

Can you make a PR with your fixes?

You are receiving this because you commented. Reply to this email directly or view it on GitHubhttps://github.com/elpaso/qgis-formawarevaluerelationwidget/issues/6#issuecomment-220582861