view 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
line wrap: on
line source

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

# SAT plugin for Pubsub Schemas
# Copyright (C) 2009-2017 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from sat.core.i18n import _
from sat.core.constants import Const as C
from twisted.words.protocols.jabber import jid
from twisted.internet import defer
from sat.tools import utils
from sat.core.log import getLogger
log = getLogger(__name__)

NS_TICKETS = 'org.salut-a-toi.tickets:0'

PLUGIN_INFO = {
    C.PI_NAME: _("Tickets management"),
    C.PI_IMPORT_NAME: "TICKETS",
    C.PI_TYPE: "EXP",
    C.PI_PROTOCOLS: [],
    C.PI_DEPENDENCIES: ["XEP-0060", "PUBSUB_SCHEMA"],
    C.PI_MAIN: "Tickets",
    C.PI_HANDLER: "no",
    C.PI_DESCRIPTION: _("""Tickets management plugin""")
}


class Tickets(object):

    def __init__(self, host):
        log.info(_(u"Tickets plugin initialization"))
        self.host = host
        self._p = self.host.plugins["XEP-0060"]
        self._s = self.host.plugins["PUBSUB_SCHEMA"]
        host.bridge.addMethod("ticketsGet", ".plugin",
                              in_sign='ssiassa{ss}s', out_sign='(asa{ss})',
                              method=self._get,
                              async=True
                              )

    def _labelsFilter(self, widget_type, args, kwargs):
        if widget_type != u'textbox':
            return widget_type, args, kwargs
        widget_type = u'list'
        options = [o for o in args.pop(0).split(u'\n') if o]
        kwargs = {'options': options,
                  'name': kwargs.get('name'),
                  'styles': (u'noselect', u'extensible', u'reducible')}
        return widget_type, args, kwargs

    def _dateFilter(self, widget_type, args, kwargs):
        if widget_type != u'string':
            return widget_type, args, kwargs
        # we convert XMPP date to timestamp
        args[0] = unicode(utils.date_parse(args[0]))
        return widget_type, args, kwargs

    def _get(self, service='', node='', max_items=10, item_ids=None, sub_id=None, extra_dict=None, profile_key=C.PROF_KEY_NONE):
        client = self.host.getClient(profile_key)
        service = jid.JID(service) if service else None
        max_items = None if max_items == C.NO_LIMIT else max_items
        extra = self._p.parseExtra(extra_dict)
        d = self.get(client, service, node or None, max_items, item_ids, sub_id or None, extra.rsm_request, extra.extra)
        d.addCallback(self._p.serItemsData)
        return d

    @defer.inlineCallbacks
    def get(self, client, service=None, node=None, max_items=None, item_ids=None, sub_id=None, rsm_request=None, extra=None):
        """Retrieve tickets and convert them to XMLUI

        @param node(unicode, None): PubSub node to use
            if None, default ticket node will be used
        other parameters as the same as for [XEP_0060.getItems]
        @return (list[unicode]): XMLUI of the tickets
        """
        if not node:
            node = NS_TICKETS
        filters = {u'labels': self._labelsFilter,
                   u'created': self._dateFilter,
                   u'updated': self._dateFilter,
                   }
        tickets, metadata = yield self._s.getDataFormItems(
            client,
            NS_TICKETS,
            service,
            node,
            max_items = max_items,
            item_ids = item_ids,
            sub_id = sub_id,
            rsm_request = rsm_request,
            extra = extra,
            filters = filters,
            )

        defer.returnValue((tickets, metadata))