view libervia/pages/tickets/new/page_meta.py @ 1245:079e8eb6e327

server (tasks): refactoring: - moved `TasksManager` to `server.tasks.manager` - tasks modules now must have a `Task` class which will be instanciated by TasksManager - `server.tasks.task` has a basis for `Task` class - `Task.prepare` can now be asynchronous - `importlib` is now used to import tasks, instead of exec - tasks are now parsed/run after pages are imported - `server.BackendReady` is now a coroutine - type hinting for Task attributes
author Goffi <goffi@goffi.org>
date Sat, 25 Apr 2020 16:08:55 +0200
parents f511f8fbbf8a
children 04e7dd6b6f4d
line wrap: on
line source

#!/usr/bin/env python3


from libervia.server.constants import Const as C
from twisted.internet import defer
from sat.tools.common import template_xmlui
from sat.core.log import getLogger

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

name = "tickets_new"
access = C.PAGES_ACCESS_PROFILE
template = "ticket/create.html"


@defer.inlineCallbacks
def prepare_render(self, request):
    data = self.getRData(request)
    template_data = request.template_data
    service, node = data.get("service", ""), data.get("node", "")
    profile = self.getProfile(request)
    schema = yield self.host.bridgeCall("ticketsSchemaGet", service.full(), node, profile)
    data["schema"] = schema
    # following fields are handled in backend
    ignore = (
        "author",
        "author_jid",
        "author_email",
        "created",
        "updated",
        "comments_uri",
        "status",
        "milestone",
        "priority",
    )
    xmlui_obj = template_xmlui.create(self.host, schema, ignore=ignore)
    try:
        # small trick to get a one line text input instead of the big textarea
        xmlui_obj.widgets["labels"].type = "string"
    except KeyError:
        pass

    # same as for tickets_edit, we have to convert for now
    wid = xmlui_obj.widgets['body']
    if wid.type == "xhtmlbox":
        wid.type = "textbox"
        wid.value =  yield self.host.bridgeCall(
            "syntaxConvert", wid.value, C.SYNTAX_XHTML, "markdown",
            False, profile)
    template_data["new_ticket_xmlui"] = xmlui_obj


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

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


    yield self.host.bridgeCall(
        "ticketSet", service.full(), node, posted_data, "", "", "", profile
    )
    # we don't want to redirect to creation page on success, but to tickets list
    data["post_redirect_page"] = (
        self.getPageByName("tickets"),
        service.full(),
        node or "@",
    )