annotate idavoll/pgsql_backend.py @ 28:39d0c6fa027f

Initial revision
author Ralph Meijer <ralphm@ik.nu>
date Mon, 18 Oct 2004 15:29:25 +0000
parents
children ea3d3544a52e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
28
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
1 from twisted.application import service
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
2 from twisted.internet import defer
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
3 from twisted.protocols.jabber import jid
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
4 from twisted.enterprise import adbapi
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
5 import backend
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
6
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
7 class Service(service.Service):
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
8 """ PostgreSQL backend Service for a JEP-0060 pubsub service """
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
9
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
10 __implements__ = backend.IService
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
11
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
12 def __init__(self):
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
13 self.dbpool = adbapi.ConnectionPool('pyPgSQL.PgSQL', user='ralphm',
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
14 database='pubsub_test')
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
15
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
16 def _do_publish(self, cursor, node_id, publisher, items):
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
17 cursor.execute("""SELECT persistent, deliver_payload FROM nodes
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
18 WHERE node=%s""",
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
19 (node_id,))
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
20 try:
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
21 persist_items, deliver_payloads = cursor.fetchone()
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
22 except TypeError:
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
23 raise backend.NodeNotFound
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
24
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
25 cursor.execute("""SELECT 1 FROM affiliations
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
26 JOIN nodes ON (node_id=nodes.id)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
27 JOIN entities ON (entity_id=entities.id)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
28 WHERE node=%s AND jid=%s AND
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
29 affiliation IN ('owner', 'publisher')""",
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
30 (node_id.encode('utf8'), publisher.encode('utf8')))
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
31
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
32 if not cursor.fetchone():
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
33 raise backend.NotAuthorized
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
34
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
35 if items and not persist_items and not deliver_payloads:
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
36 raise backend.NoPayloadAllowed
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
37 elif not items and (persist_items or deliver_payloads):
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
38 raise backend.PayloadExpected
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
39
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
40 print "publish by %s to %s" % (publisher, node_id)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
41
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
42 if persist_items or deliver_payloads:
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
43 for item in items:
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
44 if item["id"] is None:
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
45 item["id"] = 'random' # FIXME
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
46
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
47 if persist_items:
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
48 self.storeItems(node_id, publisher, items)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
49
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
50 if items and not deliver_payloads:
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
51 for item in items:
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
52 item.children = []
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
53
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
54 recipients = self.get_subscribers(node_id)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
55 recipients.addCallback(self.magic_filter, node_id, items)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
56 recipients.addCallback(self.pubsub_service.do_notification, node_id)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
57
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
58 def do_publish(self, node_id, publisher, items):
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
59 d = self.dbpool.runInteraction(self._do_publish, node_id, publisher, items)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
60 return d
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
61
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
62 def magic_filter(self, subscribers, node_id, items):
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
63 list = {}
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
64 for subscriber in subscribers:
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
65 list[subscriber] = items
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
66
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
67 return list
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
68
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
69 def get_subscribers(self, node_id):
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
70 d = self.dbpool.runQuery("""SELECT jid, resource FROM subscriptions
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
71 JOIN nodes ON (node_id=nodes.id)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
72 JOIN entities ON (entity_id=entities.id)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
73 WHERE node=%s AND
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
74 subscription='subscribed'""",
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
75 (node_id.encode('utf8'),))
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
76 d.addCallback(self.convert_to_jids)
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
77 return d
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
78
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
79 def convert_to_jids(self, list):
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
80 return [jid.JID("%s/%s" % (l[0], l[1])).full() for l in list]
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
81
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
82 def storeItems(self, node_id, publisher, items):
39d0c6fa027f Initial revision
Ralph Meijer <ralphm@ik.nu>
parents:
diff changeset
83 pass