comparison sat_pubsub/pgsql_storage.py @ 317:34adc4a8aa64

new container module, with an ItemData container: this simplify item data manipulation and transmission between storage and backend, it's also better if new data need to be used.
author Goffi <goffi@goffi.org>
date Sun, 03 Jan 2016 18:33:22 +0100
parents 5d7c3787672e
children d13526c0eb32
comparison
equal deleted inserted replaced
316:cca47e9977a5 317:34adc4a8aa64
59 from twisted.words.protocols.jabber import jid 59 from twisted.words.protocols.jabber import jid
60 60
61 from wokkel import generic 61 from wokkel import generic
62 from wokkel.pubsub import Subscription 62 from wokkel.pubsub import Subscription
63 63
64 from sat_pubsub import error, iidavoll, const 64 from sat_pubsub import error
65 from sat_pubsub import iidavoll
66 from sat_pubsub import const
67 from sat_pubsub import container
65 import psycopg2 68 import psycopg2
66 import psycopg2.extensions 69 import psycopg2.extensions
67 # we wants psycopg2 to return us unicode, not str 70 # we wants psycopg2 to return us unicode, not str
68 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) 71 psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
69 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY) 72 psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
687 @param unrestricted: if true, don't check permissions (i.e.: get all items) 690 @param unrestricted: if true, don't check permissions (i.e.: get all items)
688 @param maxItems: nb of items we want to tget 691 @param maxItems: nb of items we want to tget
689 @param rsm_data: options for RSM feature handling (XEP-0059) as a 692 @param rsm_data: options for RSM feature handling (XEP-0059) as a
690 dictionnary of C{unicode} to C{unicode}. 693 dictionnary of C{unicode} to C{unicode}.
691 694
692 @return: list of (item, access_model, id) if unrestricted is True, else list of items 695 @return: list of container.ItemData
696 if unrestricted is False, access_model and config will be None
693 """ 697 """
694 if ext_data is None: 698 if ext_data is None:
695 ext_data = {} 699 ext_data = {}
696 return self.dbpool.runInteraction(self._getItems, authorized_groups, unrestricted, maxItems, ext_data) 700 return self.dbpool.runInteraction(self._getItems, authorized_groups, unrestricted, maxItems, ext_data)
697 701
776 access_list = {} 780 access_list = {}
777 if access_model == const.VAL_AMODEL_ROSTER: #TODO: jid access_model 781 if access_model == const.VAL_AMODEL_ROSTER: #TODO: jid access_model
778 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,)) 782 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,))
779 access_list[const.OPT_ROSTER_GROUPS_ALLOWED] = [r[0] for r in cursor.fetchall()] 783 access_list[const.OPT_ROSTER_GROUPS_ALLOWED] = [r[0] for r in cursor.fetchall()]
780 784
781 ret.append((item, access_model, access_list)) 785 ret.append(container.ItemData(item, access_model, access_list))
782 return ret 786 return ret
783 items = [generic.stripNamespace(parseXml(r[0])) for r in result] 787 items_data = [container.ItemData(generic.stripNamespace(parseXml(r[0])), None, None) for r in result]
784 return items 788 return items_data
785 789
786 def countItems(self, authorized_groups, unrestricted): 790 def countItems(self, authorized_groups, unrestricted):
787 """ Count the accessible items. 791 """ Count the accessible items.
788 792
789 @param authorized_groups: we want to get items that these groups can access. 793 @param authorized_groups: we want to get items that these groups can access.
791 @return: deferred that fires a C{int}. 795 @return: deferred that fires a C{int}.
792 """ 796 """
793 return self.dbpool.runInteraction(self._countItems, authorized_groups, unrestricted) 797 return self.dbpool.runInteraction(self._countItems, authorized_groups, unrestricted)
794 798
795 def _countItems(self, cursor, authorized_groups, unrestricted): 799 def _countItems(self, cursor, authorized_groups, unrestricted):
800 # FIXME: should not be a separate method, but should be an option of getItems instead
796 self._checkNodeExists(cursor) 801 self._checkNodeExists(cursor)
797 802
798 if unrestricted: 803 if unrestricted:
799 query = ["""SELECT count(item_id) FROM nodes 804 query = ["""SELECT count(item_id) FROM nodes
800 INNER JOIN items USING (node_id) 805 INNER JOIN items USING (node_id)
860 def getItemsById(self, authorized_groups, unrestricted, itemIdentifiers): 865 def getItemsById(self, authorized_groups, unrestricted, itemIdentifiers):
861 """ Get items which are in the given list 866 """ Get items which are in the given list
862 @param authorized_groups: we want to get items that these groups can access 867 @param authorized_groups: we want to get items that these groups can access
863 @param unrestricted: if true, don't check permissions 868 @param unrestricted: if true, don't check permissions
864 @param itemIdentifiers: list of ids of the items we want to get 869 @param itemIdentifiers: list of ids of the items we want to get
865 @return: list of (item, access_model, access_list) if unrestricted is True, else list of items 870 @return: list of container.ItemData
866 access_list is managed as a dictionnary with same key as for item_config 871 ItemData.config will contains access_list (managed as a dictionnary with same key as for item_config)
872 if unrestricted is False, access_model and config will be None
867 """ 873 """
868 return self.dbpool.runInteraction(self._getItemsById, authorized_groups, unrestricted, itemIdentifiers) 874 return self.dbpool.runInteraction(self._getItemsById, authorized_groups, unrestricted, itemIdentifiers)
869 875
870 876
871 def _getItemsById(self, cursor, authorized_groups, unrestricted, itemIdentifiers): 877 def _getItemsById(self, cursor, authorized_groups, unrestricted, itemIdentifiers):
888 access_list = {} 894 access_list = {}
889 if access_model == const.VAL_AMODEL_ROSTER: #TODO: jid access_model 895 if access_model == const.VAL_AMODEL_ROSTER: #TODO: jid access_model
890 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,)) 896 cursor.execute('SELECT groupname FROM item_groups_authorized WHERE item_id=%s', (item_id,))
891 access_list[const.OPT_ROSTER_GROUPS_ALLOWED] = [r[0] for r in cursor.fetchall()] 897 access_list[const.OPT_ROSTER_GROUPS_ALLOWED] = [r[0] for r in cursor.fetchall()]
892 898
893 ret.append((item, access_model, access_list)) 899 ret.append(container.ItemData(item, access_model, access_list))
894 else: #we check permission before returning items 900 else: #we check permission before returning items
895 for itemIdentifier in itemIdentifiers: 901 for itemIdentifier in itemIdentifiers:
896 args = [self.nodeDbId, itemIdentifier] 902 args = [self.nodeDbId, itemIdentifier]
897 if authorized_groups: 903 if authorized_groups:
898 args.append(authorized_groups) 904 args.append(authorized_groups)
904 ("or (items.access_model='roster' and groupname in %s)" if authorized_groups else '') + ")", 910 ("or (items.access_model='roster' and groupname in %s)" if authorized_groups else '') + ")",
905 args) 911 args)
906 912
907 result = cursor.fetchone() 913 result = cursor.fetchone()
908 if result: 914 if result:
909 ret.append(generic.stripNamespace(parseXml(result[0]))) 915 ret.append(container.ItemData(generic.stripNamespace(parseXml(result[0])), None, None))
910 916
911 return ret 917 return ret
912 918
913 919
914 def getItemsPublishers(self, itemIdentifiers): 920 def getItemsPublishers(self, itemIdentifiers):