annotate idavoll/pgsql_storage.py @ 191:48245777acea

Return proper HTTP status codes on failed un-/subscription.
author Ralph Meijer <ralphm@ik.nu>
date Sat, 17 May 2008 18:31:31 +0000
parents 0d4474051eeb
children e404775b12df
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 170
diff changeset
1 # Copyright (c) 2003-2008 Ralph Meijer
155
5191ba7c4df8 Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents: 148
diff changeset
2 # See LICENSE for details.
5191ba7c4df8 Work towards first release 0.5.0.
Ralph Meijer <ralphm@ik.nu>
parents: 148
diff changeset
3
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
4 import copy
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
5 from twisted.enterprise import adbapi
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
6 from twisted.words.protocols.jabber import jid
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
7 from zope.interface import implements
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
8
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
9 from idavoll import error, iidavoll
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
10
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
11 class Storage:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
12
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
13 implements(iidavoll.IStorage)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
14
170
958e69630e52 Provide twistd parameters for connecting to a remote PostgreSQL database.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
15 def __init__(self, user, database, password=None, host=None, port=None):
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
16 self._dbpool = adbapi.ConnectionPool('pyPgSQL.PgSQL',
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
17 user=user,
163
1701c0e2c707 Add --dbpass option for passing a password to the PostgreSQL backend.
Ralph Meijer <ralphm@ik.nu>
parents: 155
diff changeset
18 password=password,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
19 database=database,
170
958e69630e52 Provide twistd parameters for connecting to a remote PostgreSQL database.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
20 host=host,
958e69630e52 Provide twistd parameters for connecting to a remote PostgreSQL database.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
21 port=port,
958e69630e52 Provide twistd parameters for connecting to a remote PostgreSQL database.
Ralph Meijer <ralphm@ik.nu>
parents: 167
diff changeset
22 cp_reconnect=True,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
23 client_encoding='utf-8'
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
24 )
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
25
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
26 def get_node(self, node_id):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
27 return self._dbpool.runInteraction(self._get_node, node_id)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
28
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
29 def _get_node(self, cursor, node_id):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
30 configuration = {}
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
31 cursor.execute("""SELECT persistent, deliver_payload,
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
32 send_last_published_item
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
33 FROM nodes
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
34 WHERE node=%s""",
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
35 (node_id,))
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
36 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
37 (configuration["pubsub#persist_items"],
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
38 configuration["pubsub#deliver_payloads"],
181
Ralph Meijer <ralphm@ik.nu>
parents: 180
diff changeset
39 configuration["pubsub#send_last_published_item"]) = \
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
40 cursor.fetchone()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
41 except TypeError:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
42 raise error.NodeNotFound()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
43 else:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
44 node = LeafNode(node_id, configuration)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
45 node._dbpool = self._dbpool
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
46 return node
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
47
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
48 def get_node_ids(self):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
49 d = self._dbpool.runQuery("""SELECT node from nodes""")
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
50 d.addCallback(lambda results: [r[0] for r in results])
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
51 return d
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
52
173
246911bcb699 Make node type part of node configuration.
Ralph Meijer <ralphm@ik.nu>
parents: 171
diff changeset
53 def create_node(self, node_id, owner, config=None):
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
54 return self._dbpool.runInteraction(self._create_node, node_id, owner)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
55
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
56 def _create_node(self, cursor, node_id, owner):
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
57 node_id = node_id
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
58 owner = owner.userhost()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
59 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
60 cursor.execute("""INSERT INTO nodes (node) VALUES (%s)""",
113
dfef919aaf1b Fix interpretations of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
61 (node_id))
dfef919aaf1b Fix interpretations of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
62 except cursor._pool.dbapi.OperationalError:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
63 raise error.NodeExists()
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
64
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
65 cursor.execute("""SELECT 1 from entities where jid=%s""",
113
dfef919aaf1b Fix interpretations of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
66 (owner))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
67
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
68 if not cursor.fetchone():
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
69 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
113
dfef919aaf1b Fix interpretations of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
70 (owner))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
71
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
72 cursor.execute("""INSERT INTO affiliations
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
73 (node_id, entity_id, affiliation)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
74 SELECT n.id, e.id, 'owner' FROM
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
75 (SELECT id FROM nodes WHERE node=%s) AS n
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
76 CROSS JOIN
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
77 (SELECT id FROM entities WHERE jid=%s) AS e""",
113
dfef919aaf1b Fix interpretations of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
78 (node_id, owner))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
79
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
80 def delete_node(self, node_id):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
81 return self._dbpool.runInteraction(self._delete_node, node_id)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
82
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
83 def _delete_node(self, cursor, node_id):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
84 cursor.execute("""DELETE FROM nodes WHERE node=%s""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
85 (node_id,))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
86
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
87 if cursor.rowcount != 1:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
88 raise error.NodeNotFound()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
89
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
90 def get_affiliations(self, entity):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
91 d = self._dbpool.runQuery("""SELECT node, affiliation FROM entities
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
92 JOIN affiliations ON
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
93 (affiliations.entity_id=entities.id)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
94 JOIN nodes ON
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
95 (nodes.id=affiliations.node_id)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
96 WHERE jid=%s""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
97 (entity.userhost(),))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
98 d.addCallback(lambda results: [tuple(r) for r in results])
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
99 return d
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
100
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
101 def get_subscriptions(self, entity):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
102 d = self._dbpool.runQuery("""SELECT node, jid, resource, subscription
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
103 FROM entities JOIN subscriptions ON
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
104 (subscriptions.entity_id=entities.id)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
105 JOIN nodes ON
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
106 (nodes.id=subscriptions.node_id)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
107 WHERE jid=%s""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
108 (entity.userhost(),))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
109 d.addCallback(self._convert_subscription_jids)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
110 return d
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
111
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
112 def _convert_subscription_jids(self, subscriptions):
148
b03e5ad81173 Change all calls to jid.JID to jid.internJID to avoid redoing stringprep.
Ralph Meijer <ralphm@ik.nu>
parents: 146
diff changeset
113 return [(node,
b03e5ad81173 Change all calls to jid.JID to jid.internJID to avoid redoing stringprep.
Ralph Meijer <ralphm@ik.nu>
parents: 146
diff changeset
114 jid.internJID('%s/%s' % (subscriber, resource)),
b03e5ad81173 Change all calls to jid.JID to jid.internJID to avoid redoing stringprep.
Ralph Meijer <ralphm@ik.nu>
parents: 146
diff changeset
115 subscription)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
116 for node, subscriber, resource, subscription in subscriptions]
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
117
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 170
diff changeset
118
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
119 class Node:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
120
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
121 implements(iidavoll.INode)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
122
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
123 def __init__(self, node_id, config):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
124 self.id = node_id
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
125 self._config = config
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
126
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
127 def _check_node_exists(self, cursor):
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
128 cursor.execute("""SELECT id FROM nodes WHERE node=%s""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
129 (self.id))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
130 if not cursor.fetchone():
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
131 raise error.NodeNotFound()
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
132
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
133 def get_type(self):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
134 return self.type
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
135
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
136 def get_configuration(self):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
137 return self._config
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
138
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
139 def set_configuration(self, options):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
140 config = copy.copy(self._config)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
141
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
142 for option in options:
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
143 if option in config:
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
144 config[option] = options[option]
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
145
124
c4ee16bc48e5 Change Node.set_configuration() to set cached configuration in a callback.
Ralph Meijer <ralphm@ik.nu>
parents: 121
diff changeset
146 d = self._dbpool.runInteraction(self._set_configuration, config)
c4ee16bc48e5 Change Node.set_configuration() to set cached configuration in a callback.
Ralph Meijer <ralphm@ik.nu>
parents: 121
diff changeset
147 d.addCallback(self._set_cached_configuration, config)
c4ee16bc48e5 Change Node.set_configuration() to set cached configuration in a callback.
Ralph Meijer <ralphm@ik.nu>
parents: 121
diff changeset
148 return d
c4ee16bc48e5 Change Node.set_configuration() to set cached configuration in a callback.
Ralph Meijer <ralphm@ik.nu>
parents: 121
diff changeset
149
c4ee16bc48e5 Change Node.set_configuration() to set cached configuration in a callback.
Ralph Meijer <ralphm@ik.nu>
parents: 121
diff changeset
150 def _set_configuration(self, cursor, config):
c4ee16bc48e5 Change Node.set_configuration() to set cached configuration in a callback.
Ralph Meijer <ralphm@ik.nu>
parents: 121
diff changeset
151 self._check_node_exists(cursor)
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
152 cursor.execute("""UPDATE nodes SET persistent=%s, deliver_payload=%s,
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
153 send_last_published_item=%s
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
154 WHERE node=%s""",
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
155 (config["pubsub#persist_items"],
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
156 config["pubsub#deliver_payloads"],
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
157 config["pubsub#send_last_published_item"],
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
158 self.id))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
159
124
c4ee16bc48e5 Change Node.set_configuration() to set cached configuration in a callback.
Ralph Meijer <ralphm@ik.nu>
parents: 121
diff changeset
160 def _set_cached_configuration(self, void, config):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
161 self._config = config
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
162
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
163 def get_meta_data(self):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
164 config = copy.copy(self._config)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
165 config["pubsub#node_type"] = self.type
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
166 return config
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
167
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
168 def get_affiliation(self, entity):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
169 return self._dbpool.runInteraction(self._get_affiliation, entity)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
170
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
171 def _get_affiliation(self, cursor, entity):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
172 self._check_node_exists(cursor)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
173 cursor.execute("""SELECT affiliation FROM affiliations
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
174 JOIN nodes ON (node_id=nodes.id)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
175 JOIN entities ON (entity_id=entities.id)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
176 WHERE node=%s AND jid=%s""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
177 (self.id,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
178 entity.userhost()))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
179
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
180 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
181 return cursor.fetchone()[0]
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
182 except TypeError:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
183 return None
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
184
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
185 def get_subscription(self, subscriber):
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
186 return self._dbpool.runInteraction(self._get_subscription, subscriber)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
187
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
188 def _get_subscription(self, cursor, subscriber):
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
189 self._check_node_exists(cursor)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
190
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
191 userhost = subscriber.userhost()
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
192 resource = subscriber.resource or ''
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
193
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
194 cursor.execute("""SELECT subscription FROM subscriptions
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
195 JOIN nodes ON (nodes.id=subscriptions.node_id)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
196 JOIN entities ON
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
197 (entities.id=subscriptions.entity_id)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
198 WHERE node=%s AND jid=%s AND resource=%s""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
199 (self.id,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
200 userhost,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
201 resource))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
202 try:
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
203 return cursor.fetchone()[0]
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
204 except TypeError:
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
205 return None
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
206
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
207 def add_subscription(self, subscriber, state):
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
208 return self._dbpool.runInteraction(self._add_subscription, subscriber,
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
209 state)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
210
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
211 def _add_subscription(self, cursor, subscriber, state):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
212 self._check_node_exists(cursor)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
213
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
214 userhost = subscriber.userhost()
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
215 resource = subscriber.resource or ''
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
216
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
217 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
218 cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
219 (userhost))
113
dfef919aaf1b Fix interpretations of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
220 except cursor._pool.dbapi.OperationalError:
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
221 pass
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
222
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
223 try:
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
224 cursor.execute("""INSERT INTO subscriptions
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
225 (node_id, entity_id, resource, subscription)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
226 SELECT n.id, e.id, %s, %s FROM
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
227 (SELECT id FROM nodes WHERE node=%s) AS n
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
228 CROSS JOIN
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
229 (SELECT id FROM entities WHERE jid=%s) AS e""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
230 (resource,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
231 state,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
232 self.id,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
233 userhost))
113
dfef919aaf1b Fix interpretations of JIDs.
Ralph Meijer <ralphm@ik.nu>
parents: 107
diff changeset
234 except cursor._pool.dbapi.OperationalError:
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
235 raise error.SubscriptionExists()
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
236
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
237 def remove_subscription(self, subscriber):
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
238 return self._dbpool.runInteraction(self._remove_subscription,
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
239 subscriber)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
240
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
241 def _remove_subscription(self, cursor, subscriber):
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
242 self._check_node_exists(cursor)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
243
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
244 userhost = subscriber.userhost()
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
245 resource = subscriber.resource or ''
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
246
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
247 cursor.execute("""DELETE FROM subscriptions WHERE
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
248 node_id=(SELECT id FROM nodes WHERE node=%s) AND
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
249 entity_id=(SELECT id FROM entities WHERE jid=%s)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
250 AND resource=%s""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
251 (self.id,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
252 userhost,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
253 resource))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
254 if cursor.rowcount != 1:
171
bc269696ef42 Reply with the correct error condition on subscription when not subscribed.
Ralph Meijer <ralphm@ik.nu>
parents: 170
diff changeset
255 raise error.NotSubscribed()
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
256
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
257 return None
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
258
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
259 def get_subscribers(self):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
260 d = self._dbpool.runInteraction(self._get_subscribers)
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
261 d.addCallback(self._convert_to_jids)
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
262 return d
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
263
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
264 def _get_subscribers(self, cursor):
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
265 self._check_node_exists(cursor)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
266 cursor.execute("""SELECT jid, resource FROM subscriptions
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
267 JOIN nodes ON (node_id=nodes.id)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
268 JOIN entities ON (entity_id=entities.id)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
269 WHERE node=%s AND
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
270 subscription='subscribed'""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
271 (self.id,))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
272 return cursor.fetchall()
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
273
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
274 def _convert_to_jids(self, list):
148
b03e5ad81173 Change all calls to jid.JID to jid.internJID to avoid redoing stringprep.
Ralph Meijer <ralphm@ik.nu>
parents: 146
diff changeset
275 return [jid.internJID("%s/%s" % (l[0], l[1])) for l in list]
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
276
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
277 def is_subscribed(self, entity):
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
278 return self._dbpool.runInteraction(self._is_subscribed, entity)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
279
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
280 def _is_subscribed(self, cursor, entity):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
281 self._check_node_exists(cursor)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
282
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
283 cursor.execute("""SELECT 1 FROM entities
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
284 JOIN subscriptions ON
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
285 (entities.id=subscriptions.entity_id)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
286 JOIN nodes ON
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
287 (nodes.id=subscriptions.node_id)
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
288 WHERE entities.jid=%s
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
289 AND node=%s AND subscription='subscribed'""",
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
290 (entity.userhost(),
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
291 self.id))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
292
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
293 return cursor.fetchone() is not None
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
294
145
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
295 def get_affiliations(self):
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
296 return self._dbpool.runInteraction(self._get_affiliations)
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
297
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
298 def _get_affiliations(self, cursor):
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
299 self._check_node_exists(cursor)
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
300
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
301 cursor.execute("""SELECT jid, affiliation FROM nodes
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
302 JOIN affiliations ON
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
303 (nodes.id = affiliations.node_id)
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
304 JOIN entities ON
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
305 (affiliations.entity_id = entities.id)
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
306 WHERE node=%s""",
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
307 self.id)
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
308 result = cursor.fetchall()
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
309
148
b03e5ad81173 Change all calls to jid.JID to jid.internJID to avoid redoing stringprep.
Ralph Meijer <ralphm@ik.nu>
parents: 146
diff changeset
310 return [(jid.internJID(r[0]), r[1]) for r in result]
145
f393bccec4bc Add get_affiliations to Node class in storage facilities in preparation of
Ralph Meijer <ralphm@ik.nu>
parents: 142
diff changeset
311
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
312 class LeafNodeMixin:
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
313
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
314 type = 'leaf'
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
315
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
316 def store_items(self, items, publisher):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
317 return self._dbpool.runInteraction(self._store_items, items, publisher)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
318
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
319 def _store_items(self, cursor, items, publisher):
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
320 self._check_node_exists(cursor)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
321 for item in items:
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
322 self._store_item(cursor, item, publisher)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
323
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
324 def _store_item(self, cursor, item, publisher):
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
325 data = item.toXml()
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
326 cursor.execute("""UPDATE items SET date=now(), publisher=%s, data=%s
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
327 FROM nodes
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
328 WHERE nodes.id = items.node_id AND
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
329 nodes.node = %s and items.item=%s""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
330 (publisher.full(),
127
d3689da18ed2 Don't use encode('utf-8') on serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 124
diff changeset
331 data,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
332 self.id,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
333 item["id"]))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
334 if cursor.rowcount == 1:
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
335 return
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
336
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
337 cursor.execute("""INSERT INTO items (node_id, item, publisher, data)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
338 SELECT id, %s, %s, %s FROM nodes WHERE node=%s""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
339 (item["id"],
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
340 publisher.full(),
127
d3689da18ed2 Don't use encode('utf-8') on serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 124
diff changeset
341 data,
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
342 self.id))
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
343
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
344 def remove_items(self, item_ids):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
345 return self._dbpool.runInteraction(self._remove_items, item_ids)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
346
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
347 def _remove_items(self, cursor, item_ids):
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
348 self._check_node_exists(cursor)
180
fc687620599b Properly add send_last_published_item configuration item.
Ralph Meijer <ralphm@ik.nu>
parents: 173
diff changeset
349
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
350 deleted = []
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
351
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
352 for item_id in item_ids:
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
353 cursor.execute("""DELETE FROM items WHERE
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
354 node_id=(SELECT id FROM nodes WHERE node=%s) AND
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
355 item=%s""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
356 (self.id,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
357 item_id))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
358
142
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 136
diff changeset
359 if cursor.rowcount:
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 136
diff changeset
360 deleted.append(item_id)
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 136
diff changeset
361
812300cdbc22 Changed behaviour of retraction of items so that only the actually deleted
Ralph Meijer <ralphm@ik.nu>
parents: 136
diff changeset
362 return deleted
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
363
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
364 def get_items(self, max_items=None):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
365 return self._dbpool.runInteraction(self._get_items, max_items)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
366
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
367 def _get_items(self, cursor, max_items):
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
368 self._check_node_exists(cursor)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
369 query = """SELECT data FROM nodes JOIN items ON
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
370 (nodes.id=items.node_id)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
371 WHERE node=%s ORDER BY date DESC"""
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
372 if max_items:
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
373 cursor.execute(query + " LIMIT %s",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
374 (self.id,
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
375 max_items))
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
376 else:
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
377 cursor.execute(query, (self.id))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
378
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
379 result = cursor.fetchall()
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
380 return [unicode(r[0], 'utf-8') for r in result]
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
381
127
d3689da18ed2 Don't use encode('utf-8') on serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 124
diff changeset
382 def get_items_by_id(self, item_ids):
d3689da18ed2 Don't use encode('utf-8') on serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 124
diff changeset
383 return self._dbpool.runInteraction(self._get_items_by_id, item_ids)
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
384
127
d3689da18ed2 Don't use encode('utf-8') on serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 124
diff changeset
385 def _get_items_by_id(self, cursor, item_ids):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
386 self._check_node_exists(cursor)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
387 items = []
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
388 for item_id in item_ids:
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
389 cursor.execute("""SELECT data FROM nodes JOIN items ON
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
390 (nodes.id=items.node_id)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
391 WHERE node=%s AND item=%s""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
392 (self.id,
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
393 item_id))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
394 result = cursor.fetchone()
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
395 if result:
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
396 items.append(unicode(result[0], 'utf-8'))
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
397 return items
107
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
398
d252d793f0ed Initial revision.
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
399 def purge(self):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
400 return self._dbpool.runInteraction(self._purge)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
401
127
d3689da18ed2 Don't use encode('utf-8') on serialized XML.
Ralph Meijer <ralphm@ik.nu>
parents: 124
diff changeset
402 def _purge(self, cursor):
121
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
403 self._check_node_exists(cursor)
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
404
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
405 cursor.execute("""DELETE FROM items WHERE
4f0113adb7ed Add Node._check_node_exists() calls to all Node methods, because nodes could
Ralph Meijer <ralphm@ik.nu>
parents: 113
diff changeset
406 node_id=(SELECT id FROM nodes WHERE node=%s)""",
136
327de183f48d Discover client_encoding parameter to pyPgSQL, removing all encode() calls.
Ralph Meijer <ralphm@ik.nu>
parents: 127
diff changeset
407 (self.id,))
146
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
408
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
409 class LeafNode(Node, LeafNodeMixin):
b4490bdc77e5 Change semantics of Node.is_subscribed() to match all subscriptions for an
Ralph Meijer <ralphm@ik.nu>
parents: 145
diff changeset
410
167
ef22e4150caa Move protocol implementations (pubsub, disco, forms) to and depend on wokkel.
Ralph Meijer <ralphm@ik.nu>
parents: 163
diff changeset
411 implements(iidavoll.ILeafNode)