Added delete structs

This commit is contained in:
Sam Ginn 2017-01-24 23:29:08 -08:00
parent a093ef5b60
commit 2bb1e8340c
1 changed files with 40 additions and 5 deletions

View File

@ -132,9 +132,18 @@ def emit_structs():
for uid in [name for name in os.listdir(STRUCTS_FOLDER) if os.path.isdir(os.path.join(STRUCTS_FOLDER, name))]:
uid_folder = os.path.join(STRUCTS_FOLDER, uid)
# get the most recent 7 structs
for fname in sorted([int(n[:-5]) for n in os.listdir(uid_folder)])[:100]:
path = os.path.join(uid_folder, str(fname) + ".json")
count = 0
for name in os.listdir(uid_folder):
if count > 100:
break
path = os.path.join(uid_folder, name)
if not os.path.isfile(path):
continue
fname = name[:-5]
try:
with open(path, 'r') as f:
lines = f.readlines()
@ -146,12 +155,21 @@ def emit_structs():
struct = json.loads(lines[2].strip())
score = score_struct(timestamp, len(upvotes))
message = {"uid": uid, "id": str(fname), "score": score, "upvotes": [up for up in upvotes], "struct": struct}
message = {"uid": uid, "id": fname, "score": score, "upvotes": [up for up in upvotes], "struct": struct}
emit("struct", message)
count += 1
except:
pass
def emit_user_structs_count():
""""Emits a count of the total number of user structs in the folder."""
path = os.path.join(STRUCTS_FOLDER, session.uid)
if os.path.isdir(path):
structs = [name for name in os.listdir(path) if os.path.isfile(os.path.join(path, name))]
emit("user_structs", {"structs": structs})
def emit_utterances():
"""Emit a list of the last 11 utterances for the 5 most recent turkers."""
latest_5 = []
@ -268,6 +286,18 @@ def get_score(data):
emit("score", {"score": score})
@socketio.on('delete_struct')
def delete_struct(data):
uid = session.uid
struct_id = data["id"]
struct_path = struct_id + ".json"
path = os.path.join(STRUCTS_FOLDER, uid, struct_path)
if (os.path.isfile(path)):
delete_dir = os.path.join(STRUCTS_FOLDER, uid, "deleted")
make_dir_if_necessary(delete_dir)
os.rename(path, os.path.join(delete_dir, struct_path))
@socketio.on('join')
def on_join(data):
"""When a user joins the "community" room, emit to them the list of
@ -306,7 +336,7 @@ def handle_share(data):
user_structs_folder = os.path.join(STRUCTS_FOLDER, session.uid)
make_dir_if_necessary(user_structs_folder)
names = os.listdir(user_structs_folder)
names = [name for name in os.listdir(user_structs_folder) if os.path.isfile(os.path.join(user_structs_folder, name))]
new_struct_id = "1"
if len(names) > 0:
new_struct_id = str(max([int(name[:-5]) for name in names]) + 1)
@ -408,6 +438,11 @@ def session(data):
log({"type": "connect"})
@socketio.on('getstructcount')
def getstructcount(data):
emit_user_structs_count()
@socketio.on('connect')
def connect():
"""Return an ok if connection worked"""