comparison src/plugins/plugin_misc_tickets.py @ 2404:f05c884cd3ef

plugin tickets: high level tickets handling, first draft: this plugin use plugin schema to handle tickets, and filter well known fields like created, updated and labels.
author Goffi <goffi@goffi.org>
date Fri, 27 Oct 2017 18:24:35 +0200
parents
children 03da3ef5fb5b
comparison
equal deleted inserted replaced
2403:dec31114c402 2404:f05c884cd3ef
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for Pubsub Schemas
5 # Copyright (C) 2009-2017 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.core.constants import Const as C
22 from twisted.words.protocols.jabber import jid
23 from twisted.internet import defer
24 from sat.tools import utils
25 from sat.core.log import getLogger
26 log = getLogger(__name__)
27
28 NS_TICKETS = 'org.salut-a-toi.tickets:0'
29
30 PLUGIN_INFO = {
31 C.PI_NAME: _("Tickets management"),
32 C.PI_IMPORT_NAME: "TICKETS",
33 C.PI_TYPE: "EXP",
34 C.PI_PROTOCOLS: [],
35 C.PI_DEPENDENCIES: ["XEP-0060", "PUBSUB_SCHEMA"],
36 C.PI_MAIN: "Tickets",
37 C.PI_HANDLER: "no",
38 C.PI_DESCRIPTION: _("""Tickets management plugin""")
39 }
40
41
42 class Tickets(object):
43
44 def __init__(self, host):
45 log.info(_(u"Tickets plugin initialization"))
46 self.host = host
47 self._p = self.host.plugins["XEP-0060"]
48 self._s = self.host.plugins["PUBSUB_SCHEMA"]
49 host.bridge.addMethod("ticketsGet", ".plugin",
50 in_sign='ssiassa{ss}s', out_sign='(asa{ss})',
51 method=self._get,
52 async=True
53 )
54
55 def _labelsFilter(self, widget_type, args, kwargs):
56 if widget_type != u'textbox':
57 return widget_type, args, kwargs
58 widget_type = u'list'
59 options = [o for o in args.pop(0).split(u'\n') if o]
60 kwargs = {'options': options,
61 'name': kwargs.get('name'),
62 'styles': (u'noselect', u'extensible', u'reducible')}
63 return widget_type, args, kwargs
64
65 def _dateFilter(self, widget_type, args, kwargs):
66 if widget_type != u'string':
67 return widget_type, args, kwargs
68 # we convert XMPP date to timestamp
69 args[0] = unicode(utils.date_parse(args[0]))
70 return widget_type, args, kwargs
71
72 def _get(self, service='', node='', max_items=10, item_ids=None, sub_id=None, extra_dict=None, profile_key=C.PROF_KEY_NONE):
73 client = self.host.getClient(profile_key)
74 service = jid.JID(service) if service else None
75 max_items = None if max_items == C.NO_LIMIT else max_items
76 extra = self._p.parseExtra(extra_dict)
77 d = self.get(client, service, node or None, max_items, item_ids, sub_id or None, extra.rsm_request, extra.extra)
78 d.addCallback(self._p.serItemsData)
79 return d
80
81 @defer.inlineCallbacks
82 def get(self, client, service=None, node=None, max_items=None, item_ids=None, sub_id=None, rsm_request=None, extra=None):
83 """Retrieve tickets and convert them to XMLUI
84
85 @param node(unicode, None): PubSub node to use
86 if None, default ticket node will be used
87 other parameters as the same as for [XEP_0060.getItems]
88 @return (list[unicode]): XMLUI of the tickets
89 """
90 if not node:
91 node = NS_TICKETS
92 filters = {u'labels': self._labelsFilter,
93 u'created': self._dateFilter,
94 u'updated': self._dateFilter,
95 }
96 tickets, metadata = yield self._s.getDataFormItems(
97 client,
98 NS_TICKETS,
99 service,
100 node,
101 max_items = max_items,
102 item_ids = item_ids,
103 sub_id = sub_id,
104 rsm_request = rsm_request,
105 extra = extra,
106 filters = filters,
107 )
108
109 defer.returnValue((tickets, metadata))