Пример #1
0
def pushapi():
    data = json.loads(request.body.getvalue().decode('utf-8'))
    user = ops.verifyUserApiKey(data["email"], data["apikey"])
    if not user["valid"]:
        return {"success": False}
    else:
        if data["command"] == "makeDict":
            dictID = ops.suggestDictId()
            dictTitle = re.sub(r"^\s+", "", data["dictTitle"])
            if dictTitle == "":
                dictTitle = dictID
            dictBlurb = data["dictBlurb"]
            poses = data["poses"]
            labels = data["labels"]
            res = ops.makeDict(dictID, "push", dictTitle, dictBlurb,
                               user["email"])
            if not res:
                return {"success": False}
            else:
                dictDB = ops.getDB(dictID)
                configs = ops.readDictConfigs(dictDB)
                if configs["xema"]["elements"].get("partOfSpeech"):
                    for pos in poses:
                        configs["xema"]["elements"]["partOfSpeech"][
                            "values"].append({
                                "value": pos,
                                "caption": ""
                            })
                if configs["xema"]["elements"].get("collocatePartOfSpeech"):
                    for pos in poses:
                        configs["xema"]["elements"]["collocatePartOfSpeech"][
                            "values"].append({
                                "value": pos,
                                "caption": ""
                            })
                if configs["xema"]["elements"].get("label"):
                    for label in labels:
                        configs["xema"]["elements"]["label"]["values"].append({
                            "value":
                            label,
                            "caption":
                            ""
                        })
                ops.updateDictConfig(dictDB, dictID, "xema", configs["xema"])
                return {"success": True, "dictID": dictID}
        elif data["command"] == "listDicts":
            dicts = ops.getDictsByUser(user["email"])
            return {"entries": dicts, "success": True}
        elif data["command"] == "createEntries":
            dictID = data["dictID"]
            entryXmls = data["entryXmls"]
            dictDB = ops.getDB(dictID)
            configs = ops.readDictConfigs(dictDB)
            for entry in entryXmls:
                ops.createEntry(dictDB, configs, None, entry, user["email"],
                                {"apikey": data["apikey"]})
            return {"success": True}
        else:
            return {"success": False}
Пример #2
0
def entrycreate(dictID, user, dictDB, configs):
    adjustedEntryID, adjustedXml, feedback = ops.createEntry(
        dictDB, configs, None, request.forms.content, user["email"], {})
    html = ""
    if configs["xemplate"].get("_xsl") and configs["xemplate"]["_xsl"] != "":
        import lxml.etree as ET
        dom = ET.XML(adjustedXml.encode("utf-8"))
        xslt = ET.XML(configs["xemplate"]["_xsl"].encode("utf-8"))
        html = str(ET.XSLT(xslt)(dom))
    elif configs["xemplate"].get("_css") and configs["xemplate"]["_css"] != "":
        html = adjustedXml
    else:
        entrydata = re.sub(r"'", "\\'", adjustedXml)
        entrydata = re.sub(r"[\n\r]", "", entrydata)
        html = "<script type='text/javascript'>$('#viewer').html(Xemplatron.xml2html('" + entrydata + "', " + json.dumps(
            configs["xemplate"]) + ", " + json.dumps(
                configs["xema"]) + "));</script>"
    result = {
        "success": True,
        "id": adjustedEntryID,
        "content": adjustedXml,
        "contentHtml": html
    }
    if feedback:
        result["feedback"] = feedback
    return result