plugins schema, tickets, merge_requests: use serialised data for extra dict + some cosmetic changes
This commit is contained in:
parent
0f97a75f6f
commit
481ce4c379
1 changed files with 94 additions and 72 deletions
|
|
@ -17,37 +17,38 @@
|
|||
# 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 collections import Iterable
|
||||
import copy
|
||||
import itertools
|
||||
from zope.interface import implements
|
||||
from twisted.words.protocols.jabber import jid
|
||||
from twisted.words.protocols.jabber.xmlstream import XMPPHandler
|
||||
from twisted.internet import defer
|
||||
from wokkel import disco, iwokkel
|
||||
from wokkel import data_form
|
||||
from wokkel import generic
|
||||
from sat.core.i18n import _
|
||||
from sat.core import exceptions
|
||||
from sat.core.constants import Const as C
|
||||
from sat.tools import xml_tools
|
||||
from sat.tools import utils
|
||||
from sat.tools.common import date_utils
|
||||
from twisted.words.protocols.jabber import jid
|
||||
from twisted.words.protocols.jabber.xmlstream import XMPPHandler
|
||||
from twisted.internet import defer
|
||||
from sat.tools.common import data_format
|
||||
from sat.core.log import getLogger
|
||||
|
||||
log = getLogger(__name__)
|
||||
from wokkel import disco, iwokkel
|
||||
from wokkel import data_form
|
||||
from wokkel import generic
|
||||
from zope.interface import implements
|
||||
from collections import Iterable
|
||||
import copy
|
||||
import itertools
|
||||
|
||||
NS_SCHEMA = "https://salut-a-toi/protocol/schema:0"
|
||||
NS_SCHEMA = u"https://salut-a-toi/protocol/schema:0"
|
||||
|
||||
PLUGIN_INFO = {
|
||||
C.PI_NAME: "PubSub Schema",
|
||||
C.PI_IMPORT_NAME: "PUBSUB_SCHEMA",
|
||||
C.PI_TYPE: "EXP",
|
||||
C.PI_NAME: u"PubSub Schema",
|
||||
C.PI_IMPORT_NAME: u"PUBSUB_SCHEMA",
|
||||
C.PI_TYPE: u"EXP",
|
||||
C.PI_PROTOCOLS: [],
|
||||
C.PI_DEPENDENCIES: ["XEP-0060", "IDENTITY"],
|
||||
C.PI_MAIN: "PubsubSchema",
|
||||
C.PI_HANDLER: "yes",
|
||||
C.PI_DESCRIPTION: _("""Handle Pubsub data schemas"""),
|
||||
C.PI_DEPENDENCIES: [u"XEP-0060", u"IDENTITY"],
|
||||
C.PI_MAIN: u"PubsubSchema",
|
||||
C.PI_HANDLER: u"yes",
|
||||
C.PI_DESCRIPTION: _(u"""Handle Pubsub data schemas"""),
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -510,10 +511,49 @@ class PubsubSchema(object):
|
|||
schema = generic.parseXml(schema.encode("utf-8"))
|
||||
else:
|
||||
schema = None
|
||||
if extra and u"update" in extra:
|
||||
extra[u"update"] = C.bool(extra[u"update"])
|
||||
extra = data_format.deserialise(extra)
|
||||
return client, service, node or None, schema, item_id or None, extra
|
||||
|
||||
@defer.inlineCallbacks
|
||||
def copyMissingValues(self, client, service, node, item_id, form_ns, values):
|
||||
"""Retrieve values existing in original item and missing in update
|
||||
|
||||
Existing item will be retrieve, and values not already specified in values will
|
||||
be filled
|
||||
@param service: same as for [XEP_0060.getItems]
|
||||
@param node: same as for [XEP_0060.getItems]
|
||||
@param item_id(unicode): id of the item to retrieve
|
||||
@param form_ns (unicode, None): namespace of the form
|
||||
@param values(dict): values to fill
|
||||
This dict will be modified *in place* to fill value present in existing
|
||||
item and missing in the dict.
|
||||
"""
|
||||
try:
|
||||
# we get previous item
|
||||
items_data = yield self._p.getItems(
|
||||
client, service, node, item_ids=[item_id]
|
||||
)
|
||||
item_elt = items_data[0][0]
|
||||
except Exception as e:
|
||||
log.warning(
|
||||
_(u"Can't get previous item, update ignored: {reason}").format(
|
||||
reason=e
|
||||
)
|
||||
)
|
||||
else:
|
||||
# and parse it
|
||||
form = data_form.findForm(item_elt, form_ns)
|
||||
if form is None:
|
||||
log.warning(
|
||||
_(
|
||||
u"Can't parse previous item, update ignored: data form not found"
|
||||
).format(reason=e)
|
||||
)
|
||||
else:
|
||||
for name, field in form.fields.iteritems():
|
||||
if name not in values:
|
||||
values[name] = u"\n".join(unicode(v) for v in field.values)
|
||||
|
||||
def _set(self, service, node, values, schema=None, item_id=None, extra=None,
|
||||
default_node=None, form_ns=None, fill_author=True,
|
||||
profile_key=C.PROF_KEY_NONE):
|
||||
|
|
@ -561,6 +601,8 @@ class PubsubSchema(object):
|
|||
other arguments are same as for [self._s.sendDataFormItem]
|
||||
@return (unicode): id of the created item
|
||||
"""
|
||||
if extra is None:
|
||||
extra = {}
|
||||
if not node:
|
||||
if default_node is None:
|
||||
raise ValueError(_(u"default_node must be set if node is not set"))
|
||||
|
|
@ -573,31 +615,7 @@ class PubsubSchema(object):
|
|||
raise exceptions.DataError(
|
||||
_(u'if extra["update"] is set, item_id must be set too')
|
||||
)
|
||||
try:
|
||||
# we get previous item
|
||||
items_data = yield self._p.getItems(
|
||||
client, service, node, item_ids=[item_id]
|
||||
)
|
||||
item_elt = items_data[0][0]
|
||||
except Exception as e:
|
||||
log.warning(
|
||||
_(u"Can't get previous item, update ignored: {reason}").format(
|
||||
reason=e
|
||||
)
|
||||
)
|
||||
else:
|
||||
# and parse it
|
||||
form = data_form.findForm(item_elt, form_ns)
|
||||
if form is None:
|
||||
log.warning(
|
||||
_(
|
||||
u"Can't parse previous item, update ignored: data form not found"
|
||||
).format(reason=e)
|
||||
)
|
||||
else:
|
||||
for name, field in form.fields.iteritems():
|
||||
if name not in values:
|
||||
values[name] = u"\n".join(unicode(v) for v in field.values)
|
||||
yield self.copyMissingValues(client, service, node, item_id, form_ns, values)
|
||||
|
||||
values["updated"] = now
|
||||
if fill_author:
|
||||
|
|
|
|||
Loading…
Reference in a new issue