Re: XML serialization and Unicode

2009-07-29 Thread l5x

Hello,

On 29 Lip, 15:56, cootetom  wrote:
> To add, something just popped into my head. Have you got
> DEFAULT_CHARSET = 'utf-8' in your settings.py fiile

It didn't help.

> On Jul 29, 2:51 pm, cootetom  wrote:
>
> > I would imagine that the deserialize function tries to encode the
> > input down to ascii. My suggestion would be to find the XML serializer
> > code and see what it is doing with the input string you pass to it.

Thanks for inspiration, although smart_unicode() and encode('utf-8')
in save() is not helping much, after changing /usr/lib/python2.5/xml/
sax/expatreader.py, line 207:

--BEFORE --

   try:
# The isFinal parameter is internal to the expat reader.
# If it is set to true, expat will check validity of the
entire
# document. When feeding chunks, they are not normally
final -
# except when invoked from close.
self._parser.Parse(data, isFinal)
except expat.error, e:
exc = SAXParseException(expat.ErrorString(e.code), e,
self)
# FIXME: when to invoke error()?
self._err_handler.fatalError(exc)

-- AFTER --

   try:
# The isFinal parameter is internal to the expat reader.
# If it is set to true, expat will check validity of the
entire
# document. When feeding chunks, they are not normally
final -
# except when invoked from close.

data = data.encode('utf-8') # this makes difference

self._parser.Parse(data, isFinal)
except expat.error, e:
exc = SAXParseException(expat.ErrorString(e.code), e,
self)
# FIXME: when to invoke error()?
self._err_handler.fatalError(exc)

It works. Thank you!

Best regards,
Luke
--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: XML serialization and Unicode

2009-07-29 Thread cootetom

To add, something just popped into my head. Have you got
DEFAULT_CHARSET = 'utf-8' in your settings.py file.



On Jul 29, 2:51 pm, cootetom  wrote:
> I would imagine that the deserialize function tries to encode the
> input down to ascii. My suggestion would be to find the XML serializer
> code and see what it is doing with the input string you pass to it.
>
> On Jul 29, 2:23 pm, l5x  wrote:
>
> > Hello,
>
> > first of all, congratulations!
>
> > I have a problem. When trying to open template I get following error:
>
> > Original Traceback (most recent call last):
> >   File "/usr/lib/python2.5/site-packages/django/template/debug.py",
> > line 71, in render_node
> >     result = node.render(context)
> >   File "/usr/lib/python2.5/site-packages/django/template/
> > defaulttags.py", line 206, in render
> >     val1 = self.var1.resolve(context, True)
> >   File "/usr/lib/python2.5/site-packages/django/template/__init__.py",
> > line 546, in resolve
> >     obj = self.var.resolve(context)
> >   File "/usr/lib/python2.5/site-packages/django/template/__init__.py",
> > line 687, in resolve
> >     value = self._resolve_lookup(context)
> >   File "/usr/lib/python2.5/site-packages/django/template/__init__.py",
> > line 722, in _resolve_lookup
> >     current = current()
> >   File "/home/user/django_projects/bis/bis/../bis/Log/models.py", line
> > 124, in deserialized_object
> >     for obj in serializers.deserialize("xml", self.data):
> >   File "/usr/lib/python2.5/site-packages/django/core/serializers/
> > xml_serializer.py", line 128, in next
> >     for event, node in self.event_stream:
> >   File "/usr/lib/python2.5/xml/dom/pulldom.py", line 232, in next
> >     rc = self.getEvent()
> >   File "/usr/lib/python2.5/xml/dom/pulldom.py", line 265, in getEvent
> >     self.parser.feed(buf)
> >   File "/usr/lib/python2.5/xml/sax/expatreader.py", line 207, in feed
> >     self._parser.Parse(data, isFinal)
> > UnicodeEncodeError: 'ascii' codec can't encode character u'\u0142' in
> > position 154: ordinal not in range(128)
>
> > I use latest SVN version (1.1) revision 11367. I have # -*- coding:
> > utf-8 -*-  line at the beginning of the file, and __unicode__ method
> > is set on the model, which I serialize and on Log model as well.
>
> > My serialization:
>
> >     [ some model ... ]
>
> > def save(self, force_insert=False, force_update=False):
> >         super(Record,self).save(force_insert, force_update)
> >         data_xml = serializers.serialize("xml", Record.objects.filter
> > (id=self.id))
> >         Log.objects.create(user=user, action="zapis", type="Record",
> >               object=self.id, data=data_xml)
>
> > Deserialization:
>
> >     [ my model: Log ... ]
>
> >     def deserialized_object(self):
> >         for obj in serializers.deserialize("xml", self.data):
> >             return obj.object   [1]
>
> > [1] There is only one object so I use return on the first try.
>
> > My template:
>
> > {% ifnotequal log.deserialized_object.value
> > log.previous.deserialized_object.value %}
> >     something
> >  {% endifnotequal %}
>
> > It doesn't matter how do I put data to database: manually using create
> > () or via form.save(), and it is in UTF-8. In addition I don't have
> > any problems with database and unicode in other parts of my project,
> > only during this deserialization.
>
> > I tried to pass data to deserializer after using force_unicode, but
> > the result is the same.
>
> > I would appreciate any help.
>
> > Best regards,
> > Luke
--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: XML serialization and Unicode

2009-07-29 Thread cootetom

I would imagine that the deserialize function tries to encode the
input down to ascii. My suggestion would be to find the XML serializer
code and see what it is doing with the input string you pass to it.



