view libervia/pages/tickets/edit/ @ 1194:bed008b65d7c

pages (merge-requests/edit, tickets/edit, tickets/new): use serialised extra following backend change + fixed use of "update" when editing ticket: "update" was not set in extra, as a result some metadata could be lost on update (author, comments_uri). fix 301
author Goffi <>
date Fri, 14 Jun 2019 12:50:59 +0200
parents 7b05ebc46141
children 5e67c2564779
line wrap: on
line source

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

from libervia.server.constants import Const as C
from sat.core.i18n import _
from twisted.internet import defer
from import template_xmlui
from import data_format
from sat.core.log import getLogger

log = getLogger(__name__)
"""ticket handling pages"""

name = u"tickets_edit"
template = u"ticket/edit.html"

def parse_url(self, request):
        item_id = self.nextPath(request)
    except IndexError:
        log.warning(_(u"no ticket id specified"))
        self.pageError(request, C.HTTP_BAD_REQUEST)

    data = self.getRData(request)
    data[u"ticket_id"] = item_id

def prepare_render(self, request):
    data = self.getRData(request)
    template_data = request.template_data
    service, node, ticket_id = (
        data.get(u"service", u""),
        data.get(u"node", u""),
    profile = self.getProfile(request)

    # we don't ignore "author" below to keep it when a ticket is edited
    # by node owner/admin and "consistent publisher" is activated
    ignore = (
    tickets = yield
        service.full() if service else u"",
    ticket = [template_xmlui.create(, x, ignore=ignore) for x in tickets[0]][0]

        # small trick to get a one line text input instead of the big textarea
        ticket.widgets[u"labels"].type = u"string"
        ticket.widgets[u"labels"].value = ticket.widgets[u"labels"].value.replace(
            u"\n", ", "
    except KeyError:

    # for now we don't have XHTML editor, so we'll go with a TextBox and a convertion
    # to a text friendly syntax using markdown
    wid = ticket.widgets[u'body']
    if wid.type == u"xhtmlbox":
        wid.type = u"textbox"
        wid.value =  yield
            u"syntaxConvert", wid.value, C.SYNTAX_XHTML, u"markdown",
            False, profile)

    template_data[u"new_ticket_xmlui"] = ticket

def on_data_post(self, request):
    data = self.getRData(request)
    service = data["service"]
    node = data["node"]
    ticket_id = data["ticket_id"]
    posted_data = self.getAllPostedData(request)
    if not posted_data["title"] or not posted_data["body"]:
        self.pageError(request, C.HTTP_BAD_REQUEST)
        posted_data["labels"] = [l.strip() for l in posted_data["labels"][0].split(",")]
    except (KeyError, IndexError):
    profile = self.getProfile(request)

    # we convert back body to XHTML
    body = yield
        u"syntaxConvert", posted_data[u'body'][0], u"markdown", C.SYNTAX_XHTML,
        False, profile)
    posted_data[u'body'] = [u'<div xmlns="{ns}">{body}</div>'.format(ns=C.NS_XHTML,

    extra = {u'update': True}
        "ticketSet", service.full(), node, posted_data, u"", ticket_id,
        data_format.serialise(extra), profile
    # we don't want to redirect to edit page on success, but to tickets list
    data["post_redirect_page"] = (
        node or u"@",