def end_session(request): if "session_file" not in request.session: if "open_session" in request.session: del request.session["open_session"] out_message = "You do not currently have an open session" return render(request, "confirmation.html", {"out_message": out_message}) else: # package up everything in file, create a json file session_file_name = "session_file" temp_session_filename = request.session[session_file_name] if not os.path.isfile(temp_session_filename): if "open_session" in request.session: del request.session["open_session"] out_message = "You do not have any new edits or updates to lexical entries." return render(request, "confirmation.html", {"out_message": out_message}) temp_session_filename_base = os.path.basename(temp_session_filename) json_out = temp_sessions_to_json(temp_session_filename) # entries in json format today_uniq = datetime.today().strftime(settings.FORMAT_SESSIONS_DATE) # today's date (unique identifier) if "admin" in request.session["user_permissions"][request.session["username"]] and settings.ADMIN_BYPASS: request.session["lexicon"], request.session["last_used_lexid"] = renew_lexicon_stub() save_to_lexicon(json_out, request.session["lexicon"], request.session["last_used_lexid"]) request.session.modified = True # print(json_out, request.session['lexicon']['L14217']) shutil.move( settings.LEXICON_DATA_DICT, settings.LEXICON_DATA_DICT_BACKUP.replace(".json", "-" + today_uniq + ".json"), ) with open(settings.LEXICON_DATA_DICT, "w") as fout: json.dump(request.session["lexicon"], fout, ensure_ascii=True) del request.session["open_session"] out_message = "Your entries have been saved to the lexicon." else: # save to settings.LEXICON_DATA_SESSIONS under the same filename_base (use dump) fname = temp_session_filename_base.split("-")[0] + "-" + today_uniq + ".json" with open(os.path.join(settings.LEXICON_DATA_SESSIONS, fname), "w") as fout: fout.write(json.dumps(json_out, ensure_ascii=True, sort_keys=True)) del request.session["open_session"] out_message = "Your entries have been saved for adjudication." # delete session_file os.remove(temp_session_filename) del request.session[session_file_name] return render(request, "confirmation.html", {"out_message": out_message})
def commit_adjudication(request, filename): temp_session_adjudication = os.path.join(settings.LEXICON_DATA_ADJUDICATION_TEMP, filename + ".json") temp_session_adjudication_discard = os.path.join( settings.LEXICON_DATA_ADJUDICATION_TEMP, filename + "-discarded.json" ) session_adjudication = os.path.join( settings.LEXICON_DATA_ADJUDICATION + request.session["username"], filename + ".json" ) if not os.path.isfile(temp_session_adjudication) and not os.path.isfile(temp_session_adjudication_discard): out_message = "You do not currently do not have adjudicated instances." else: if os.path.isfile(temp_session_adjudication): with open(session_adjudication) as fin: session_adjudication_json = json.load(fin) json_out = temp_sessions_to_json(temp_session_adjudication) # entries in json format if len(session_adjudication_json) > len(json_out): adjudicated_entries = get_lexids(json_out) for_discard = [] for entryid, entry in session_adjudication_json.items(): if entry["lexid"] not in adjudicated_entries: for_discard.append(entry) if len(for_discard) > 0: with open(temp_session_adjudication_discard, "a") as fout: for d in for_discard: fout.write(json.dumps(d) + "\n") today_uniq = datetime.today().strftime(settings.FORMAT_SESSIONS_DATE) # today's date (unique identifier) request.session["lexicon"], request.session["last_used_lexid"] = renew_lexicon_stub() save_to_lexicon(json_out, request.session["lexicon"], request.session["last_used_lexid"]) request.session.modified = True shutil.move( settings.LEXICON_DATA_DICT, settings.LEXICON_DATA_DICT_BACKUP.replace(".json", "-" + today_uniq + ".json"), ) with open(settings.LEXICON_DATA_DICT, "w") as fout: json.dump(request.session["lexicon"], fout, ensure_ascii=True) # move temp_session_files to adjudicator directory, and rename as committed shutil.move(temp_session_adjudication, session_adjudication.replace(".json", "-committed.json")) # save original adjudication and temp duscaed file for historical record shutil.move(session_adjudication, session_adjudication.replace(".json", "-adjudicated.json")) if os.path.isfile(temp_session_adjudication_discard): shutil.move(temp_session_adjudication_discard, session_adjudication.replace(".json", "-discarded.json")) out_message = "Your adjudications have been saved to the lexicon." return render(request, "confirmation.html", {"out_message": out_message})
def batch_entry(request): # note that this parallels end_session() batch_fields = ["morphology", "base_form", "semantic_domain", "status"] entries = {} for k in request.POST: if k == "csrfmiddlewaretoken": continue lid, val = k.split("-") if lid not in entries: entries[lid] = {} entries[lid][val] = request.POST.get(k) json_out = {} today_uniq = datetime.today().strftime(settings.FORMAT_SESSIONS_DATE) # today's date (unique identifier) for lid, val in entries.items(): lexical_item = request.session["lexicon"][lid] lexical_item["lexid"] = lid # save_to_lexicon() needs this has_changes = False for b in batch_fields: if b not in val: continue if (val[b].strip() and b not in lexical_item) or ( b in lexical_item and val[b].strip() != lexical_item[b].strip() ): lexical_item[b] = val[b] has_changes = True if has_changes: json_out[lid] = lexical_item request.session["lexicon"], request.session["last_used_lexid"] = renew_lexicon_stub() save_to_lexicon(json_out, request.session["lexicon"], request.session["last_used_lexid"]) request.session.modified = True shutil.move( settings.LEXICON_DATA_DICT, settings.LEXICON_DATA_DICT_BACKUP.replace(".json", "-" + today_uniq + ".json") ) with open(settings.LEXICON_DATA_DICT, "w") as fout: json.dump(request.session["lexicon"], fout, ensure_ascii=True) return render(request, "confirmation.html", {"out_message": "Your changes have been saved to the lexicon"})