comparison sat_pubsub/pgsql_storage.py @ 271:232002e132db 0.1.1

pgsql_backend unicode fix: parseXml was expecting str and getting unicode
author Goffi <goffi@goffi.org>
date Wed, 13 Aug 2014 17:30:37 +0200
parents f17034e4cf4a
children b757c29b20d7
comparison
equal deleted inserted replaced
270:f17034e4cf4a 271:232002e132db
57 from zope.interface import implements 57 from zope.interface import implements
58 58
59 from twisted.internet import defer 59 from twisted.internet import defer
60 from twisted.words.protocols.jabber import jid 60 from twisted.words.protocols.jabber import jid
61 61
62 from wokkel.generic import parseXml, stripNamespace 62 from wokkel import generic
63 from wokkel.pubsub import Subscription 63 from wokkel.pubsub import Subscription
64 64
65 from sat_pubsub import error, iidavoll, const 65 from sat_pubsub import error, iidavoll, const
66 import psycopg2 66 import psycopg2
67 import psycopg2.extensions 67 import psycopg2.extensions
68 # we wants psycopg2 to return us unicode, not str
68 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) 69 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
69 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY) 70 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
71
72 # parseXml manage str, but we get unicode
73 parseXml = lambda unicode_data: generic.parseXml(unicode_data.encode('utf-8'))
70 74
71 class Storage: 75 class Storage:
72 76
73 implements(iidavoll.IStorage) 77 implements(iidavoll.IStorage)
74 78
641 645
642 result = cursor.fetchall() 646 result = cursor.fetchall()
643 if unrestricted: 647 if unrestricted:
644 ret = [] 648 ret = []
645 for data in result: 649 for data in result:
646 item = stripNamespace(parseXml(data[0])) 650 item = generic.stripNamespace(parseXml(data[0]))
647 access_model = data[1] 651 access_model = data[1]
648 item_id = data[2] 652 item_id = data[2]
649 if access_model == 'roster': #TODO: jid access_model 653 if access_model == 'roster': #TODO: jid access_model
650 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,)) 654 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,))
651 access_list = [r[0] for r in cursor.fetchall()] 655 access_list = [r[0] for r in cursor.fetchall()]
652 else: 656 else:
653 access_list = None 657 access_list = None
654 658
655 ret.append((item, access_model, access_list)) 659 ret.append((item, access_model, access_list))
656 return ret 660 return ret
657 items = [stripNamespace(parseXml(r[0])) for r in result] 661 items = [generic.stripNamespace(parseXml(r[0])) for r in result]
658 return items 662 return items
659 663
660 664
661 def getItemsById(self, authorized_groups, unrestricted, itemIdentifiers): 665 def getItemsById(self, authorized_groups, unrestricted, itemIdentifiers):
662 """ Get items which are in the given list 666 """ Get items which are in the given list
679 (self.nodeIdentifier, 683 (self.nodeIdentifier,
680 itemIdentifier)) 684 itemIdentifier))
681 result = cursor.fetchone() 685 result = cursor.fetchone()
682 if result: 686 if result:
683 for data in result: 687 for data in result:
684 item = stripNamespace(parseXml(data[0])) 688 item = generic.stripNamespace(parseXml(data[0]))
685 access_model = data[1] 689 access_model = data[1]
686 item_id = data[2] 690 item_id = data[2]
687 if access_model == 'roster': #TODO: jid access_model 691 if access_model == 'roster': #TODO: jid access_model
688 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,)) 692 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,))
689 access_list = [r[0] for r in cursor.fetchall()] 693 access_list = [r[0] for r in cursor.fetchall()]