On Jul 29, 2:23 pm, l5x  wrote:
> Hello,
>
> first of all, congratulations!
>
> I have a problem. When trying to open template I get following error:
>
> Original Traceback (most recent call last):
>   File "/usr/lib/python2.5/site-packages/django/template/debug.py",
> line 71, in render_node
>     result = node.render(context)
>   File "/usr/lib/python2.5/site-packages/django/template/
> defaulttags.py", line 206, in render
>     val1 = self.var1.resolve(context, True)
>   File "/usr/lib/python2.5/site-packages/django/template/__init__.py",
> line 546, in resolve
>     obj = self.var.resolve(context)
>   File "/usr/lib/python2.5/site-packages/django/template/__init__.py",
> line 687, in resolve
>     value = self._resolve_lookup(context)
>   File "/usr/lib/python2.5/site-packages/django/template/__init__.py",
> line 722, in _resolve_lookup
>     current = current()
>   File "/home/user/django_projects/bis/bis/../bis/Log/models.py", line
> 124, in deserialized_object
>     for obj in serializers.deserialize("xml", self.data):
>   File "/usr/lib/python2.5/site-packages/django/core/serializers/
> xml_serializer.py", line 128, in next
>     for event, node in self.event_stream:
>   File "/usr/lib/python2.5/xml/dom/pulldom.py", line 232, in next
>     rc = self.getEvent()
>   File "/usr/lib/python2.5/xml/dom/pulldom.py", line 265, in getEvent
>     self.parser.feed(buf)
>   File "/usr/lib/python2.5/xml/sax/expatreader.py", line 207, in feed
>     self._parser.Parse(data, isFinal)
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u0142' in
> position 154: ordinal not in range(128)
>
> I use latest SVN version (1.1) revision 11367. I have # -*- coding:
> utf-8 -*-  line at the beginning of the file, and __unicode__ method
> is set on the model, which I serialize and on Log model as well.
>
> My serialization:
>
>     [ some model ... ]
>
> def save(self, force_insert=False, force_update=False):
>         super(Record,self).save(force_insert, force_update)
>         data_xml = serializers.serialize("xml", Record.objects.filter
> (id=self.id))
>         Log.objects.create(user=user, action="zapis", type="Record",
>               object=self.id, data=data_xml)
>
> Deserialization:
>
>     [ my model: Log ... ]
>
>     def deserialized_object(self):
>         for obj in serializers.deserialize("xml", self.data):
>             return obj.object   [1]
>
> [1] There is only one object so I use return on the first try.
>
> My template:
>
> {% ifnotequal log.deserialized_object.value
> log.previous.deserialized_object.value %}
>     something
>  {% endifnotequal %}
>
> It doesn't matter how do I put data to database: manually using create
> () or via form.save(), and it is in UTF-8. In addition I don't have
> any problems with database and unicode in other parts of my project,
> only during this deserialization.
>
> I tried to pass data to deserializer after using force_unicode, but
> the result is the same.
>
> I would appreciate any help.
>
> Best regards,
> Luke
--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



XML serialization and Unicode

2009-07-29 Thread l5x

Hello,

first of all, congratulations!

I have a problem. When trying to open template I get following error:

Original Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/django/template/debug.py",
line 71, in render_node
result = node.render(context)
  File "/usr/lib/python2.5/site-packages/django/template/
defaulttags.py", line 206, in render
val1 = self.var1.resolve(context, True)
  File "/usr/lib/python2.5/site-packages/django/template/__init__.py",
line 546, in resolve
obj = self.var.resolve(context)
  File "/usr/lib/python2.5/site-packages/django/template/__init__.py",
line 687, in resolve
value = self._resolve_lookup(context)
  File "/usr/lib/python2.5/site-packages/django/template/__init__.py",
line 722, in _resolve_lookup
current = current()
  File "/home/user/django_projects/bis/bis/../bis/Log/models.py", line
124, in deserialized_object
for obj in serializers.deserialize("xml", self.data):
  File "/usr/lib/python2.5/site-packages/django/core/serializers/
xml_serializer.py", line 128, in next
for event, node in self.event_stream:
  File "/usr/lib/python2.5/xml/dom/pulldom.py", line 232, in next
rc = self.getEvent()
  File "/usr/lib/python2.5/xml/dom/pulldom.py", line 265, in getEvent
self.parser.feed(buf)
  File "/usr/lib/python2.5/xml/sax/expatreader.py", line 207, in feed
self._parser.Parse(data, isFinal)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0142' in
position 154: ordinal not in range(128)

I use latest SVN version (1.1) revision 11367. I have # -*- coding:
utf-8 -*-  line at the beginning of the file, and __unicode__ method
is set on the model, which I serialize and on Log model as well.

My serialization:

[ some model ... ]

def save(self, force_insert=False, force_update=False):
super(Record,self).save(force_insert, force_update)
data_xml = serializers.serialize("xml", Record.objects.filter
(id=self.id))
Log.objects.create(user=user, action="zapis", type="Record",
  object=self.id, data=data_xml)

Deserialization:

[ my model: Log ... ]

def deserialized_object(self):
for obj in serializers.deserialize("xml", self.data):
return obj.object   [1]

[1] There is only one object so I use return on the first try.

My template:

{% ifnotequal log.deserialized_object.value
log.previous.deserialized_object.value %}
something
 {% endifnotequal %}

It doesn't matter how do I put data to database: manually using create
() or via form.save(), and it is in UTF-8. In addition I don't have
any problems with database and unicode in other parts of my project,
only during this deserialization.

I tried to pass data to deserializer after using force_unicode, but
the result is the same.

I would appreciate any help.

Best regards,
Luke
--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---