forked from kristjan/cjvt-valency
modifying frames api
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from flask import Flask, render_template, request, url_for, redirect
|
||||
from valency import Frame, Slot
|
||||
from valency.reduce_functions import reduce_functions
|
||||
|
||||
"""
|
||||
from valency import k_utils
|
||||
@@ -26,12 +28,19 @@ from pathlib import Path
|
||||
from pymongo import MongoClient
|
||||
import argparse
|
||||
|
||||
# some db collections
|
||||
USERS_COLL = "users"
|
||||
TOKENS_COLL = "usertokens"
|
||||
SENSES_COLL = "senses"
|
||||
SENSEMAP_COLL = "sensemap"
|
||||
|
||||
# pre-generated data (gui leftside word index)
|
||||
CORPORA = ["ssj", "kres"]
|
||||
app_index = {c: {} for c in CORPORA}
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
app = Flask(__name__)
|
||||
|
||||
app_index = {c: {} for c in CORPORA}
|
||||
|
||||
# when running vuejs via webpack
|
||||
# CORS(app)
|
||||
@@ -41,23 +50,7 @@ app_index = {c: {} for c in CORPORA}
|
||||
CORS(app)
|
||||
|
||||
|
||||
# for testing functions
|
||||
@app.route("/test_dev")
|
||||
def test_dev():
|
||||
ret = vallex.test_dev()
|
||||
return(str(ret) or "edit val_struct.py: test_dev()")
|
||||
|
||||
|
||||
@app.route("/")
|
||||
def index():
|
||||
return(render_template("index.html"))
|
||||
|
||||
|
||||
@app.route("/home", defaults={"pathname": ""})
|
||||
@app.route("/home/<path:pathname>")
|
||||
def home(pathname):
|
||||
return redirect(url_for("index"), code=302)
|
||||
|
||||
# INDEX SELECTION -------------------.
|
||||
|
||||
@app.route("/api/words/<corpus>")
|
||||
def api_words(corpus):
|
||||
@@ -69,10 +62,13 @@ def api_words(corpus):
|
||||
def api_functors(corpus):
|
||||
return json.dumps(app_index[corpus]["functors"])
|
||||
|
||||
# INDEX SELECTION -------------------^
|
||||
|
||||
|
||||
# AUTH ------------------------------.
|
||||
|
||||
@app.route("/api/register", methods=["POST"])
|
||||
def api_register():
|
||||
USERS_COLL = "v2_users"
|
||||
b = request.get_data()
|
||||
data = json.loads(b.decode())
|
||||
username = data["username"]
|
||||
@@ -84,7 +80,7 @@ def api_register():
|
||||
email == ""
|
||||
):
|
||||
return "ERR"
|
||||
existing = list(vallex.db[USERS_COLL].find({
|
||||
existing = list(valdb[USERS_COLL].find({
|
||||
"$or": [{"username": username}, {"email": email}]
|
||||
}))
|
||||
if len(existing) > 0:
|
||||
@@ -96,21 +92,19 @@ def api_register():
|
||||
"email": hashlib.sha256(
|
||||
email.encode("utf-8")).hexdigest()
|
||||
}
|
||||
vallex.db[USERS_COLL].insert(entry)
|
||||
valdb[USERS_COLL].insert(entry)
|
||||
return "OK"
|
||||
|
||||
|
||||
@app.route("/api/login", methods=["POST"])
|
||||
def api_login():
|
||||
USERS_COLL = "v2_users"
|
||||
TOKENS_COLL = "v2_user_tokens"
|
||||
b = request.get_data()
|
||||
data = json.loads(b.decode())
|
||||
username = data["username"]
|
||||
password = data["password"]
|
||||
hpass = hashlib.sha256(password.encode("utf-8")).hexdigest()
|
||||
|
||||
db_user = list(vallex.db[USERS_COLL].find({
|
||||
db_user = list(valdb[USERS_COLL].find({
|
||||
"username": username,
|
||||
"hpass": hpass
|
||||
}))
|
||||
@@ -124,7 +118,7 @@ def api_login():
|
||||
"date": datetime.datetime.utcnow(),
|
||||
"token": token
|
||||
}
|
||||
vallex.db[TOKENS_COLL].update(
|
||||
valdb[TOKENS_COLL].update(
|
||||
{"username": token_entry["username"]},
|
||||
token_entry,
|
||||
upsert=True
|
||||
@@ -167,7 +161,7 @@ def api_new_pass():
|
||||
username = data["username"]
|
||||
email = data["email"]
|
||||
hemail = hashlib.sha256(email.encode("utf-8")).hexdigest()
|
||||
db_res = list(vallex.db.v2_users.find({
|
||||
db_res = list(valdb[USERS_COLL].find({
|
||||
"username": username,
|
||||
"email": hemail
|
||||
}))
|
||||
@@ -179,7 +173,7 @@ def api_new_pass():
|
||||
string.ascii_letters + string.digits) for i in range(10)])
|
||||
# update locally
|
||||
hpass = hashlib.sha256(new_pass.encode("utf-8")).hexdigest()
|
||||
vallex.db.v2_users.update(
|
||||
valdb[USERS_COLL].update(
|
||||
{
|
||||
"username": username,
|
||||
"email": hemail
|
||||
@@ -193,6 +187,39 @@ def api_new_pass():
|
||||
return json.dumps({"confirmation": True})
|
||||
|
||||
|
||||
def token_to_username(token):
|
||||
key = {
|
||||
"token": token
|
||||
}
|
||||
res = list(valdb[TOKENS_COLL].find(key))
|
||||
if len(res) != 1:
|
||||
return None
|
||||
username = res[0]["username"]
|
||||
# update deletion interval
|
||||
valdb[TOKENS_COLL].update(
|
||||
key, {"$set": {"date": datetime.datetime.utcnow()}})
|
||||
return username
|
||||
|
||||
|
||||
@app.route("/api/token", methods=["POST"])
|
||||
def api_token():
|
||||
# check if token is valid
|
||||
b = request.get_data()
|
||||
data = json.loads(b.decode())
|
||||
token = data.get("token")
|
||||
# user = data.get("user")
|
||||
user = token_to_username(token)
|
||||
confirm = (user is not None)
|
||||
return json.dumps({
|
||||
"confirmation": confirm,
|
||||
"username": user
|
||||
})
|
||||
|
||||
# AUTH ------------------------------^
|
||||
|
||||
|
||||
# FRAMES ----------------------------.
|
||||
|
||||
def prepare_frames(ret_frames):
|
||||
# append sentences
|
||||
for frame in ret_frames:
|
||||
@@ -218,19 +245,21 @@ def prepare_frames(ret_frames):
|
||||
return json.dumps(json_ret)
|
||||
|
||||
|
||||
@app.route("/api/frames")
|
||||
# input: hw, reduct_function
|
||||
@app.route("/api/hw-frames")
|
||||
def api_get_frames():
|
||||
hw = request.args.get("hw")
|
||||
if hw is None:
|
||||
return json.dumps({"error": "Headword not found."})
|
||||
return json.dumps({"error": "Required argument: hw (headword)."})
|
||||
|
||||
rf_name = request.args.get("rf", "reduce_0") # 2nd is default
|
||||
RF = reduce_functions[rf_name]["f"]
|
||||
entry = vallex.entries[hw]
|
||||
entry = vallex.entries[hw] # TODO hw -> [Frame,]
|
||||
ret_frames = RF(entry.raw_frames, vallex)
|
||||
return prepare_frames(ret_frames)
|
||||
|
||||
|
||||
# input: functor, reduce_function
|
||||
@app.route("/api/functor-frames")
|
||||
def api_get_functor_frames():
|
||||
functor = request.args.get("functor")
|
||||
@@ -238,49 +267,23 @@ def api_get_functor_frames():
|
||||
return json.dumps({"error": "Missing argument: functor."})
|
||||
rf_name = request.args.get("rf", "reduce_0") # 2nd is default
|
||||
RF = reduce_functions[rf_name]["f"]
|
||||
raw_frames = vallex.functors_index[functor]
|
||||
raw_frames = vallex.functors_index[functor] # TODO
|
||||
ret_frames = RF(raw_frames, vallex)
|
||||
return prepare_frames(ret_frames)
|
||||
|
||||
|
||||
def token_to_username(token):
|
||||
COLLNAME = "v2_user_tokens"
|
||||
key = {
|
||||
"token": token
|
||||
}
|
||||
res = list(vallex.db[COLLNAME].find(key))
|
||||
if len(res) != 1:
|
||||
return None
|
||||
username = res[0]["username"]
|
||||
# update deletion interval
|
||||
vallex.db[COLLNAME].update(
|
||||
key, {"$set": {"date": datetime.datetime.utcnow()}})
|
||||
return username
|
||||
# FRAMES ----------------------------^
|
||||
|
||||
|
||||
@app.route("/api/token", methods=["POST"])
|
||||
def api_token():
|
||||
# check if token is valid
|
||||
b = request.get_data()
|
||||
data = json.loads(b.decode())
|
||||
token = data.get("token")
|
||||
# user = data.get("user")
|
||||
user = token_to_username(token)
|
||||
confirm = (user is not None)
|
||||
return json.dumps({
|
||||
"confirmation": confirm,
|
||||
"username": user
|
||||
})
|
||||
|
||||
# SENSES ----------------------------.
|
||||
|
||||
@app.route("/api/senses/get")
|
||||
def api_senses_get():
|
||||
# returns senses and mapping for hw
|
||||
hw = request.args.get("hw")
|
||||
senses = list(vallex.db["v2_senses"].find({
|
||||
senses = list(valdb[SENSES_COLL].find({
|
||||
"hw": hw
|
||||
}))
|
||||
sense_map_query = list(vallex.db["v2_sense_map"].find({
|
||||
sense_map_query = list(valdb[SENSEMAP_COLL].find({
|
||||
"hw": hw
|
||||
}))
|
||||
# aggregation by max date possible on DB side
|
||||
@@ -358,7 +361,7 @@ def api_senses_update():
|
||||
id_map[frontend_sense_id] = new_sense_id
|
||||
|
||||
# insert into db
|
||||
vallex.db["v2_senses"].insert(ns)
|
||||
valdb[SENSES_COLL].insert(ns)
|
||||
|
||||
# replace tmp_id with mongo's _id
|
||||
for ssj_id, el in sense_map.items():
|
||||
@@ -373,9 +376,14 @@ def api_senses_update():
|
||||
"date": datetime.datetime.utcnow()
|
||||
}
|
||||
# vallex.db["v2_sense_map"].update(key, data, upsert=True)
|
||||
vallex.db["v2_sense_map"].insert(data)
|
||||
valdb[SENSEMAP_COLL].insert(data)
|
||||
return "OK"
|
||||
|
||||
# SENSES ----------------------------^
|
||||
|
||||
|
||||
# APP PREFLIGHT ---------------------.
|
||||
|
||||
def prepare_db():
|
||||
def helper_tid_to_token(tid, tokens):
|
||||
for t in tokens:
|
||||
@@ -384,7 +392,7 @@ def prepare_db():
|
||||
return None
|
||||
|
||||
# update entries (add headwords and fuctors for indexing)
|
||||
for corpus in ["ssj", "kres"]:
|
||||
for corpus in CORPORA:
|
||||
for e in valdb[corpus].find({}):
|
||||
if e["srl_links"] is None:
|
||||
continue
|
||||
@@ -435,6 +443,8 @@ def prepare_db():
|
||||
functors = sorted(functors, key=lambda x: x[0])
|
||||
app_index[corpus]["functors"] = functors
|
||||
|
||||
# APP PREFLIGHT ---------------------^
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("Starting app.py main()")
|
||||
|
||||
Reference in New Issue
Block a user