Index: grammalecte-server.py ================================================================== --- grammalecte-server.py +++ grammalecte-server.py @@ -49,10 +49,18 @@
  • "options" (text) : une chaîne au format JSON avec le nom des options comme attributs et un booléen comme valeur. Exemple : {"gv": true, "html": true}
  • Remise à zéro de ses options

    [adresse_serveur]:{SERVER_PORT}/reset_options/fr (POST)

    + +

    Suggestions orthographiques

    +

    [adresse_serveur]:{SERVER_PORT}/suggest/fr/<token> (GET)

    +

    [adresse_serveur]:{SERVER_PORT}/suggest/fr (POST)

    +

    Paramètres :

    +

    TEST

    Analyse

    @@ -72,10 +80,16 @@

    Remise à zéro de ses options

    + +

    Suggestion orthographique

    +
    +

    +

    +
    """ @@ -112,11 +126,15 @@ @app.route("/get_options/fr") def listOptions (): "returns grammar options in a text JSON format" sUserId = request.cookies.user_id dOptions = dUser[sUserId]["gc_options"] if sUserId and sUserId in dUser else dGCOptions - return '{ "values": ' + json.dumps(dOptions) + ', "labels": ' + json.dumps(gce.getOptionsLabels("fr"), ensure_ascii=False) + ' }' + return '{ "values": ' + json.dumps(dOptions, ensure_ascii=False) + ', "labels": ' + json.dumps(gce.getOptionsLabels("fr"), ensure_ascii=False) + ' }' + +@app.route("/suggest/fr/") +def suggestGet (token): + return suggest(token) # POST @app.route("/gc_text/fr", method="POST") def gcText (): @@ -159,11 +177,11 @@ dOptions = dUser[sUserId]["gc_options"] if sUserId in dUser else dict(dGCOptions) try: dOptions.update(json.loads(request.forms.options)) dUser[sUserId] = { "time": int(time.time()), "gc_options": dOptions } response.set_cookie("user_id", sUserId, path="/", max_age=86400) # 24h - return json.dumps(dUser[sUserId]["gc_options"]) + return json.dumps(dUser[sUserId]["gc_options"], ensure_ascii=False) except (KeyError, json.JSONDecodeError): traceback.print_exc() return '{"error": "options not registered"}' return '{"error": "no options received"}' @@ -180,10 +198,31 @@ return oTextFormatter.formatText(request.forms.text) #@app.route('/static/') #def server_static (filepath): # return static_file(filepath, root='./views/static') + +@app.route("/suggest/fr", method="POST") +def suggestPost (): + return suggest(request.forms.token) + + +## Common functions + +def suggest (sToken): + if sToken: + lSugg = [] + try: + for l in oSpellChecker.suggest(sToken): + lSugg.extend(l) + except: + return '{"error": "suggestion module failed"}' + try: + return '{"suggestions": ' + json.dumps(lSugg, ensure_ascii=False) + '}' + except json.JSONDecodeError: + return '{"error": "json encoding error"}' + return '{"error": "no token given"}' def purgeUsers (): "delete user options older than n hours" try: