annotate src/plugins/plugin_misc_tickets.py @ 2411:f9167c053475

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