From 6a3819eb0a2381baf522ca0944cea82c1bda43a3 Mon Sep 17 00:00:00 2001 From: siulkilulki Date: Fri, 25 May 2018 12:39:06 +0200 Subject: [PATCH] Add passing users not annotated utterances (by them) Switch to secrets module for cookie tokens. Add console, exec mode to annotation_stats.py (todo rename script) Add some more info in index.html helper modal. --- annotation_stats.py | 5 +++++ webapp/app.py | 43 ++++++++++++++++++++++++++----------- webapp/templates/index.html | 2 ++ 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/annotation_stats.py b/annotation_stats.py index c490445..859f60f 100755 --- a/annotation_stats.py +++ b/annotation_stats.py @@ -84,6 +84,11 @@ def main(): investigate_by_cookie(sys.argv[2]) elif sys.argv[1] == 'index': pprint_utterance(int(sys.argv[2])) + elif sys.argv[1] == 'console': + import ipdb + ipdb.set_trace() + elif sys.argv[1] == 'exec': + exec('print(r.{})'.format(sys.argv[2]), {'print': print, 'r': r}) if __name__ == '__main__': diff --git a/webapp/app.py b/webapp/app.py index 07987e7..78ed9d6 100644 --- a/webapp/app.py +++ b/webapp/app.py @@ -1,6 +1,6 @@ from flask import Flask, render_template, request, make_response, jsonify +import secrets import time -import os from get_utterances import Utterance import redis import pickle @@ -46,19 +46,37 @@ if status != 'filled': status = 'filled' -def get_next(): - index = int(r.zrangebyscore(UTT_SCORES, '-inf', 'inf')[1]) +def get_utterance_for_web(index): left_context = utterances[index]['prefix'].replace('\n', '
') hour = utterances[index]['hour'].replace('\n', '
') right_context = ' '.join( utterances[index]['suffix'].split(' ')[:10]).replace('\n', '
') + return left_context, hour, right_context + + +def find_not_annotated(cookie_hash): + # XXX: should be effecient enough even though it's O(n) + for index in range(len(utterances)): + if not r.exists(f'{cookie_hash}:{index}'): + return index + + +def get_next(cookie_hash): + """returns utterance with minmum annotations if that utterance + wasn't annotated by cookie_hash user + or not yet annotated utterance by cookie_hash user""" + index = int(r.zrangebyscore(UTT_SCORES, '-inf', 'inf')[1]) + if r.exists(f'{cookie_hash}:{index}'): + index = find_not_annotated(cookie_hash) + log('found unannotated index: {}'.format(index)) + left_context, hour, right_context = get_utterance_for_web(index) # log('get_next index: {}, score: {}'.format(index, # r.zscore(UTT_SCORES, index))) return index, left_context, hour, right_context def get_next_response(cookie_hash): - index, left_context, hour, right_context = get_next() + index, left_context, hour, right_context = get_next(cookie_hash) resp = jsonify( index=index, left_context=left_context, @@ -70,10 +88,7 @@ def get_next_response(cookie_hash): def get_by_index(index): - left_context = utterances[index]['prefix'].replace('\n', '
') - hour = utterances[index]['hour'].replace('\n', '
') - right_context = ' '.join( - utterances[index]['suffix'].split(' ')[:10]).replace('\n', '
') + left_context, hour, right_context = get_utterance_for_web(index) # log('get_next index: {}, score: {}'.format(index, # r.zscore(UTT_SCORES, index))) return index, left_context, hour, right_context @@ -124,13 +139,13 @@ def annotate_redis(yesno, index, ip_addr, cookie_hash): def set_cookie(js_hash): ## TODO: dodawać nowe js_hash do listy z key bedacym cookie_hash | czy trzeba? old_cookie_hash = None + js_hash_key = 'jshash:' + js_hash cookie_hash = request.cookies.get(COOKIE_NAME) if not cookie_hash: - old_cookie_hash = r.get(js_hash) + old_cookie_hash = r.get(js_hash_key) if not old_cookie_hash: - cookie_hash = str( - int.from_bytes(os.urandom(4), byteorder='little')) - r.set(js_hash, cookie_hash) + cookie_hash = secrets.token_urlsafe(16) + r.set(js_hash_key, cookie_hash) log('Cookie not on client side. Creating new cookie.') else: log('Cookie not on client side. Getting cookie from fingerprint.') @@ -159,11 +174,12 @@ def undo(cookie_hash): r.rpush('undo:' + cookie_hash, last_action) index = int(last_action.split(':')[1]) return get_response_by_index(index, cookie_hash) + log('No last action returning None') # if no cookie-hash or action list is empty resp = None def handle_ip_cookies(ip_key, cookie_hash): - """mechanism for forcing users to use X cookies per ip but no more than X""" + """mechanism for forcing users to use maximum X cookies per ip but no more than X""" r.sadd(ip_key, cookie_hash) if int(r.ttl(ip_key)) == -1: r.expire(ip_key, 60 * 60 * 3) @@ -197,6 +213,7 @@ def http_post(): else: cookie_hash = request.cookies.get(COOKIE_NAME) if not cookie_hash: + log('No cookie hash given by client') return None if action == 'undo': resp = undo(cookie_hash) diff --git a/webapp/templates/index.html b/webapp/templates/index.html index 802e015..c5ce9b9 100644 --- a/webapp/templates/index.html +++ b/webapp/templates/index.html @@ -64,11 +64,13 @@
Kiedy nacisnąć "Tak"
Kiedy nacisnąć "Nie"
Co jeśli się pomyliłem

Jeśli się pomyliłeś nacisńij przycisk Cofnij znajdujący się w lewym górnym rogu. Możesz cofać tak długo, aż znajdziesz swoją pomyłkę. Najbliżej znajdują się Twoje ostatnio oznaczone kawałki.