annotate src/plugins/plugin_misc_tickets.py @ 2459:a9c092bf4ee9

plugin XEP-0060: added an config option to set pubsub service "pubsub_service" can now be used in [DEFAULT] section. If not found or invalid, first pubsub service found will be used fix 216
author Goffi <goffi@goffi.org>
date Tue, 12 Dec 2017 01:07:25 +0100
parents 9e692f09f367
children 544c4d2fec45
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
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
22 from sat.core import exceptions
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.protocols.jabber import jid
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import defer
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
25 from wokkel import generic
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.tools import utils
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
27 from sat.tools.common import uri
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.core.log import getLogger
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
29 import shortuuid
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
30 from wokkel import data_form
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 log = getLogger(__name__)
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 NS_TICKETS = 'org.salut-a-toi.tickets:0'
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 PLUGIN_INFO = {
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_NAME: _("Tickets management"),
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_IMPORT_NAME: "TICKETS",
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_TYPE: "EXP",
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 C.PI_PROTOCOLS: [],
2430
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
40 C.PI_DEPENDENCIES: ["XEP-0060", "PUBSUB_SCHEMA", "XEP-0277", "IDENTITY"],
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_MAIN: "Tickets",
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 C.PI_HANDLER: "no",
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 C.PI_DESCRIPTION: _("""Tickets management plugin""")
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 }
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 class Tickets(object):
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 def __init__(self, host):
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 log.info(_(u"Tickets plugin initialization"))
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.host = host
2443
81a45e7886c9 core: added a mechanism to associate short names to namespaces:
Goffi <goffi@goffi.org>
parents: 2434
diff changeset
52 host.registerNamespace('tickets', NS_TICKETS)
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 self._p = self.host.plugins["XEP-0060"]
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self._s = self.host.plugins["PUBSUB_SCHEMA"]
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
55 self._m = self.host.plugins["XEP-0277"]
2430
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
56 self._i = self.host.plugins["IDENTITY"]
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 host.bridge.addMethod("ticketsGet", ".plugin",
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 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
59 method=self._get,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 async=True
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 )
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
62 host.bridge.addMethod("ticketSet", ".plugin",
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
63 in_sign='ssa{sas}ssa{ss}s', out_sign='s',
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
64 method=self._set,
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
65 async=True)
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
66 host.bridge.addMethod("ticketsSchemaGet", ".plugin",
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
67 in_sign='sss', out_sign='s',
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
68 method=self._getSchema,
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
69 async=True)
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70
2430
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
71 def _reporterFilter(self, client, form_xmlui, widget_type, args, kwargs):
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
72 if not args[0]:
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
73 # if reporter is not filled, we use user part of publisher
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
74 # (if we have it)
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
75 try:
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
76 publisher = jid.JID(form_xmlui.named_widgets['publisher'].value)
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
77 except (KeyError, RuntimeError):
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
78 pass
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
79 else:
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
80 args[0] = publisher.user.capitalize()
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
81 return widget_type, args, kwargs
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
82
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
83 def _labelsFilter(self, form_xmlui, widget_type, args, kwargs):
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 if widget_type != u'textbox':
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 return widget_type, args, kwargs
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 widget_type = u'list'
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 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
88 kwargs = {'options': options,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 'name': kwargs.get('name'),
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 'styles': (u'noselect', u'extensible', u'reducible')}
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 return widget_type, args, kwargs
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92
2430
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
93 def _dateFilter(self, form_xmlui, widget_type, args, kwargs):
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
94 if widget_type != u'string' or not args[0]:
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 return widget_type, args, kwargs
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 # we convert XMPP date to timestamp
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
97 try:
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
98 args[0] = unicode(utils.date_parse(args[0]))
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
99 except Exception as e:
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
100 log.warning(_(u"Can't parse date field: {msg}").format(msg=e))
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 return widget_type, args, kwargs
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 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
104 client = self.host.getClient(profile_key)
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 service = jid.JID(service) if service else None
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 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
107 extra = self._p.parseExtra(extra_dict)
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 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
109 d.addCallback(self._p.serItemsData)
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 return d
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 @defer.inlineCallbacks
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
113 def get(self, client, service=None, node=None, max_items=None, item_ids=None, sub_id=None, rsm_request=None, extra=None, form_ns=NS_TICKETS):
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 """Retrieve tickets and convert them to XMLUI
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
116 @param service(None, jid.JID): Pubsub service to use
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 @param node(unicode, None): PubSub node to use
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 if None, default ticket node will be used
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 other parameters as the same as for [XEP_0060.getItems]
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
120 @return (tuple(list[unicode], dict[unicode, object])):
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
121 - XMLUI of the tickets
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
122 - metadata dict
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 """
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 if not node:
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 node = NS_TICKETS
2430
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
126 filters = {u'reporter': lambda *args: self._reporterFilter(client, *args),
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
127 u'labels': self._labelsFilter,
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 u'created': self._dateFilter,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 u'updated': self._dateFilter,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 }
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 tickets, metadata = yield self._s.getDataFormItems(
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 client,
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
133 form_ns,
2404
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 service,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 node,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 max_items = max_items,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 item_ids = item_ids,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 sub_id = sub_id,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 rsm_request = rsm_request,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 extra = extra,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 filters = filters,
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 )
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143
f05c884cd3ef plugin tickets: high level tickets handling, first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 defer.returnValue((tickets, metadata))
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
145
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
146 def _set(self, service, node, values, schema=None, item_id=None, extra=None, profile_key=C.PROF_KEY_NONE):
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
147 client = self.host.getClient(profile_key)
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
148 service = None if not service else jid.JID(service)
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
149 if schema:
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
150 schema = generic.parseXml(schema.encode('utf-8'))
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
151 else:
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
152 schema = None
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
153 if extra and u'update' in extra:
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
154 extra[u'update'] = C.bool(extra[u'update'])
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
155 d = self.set(client, service, node or None, values, schema, item_id or None, extra, deserialise=True)
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
156 d.addCallback(lambda ret: ret or u'')
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
157 return d
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
158
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
159 @defer.inlineCallbacks
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
160 def set(self, client, service, node, values, schema=None, item_id=None, extra=None, deserialise=False, form_ns=NS_TICKETS):
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
161 """Publish a tickets
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
162
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
163 @param node(unicode, None): Pubsub node to use
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
164 None to use default tickets node
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
165 @param values(dict[key(unicode), [iterable[object], object]]): values of the ticket
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
166 if not iterable, will be put in a list
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
167 'created' and 'updated' will be forced to current time:
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
168 - 'created' is set if item_id is None, i.e. if it's a new ticket
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
169 - 'updated' is set everytime
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
170 @param extra(dict, None): same as for [XEP-0060.sendItem] with additional keys:
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
171 - update(bool): if True, get previous item data to merge with current one
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
172 if True, item_id must be None
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
173 other arguments are same as for [self._s.sendDataFormItem]
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
174 @return (unicode): id of the created item
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
175 """
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
176 if not node:
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
177 node = NS_TICKETS
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
178 now = utils.xmpp_date()
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
179 if not item_id:
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
180 values['created'] = now
2434
7fa9456032e7 plugin tickets: specify parent service when using getCommentsService + fill reporter_jid is missing (will be discarded later if not in the schema)
Goffi <goffi@goffi.org>
parents: 2430
diff changeset
181 comments_service = yield self._m.getCommentsService(client, service)
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
182
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
183 # we need to use uuid for comments node, because we don't know item id in advance
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
184 # (we don't want to set it ourselves to let the server choose, so we can have
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
185 # a nicer id if serial ids is activated)
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
186 comments_node = self._m.getCommentsNode(node + u'_' + unicode(shortuuid.uuid()))
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
187 options = {self._p.OPT_ACCESS_MODEL: self._p.ACCESS_OPEN,
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
188 self._p.OPT_PERSIST_ITEMS: 1,
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
189 self._p.OPT_MAX_ITEMS: -1,
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
190 self._p.OPT_DELIVER_PAYLOADS: 1,
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
191 self._p.OPT_SEND_ITEM_SUBSCRIBE: 1,
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
192 self._p.OPT_PUBLISH_MODEL: self._p.ACCESS_OPEN,
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
193 }
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
194 yield self._p.createNode(client, comments_service, comments_node, options)
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
195 values['comments_uri'] = uri.buildXMPPUri(u'pubsub', subtype='microblog', path=comments_service.full(), node=comments_node)
2447
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
196 elif extra.get(u'update', False):
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
197 if item_id is None:
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
198 raise exceptions.DataError(_(u'if extra["update"] is set, item_id must be set too'))
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
199 try:
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
200 # we get previous item
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
201 items_data = yield self._p.getItems(client, service, node, item_ids=[item_id])
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
202 item_elt = items_data[0][0]
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
203 except Exception as e:
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
204 log.warning(_(u"Can't get previous item, update ignored: {reason}").format(
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
205 reason = e))
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
206 else:
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
207 # and parse it
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
208 form = data_form.findForm(item_elt, form_ns)
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
209 if form is None:
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
210 log.warning(_(u"Can't parse previous item, update ignored: data form not found").format(
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
211 reason = e))
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
212 else:
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
213 for name, field in form.fields.iteritems():
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
214 if name not in values:
9e692f09f367 plugin tickets: handle "update" flag + various improvments:
Goffi <goffi@goffi.org>
parents: 2443
diff changeset
215 values[name] = u'\n'.join(unicode(v) for v in field.values)
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
216
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
217 values['updated'] = now
2430
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
218 if not values.get('reporter'):
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
219 identity = yield self._i.getIdentity(client, client.jid)
3faf18111d61 plugin tickets: fill reporter field when it is not already filled
Goffi <goffi@goffi.org>
parents: 2420
diff changeset
220 values['reporter'] = identity['nick']
2434
7fa9456032e7 plugin tickets: specify parent service when using getCommentsService + fill reporter_jid is missing (will be discarded later if not in the schema)
Goffi <goffi@goffi.org>
parents: 2430
diff changeset
221 if not values.get('reporter_jid'):
7fa9456032e7 plugin tickets: specify parent service when using getCommentsService + fill reporter_jid is missing (will be discarded later if not in the schema)
Goffi <goffi@goffi.org>
parents: 2430
diff changeset
222 values['reporter_jid'] = client.jid.full()
2420
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
223 item_id = yield self._s.sendDataFormItem(client, service, node, values, schema, item_id, extra, deserialise)
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
224 defer.returnValue(item_id)
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
225
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
226 def _getSchema(self, service, node, profile_key=C.PROF_KEY_NONE):
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
227 if not node:
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
228 node = NS_TICKETS
03da3ef5fb5b plugin tickets: added ticketsSet and ticketsSchemaGet methods:
Goffi <goffi@goffi.org>
parents: 2404
diff changeset
229 return self._s._getUISchema(service, node, profile_key)