diff src/plugins/plugin_misc_tickets.py @ 2420:03da3ef5fb5b

plugin tickets: added ticketsSet and ticketsSchemaGet methods: those methods are high level methods specialised for tickets. ticketsSet will use default tickets node if node is not set, set "created" and "updated" fields, create comments node if the ticket is new, and associate it with "comments_uri" field. ticketsSchemaGet is like getUISchema with node defaulting to tickets default node.
author Goffi <goffi@goffi.org>
date Sun, 05 Nov 2017 15:36:06 +0100
parents f05c884cd3ef
children 3faf18111d61
line wrap: on
line diff
--- a/src/plugins/plugin_misc_tickets.py	Sun Nov 05 13:53:28 2017 +0100
+++ b/src/plugins/plugin_misc_tickets.py	Sun Nov 05 15:36:06 2017 +0100
@@ -21,8 +21,11 @@
 from sat.core.constants import Const as C
 from twisted.words.protocols.jabber import jid
 from twisted.internet import defer
+from wokkel import generic
 from sat.tools import utils
+from sat.tools.common import uri
 from sat.core.log import getLogger
+import shortuuid
 log = getLogger(__name__)
 
 NS_TICKETS = 'org.salut-a-toi.tickets:0'
@@ -32,7 +35,7 @@
     C.PI_IMPORT_NAME: "TICKETS",
     C.PI_TYPE: "EXP",
     C.PI_PROTOCOLS: [],
-    C.PI_DEPENDENCIES: ["XEP-0060", "PUBSUB_SCHEMA"],
+    C.PI_DEPENDENCIES: ["XEP-0060", "PUBSUB_SCHEMA", "XEP-0277"],
     C.PI_MAIN: "Tickets",
     C.PI_HANDLER: "no",
     C.PI_DESCRIPTION: _("""Tickets management plugin""")
@@ -46,11 +49,20 @@
         self.host = host
         self._p = self.host.plugins["XEP-0060"]
         self._s = self.host.plugins["PUBSUB_SCHEMA"]
+        self._m = self.host.plugins["XEP-0277"]
         host.bridge.addMethod("ticketsGet", ".plugin",
                               in_sign='ssiassa{ss}s', out_sign='(asa{ss})',
                               method=self._get,
                               async=True
                               )
+        host.bridge.addMethod("ticketsSet", ".plugin",
+                              in_sign='ssa{sas}ssa{ss}s', out_sign='s',
+                              method=self._set,
+                              async=True)
+        host.bridge.addMethod("ticketsSchemaGet", ".plugin",
+                              in_sign='sss', out_sign='s',
+                              method=self._getSchema,
+                              async=True)
 
     def _labelsFilter(self, widget_type, args, kwargs):
         if widget_type != u'textbox':
@@ -107,3 +119,58 @@
             )
 
         defer.returnValue((tickets, metadata))
+
+    def _set(self, service, node, values, schema=None, item_id=None, extra=None, profile_key=C.PROF_KEY_NONE):
+        client = self.host.getClient(profile_key)
+        service = None if not service else jid.JID(service)
+        if schema:
+            schema = generic.parseXml(schema.encode('utf-8'))
+        else:
+            schema = None
+        d = self.set(client, service, node or None, values, schema, item_id or None, extra, deserialise=True)
+        d.addCallback(lambda ret: ret or u'')
+        return d
+
+    @defer.inlineCallbacks
+    def set(self, client, service, node, values, schema=None, item_id=None, extra=None, deserialise=False):
+        """Publish a tickets
+
+        @param node(unicode, None): Pubsub node to use
+            None to use default tickets node
+        @param values(dict[key(unicode), [iterable[object], object]]): values of the ticket
+            if not iterable, will be put in a list
+            'created' and 'updated' will be forced to current time:
+                - 'created' is set if item_id is None, i.e. if it's a new ticket
+                - 'updated' is set everytime
+        other arguments are same as for [self._s.sendDataFormItem]
+        @return (unicode): id of the created item
+        """
+        if not node:
+            node = NS_TICKETS
+        now = utils.xmpp_date()
+        if not item_id:
+            values['created'] = now
+            comments_service = self._m.getCommentsService(client)
+
+            # we need to use uuid for comments node, because we don't know item id in advance
+            # (we don't want to set it ourselves to let the server choose, so we can have
+            #  a nicer id if serial ids is activated)
+            comments_node = self._m.getCommentsNode(node + u'_' + unicode(shortuuid.uuid()))
+            options = {self._p.OPT_ACCESS_MODEL: self._p.ACCESS_OPEN,
+                       self._p.OPT_PERSIST_ITEMS: 1,
+                       self._p.OPT_MAX_ITEMS: -1,
+                       self._p.OPT_DELIVER_PAYLOADS: 1,
+                       self._p.OPT_SEND_ITEM_SUBSCRIBE: 1,
+                       self._p.OPT_PUBLISH_MODEL: self._p.ACCESS_OPEN,
+                       }
+            yield self._p.createNode(client, comments_service, comments_node, options)
+            values['comments_uri'] = uri.buildXMPPUri(u'pubsub', subtype='microblog', path=comments_service.full(), node=comments_node)
+
+        values['updated'] = now
+        item_id = yield self._s.sendDataFormItem(client, service, node, values, schema, item_id, extra, deserialise)
+        defer.returnValue(item_id)
+
+    def _getSchema(self, service, node, profile_key=C.PROF_KEY_NONE):
+        if not node:
+            node = NS_TICKETS
+        return self._s._getUISchema(service, node, profile_key)