Esempio n. 1
0
def history(dictID):
    if not ops.dictExists(dictID):
        return redirect("/")
    user, configs = ops.verifyLoginAndDictAccess(request.cookies.email,
                                                 request.cookies.sessionkey,
                                                 ops.getDB(dictID))
    history = ops.readDictHistory(ops.getDB(dictID), dictID, configs,
                                  request.forms.id)
    res_history = []
    for item in history:
        xml = item["content"]
        html = ""
        if xml:
            if configs["xemplate"].get(
                    "_xsl") and configs["xemplate"]["_xsl"] != "":
                import lxml.etree as ET
                dom = ET.XML(xml.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 = xml
            else:
                entrydata = re.sub(r"'", "\\'", xml)
                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>"
        item["contentHtml"] = html
        res_history.append(item)
    return {"history": res_history}
Esempio n. 2
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}
Esempio n. 3
0
def publicentry(dictID, entryID):
    if not ops.dictExists(dictID):
        return redirect("/")
    dictDB = ops.getDB(dictID)
    user, configs = ops.verifyLoginAndDictAccess(request.cookies.email, request.cookies.sessionkey, dictDB)
    if not configs["publico"]["public"]:
        return redirect("/"+dictID)
    adjustedEntryID, xml, _title = ops.readEntry(dictDB, configs, entryID)
    if adjustedEntryID == 0:
        return redirect("/"+dictID)
    nabes = ops.readNabesByEntryID(dictDB, dictID, entryID, configs)
    if "_xsl" in configs["xemplate"] and configs["xemplate"]["_xsl"] != "":
        from lxml import etree
        xslt_root = etree.XML(configs["xemplate"]["_xsl"].encode("utf-8"))
        transform = etree.XSLT(xslt_root)
        doc_root = etree.XML(xml.encode("utf-8"))
        html = transform(doc_root)
    elif "_css" in configs["xemplate"] and configs["xemplate"]["_css"] != "":
        html = xml
    else:
        entrydata = re.sub(r"'", "\\'", xml)
        entrydata = re.sub(r"\n", " ", entrydata)
        html = "<script type='text/javascript'>$('#viewer').html(Xemplatron.xml2html('"+entrydata+"', "+json.dumps(configs["xemplate"])+", "+json.dumps(configs["xema"])+"));</script>"
        #rewrite xemplatron to python, too?
    css = ""
    if "_css" in configs["xemplate"]:
        css = configs["xemplate"]["_css"]
    return template("dict-entry.tpl", **{"siteconfig": siteconfig, "user": user, "dictID": dictID, "dictTitle": configs["ident"]["title"], "dictBlurb": configs["ident"]["blurb"], "publico": configs["publico"], "entryID": adjustedEntryID, "nabes": nabes, "html": html, "title": _title, "css": css})
Esempio n. 4
0
def dictsearch(dictID):
    if not ops.dictExists(dictID):
        return redirect("/")
    dictDB = ops.getDB(dictID)
    user, configs = ops.verifyLoginAndDictAccess(request.cookies.email,
                                                 request.cookies.sessionkey,
                                                 dictDB)
    if not configs["publico"]["public"]:
        return redirect("/" + dictID)
    entries = ops.listEntriesPublic(dictDB, dictID, configs, request.query.q)
    if len(entries) == 1 and entries[0]["exactMatch"]:
        redirect("/" + dictID + "/" + entries[0]["id"])
    else:
        nabes = ops.readNabesByText(dictDB, dictID, configs, request.query.q)
        return template(
            "dict-search.tpl", **{
                "siteconfig": siteconfig,
                "user": user,
                "dictID": dictID,
                "dictTitle": configs["ident"]["title"],
                "dictBlurb": configs["ident"]["blurb"],
                "publico": configs["publico"],
                "q": request.query.q,
                "entries": entries,
                "nabes": nabes
            })
Esempio n. 5
0
def publicrandom(dictID):
    if not ops.dictExists(dictID):
        return redirect("/")
    dictDB = ops.getDB(dictID)
    configs = ops.readDictConfigs(dictDB)
    if not configs["publico"]["public"]:
        return {"more": False, "entries": []}
    res = ops.readRandoms(dictDB)
    return res
Esempio n. 6
0
def publicentryxml(dictID, entryID):
    if not ops.dictExists(dictID):
        return redirect("/")
    dictDB = ops.getDB(dictID)
    user, configs = ops.verifyLoginAndDictAccess(request.cookies.email, request.cookies.sessionkey, dictDB)
    if not configs["publico"]["public"]:
        return redirect("/"+dictID)
    if not "licence" in configs["publico"] or not siteconfig["licences"][configs["publico"]["licence"]]["canDownloadXml"]:
        return redirect("/"+dictID)
    res = ops.exportEntryXml(dictDB, dictID, entryID, configs, siteconfig["baseUrl"])
    if res["entryID"] == 0:
        return redirect("/"+dictID)
    response.content_type = "text/xml; charset=utf-8"
    return res["xml"]
Esempio n. 7
0
 def wrapper_verifyLoginAndDictAccess(*args, **kwargs):
     try:
         conn = ops.getDB(kwargs["dictID"])
     except IOError:
         abort(404, "No such dictionary")
     res, configs = ops.verifyLoginAndDictAccess(request.cookies.email, request.cookies.sessionkey, conn)
     for r in checkRights:
         if not res.get(r, False):
             return res
     del kwargs["dictID"]
     kwargs["user"] = res
     kwargs["dictDB"] = conn
     kwargs["configs"] = configs
     return func(*args, **kwargs)
Esempio n. 8
0
def publicdict(dictID):
    if not ops.dictExists(dictID):
        return redirect("/")
    user, configs = ops.verifyLoginAndDictAccess(request.cookies.email,
                                                 request.cookies.sessionkey,
                                                 ops.getDB(dictID))
    blurb = ops.markdown_text(configs["ident"]["blurb"])
    return template(
        "dict.tpl", **{
            "siteconfig": siteconfig,
            "user": user,
            "dictID": dictID,
            "dictTitle": configs["ident"]["title"],
            "dictBlurb": blurb,
            "publico": configs["publico"]
        })
Esempio n. 9
0
def linkNaisc(dictID, user, dictDB, configs):
    otherdictID = request.query.otherdictID
    if dictID == otherdictID:
        abort(
            400,
            "Linking dictionary to the same dictionary does not make any sense"
        )
    try:
        otherconn = ops.getDB(otherdictID)
    except IOError:
        abort(404, "No such dictionary")
    _res, otherconfigs = ops.verifyLoginAndDictAccess(
        request.cookies.email, request.cookies.sessionkey, otherconn)
    res = ops.linkNAISC(dictDB, dictID, configs, otherconn, otherdictID,
                        otherconfigs)
    return res
Esempio n. 10
0
def ontolex(dictID, doctype):
    data = json.loads(request.body.getvalue().decode('utf-8'))
    if not data.get("email") or not data.get("apikey"):
        return {"success": False, "message": "missing email or api key"}
    user = ops.verifyUserApiKey(data["email"], data["apikey"])
    if not user["valid"]:
        return {"success": False}
    else:
        if data.get("search"):
            search = data["search"]
        else:
            search = ""
        dictDB = ops.getDB(dictID)
        configs = ops.readDictConfigs(dictDB)
        dictAccess = configs["users"].get(
            user["email"]) or user["email"] in siteconfig["admins"]
        if not dictAccess:
            return {"success": False}
        else:
            response.headers['Content-Type'] = "text/plain; charset=utf-8"
            return ops.listOntolexEntries(dictDB, dictID, configs, doctype,
                                          search)