Skip to content

Commit

Permalink
Merge pull request #109 from lgessler/master
Browse files Browse the repository at this point in the history
Make metadata editing async in editor.py, finish migration to mustache
  • Loading branch information
amir-zeldes authored Nov 4, 2018
2 parents a6598b4 + 794f0b6 commit fde7de0
Show file tree
Hide file tree
Showing 29 changed files with 800 additions and 784 deletions.
23 changes: 6 additions & 17 deletions admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from modules.dataenc import pass_dec, pass_enc
from paths import get_menu
from editor import harvest_meta
from modules.ether import make_spreadsheet, get_ether_stylesheet_select, get_corpus_select
from modules.ether import make_spreadsheet, get_ether_stylesheets
from modules.renderer import render
from passlib.apps import custom_app_context as pwd_context
import github3
Expand All @@ -33,19 +33,8 @@
project = config["project"]


def get_status_select():

status_list = open(prefix+"status.tab").read().replace("\r","").split("\n")

select = """<select name="status_select" id="status_select">\n"""
options = ""
for stat in status_list:
options += '\t<option value="'+stat+'">'+stat+'</option>\n'

select += '\t<option value="--ALL--">[all statuses]</option>\n'

select += options + "</select>\n"
return select
def get_statuses():
return open(prefix+"status.tab").read().replace("\r","").split("\n")


def write_user_file(username,password,admin,email,realname,git_username,git_password,git_2fa=False):
Expand Down Expand Up @@ -158,9 +147,9 @@ def load_admin(user, admin, theform):
render_data['userfiles'].append(userfile)

# get html for dropdown selections
render_data['corpus_select_html'] = get_corpus_select()
render_data['status_select_html'] = get_status_select()
render_data['stylesheet_select_html'] = get_ether_stylesheet_select()
render_data['corpora'] = [x[0] for x in get_corpora()]
render_data['statuses'] = get_statuses()
render_data['ether_stylesheets'] = get_ether_stylesheets()

# handle upload
imported = 0
Expand Down
61 changes: 59 additions & 2 deletions css/gitdox.css
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,55 @@ padding: 10px;
height: 12.8px;
}

#ValidationTableContainer{
width: 100%}
/* for validation_rules */
ul.tabs{
margin: 0px;
padding: 0px;
list-style: none;
}

ul.tabs li{
background: none;
display: inline-block;
padding: 10px 15px;
cursor: pointer;
min-width: 100px;
font-size: 18px;
font-weight: 600;
color: #222;
text-align: center;
}

ul.tabs li.current{
background: #ededed;
color: #222;
}

.tab-content{
display: none;
background: #ededed;
padding: 15px;
}

.tab-content.current{
display: inherit;
}

/* override jtable styles for consistency with rest of gitdox */
.jtable-title {
font-size: 16px !important;
border: none !important;
border-radius: 0 !important;
background: #eeeeee !important;
}
table.jtable {
border: none !important;
}

.ui-widget-overlay {
opacity: 0.5 !important;
}


.jtable td{
word-break: break-all;
Expand All @@ -182,3 +229,13 @@ tfoot {

#filter_id{width:30px}
#filter_mode{width:60px}


table.admin {
font-family: arial, sans-serif;
border-collapse: collapse;
}
table.admin td, table.admin th {
text-align: left;
padding: 8px;
}
111 changes: 20 additions & 91 deletions editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import platform, re
from paths import ether_url, get_menu, get_nlp_credentials
from modules.ether import make_spreadsheet, delete_spreadsheet, sheet_exists, get_socialcalc, ether_to_sgml, \
build_meta_tag, get_ether_stylesheet_select, get_file_list
build_meta_tag, get_ether_stylesheets, get_file_list
from modules.renderer import render

# Support IIS site prefix on Windows
Expand Down Expand Up @@ -66,6 +66,11 @@ def serialize_file(text_content,file_name):
f.write(text_content)#.encode("utf8"))
f.close()

def get_user_list():
user_list=[]
scriptpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
userdir = scriptpath + "users" + os.sep
return get_file_list(userdir,"ini",forbidden=["admin","default","config"],hide_extension=True)

def load_page(user,admin,theform):
global ether_url
Expand Down Expand Up @@ -94,6 +99,9 @@ def load_page(user,admin,theform):
else:
git_username, git_token, git_2fa = (None, None, None)

# dict of variables we'll need to render the html
render_data = {}

if theform.getvalue('id'):
doc_id = theform.getvalue('id')
if int(doc_id) > int(max_id):
Expand Down Expand Up @@ -313,94 +321,25 @@ def load_page(user,admin,theform):
# Editing options
# Docname
# Filename
push_git = """<input type="hidden" name="push_git" id="push_git" value="">
<input type="text" name="commit_msg" id="commit_msg" placeholder="commit message here" style="width:140px">"""
if git_2fa == "true":
push_git += """<input type="text" id="code_2fa" name="2fa" placeholder = "2-factor code" style="width:80px" autocomplete="off">"""
push_git += """<div name="push_git" class="button h128" onclick="do_push();"> <i class="fa fa-github"></i> Commit </div>
"""

status_list = open(prefix+"status.tab").read().replace("\r","").split("\n")
render_data['status_options'] = [{'text': x, 'selected': x == status} for x in status_list]
render_data['assignee_options'] = [{'text': x, 'selected': x == assignee} for x in get_user_list()]
render_data['mode_options'] = [{'text': x, 'selected': x == mode} for x in ["xml", "ether"]]
render_data['nlp_service'] = {'xml_button_html': xml_nlp_button.decode("utf8"),
'spreadsheet_button_html': spreadsheet_nlp_button.decode("utf8"),
'disabled': user == "demo" or mode == "ether"}
render_data['git_2fa'] = git_2fa == "true"
if git_status:
# Remove some html keyword symbols in the commit message returned by github3
push_msg=git_status.replace('<','')
push_msg=push_msg.replace('>','')
push_git+="""<p style='color:red;'>""" + push_msg + ' successful' + """</p>"""
render_data['git_commit_response'] = git_status.replace('<','').replace('>','')

status_list = open(prefix+"status.tab").read().replace("\r","").split("\n")

options = ""
for stat in status_list:
options +='<option value="'+stat+'">'+stat+'</option>\n'
options = options.replace('">'+status +'<', '" selected="selected">'+status+'<')

edit_status="""<select name="edit_status" onchange='do_save();'>"""

edit_status += options+"</select>"

# Get user_list from the logintools
user_list=[]
scriptpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
userdir = scriptpath + "users" + os.sep

user_list = get_file_list(userdir,"ini",forbidden=["admin","default","config"],hide_extension=True)

edit_assignee="""<select name="edit_assignee" onchange="do_save();">"""
for list_user in user_list:
assignee_select=""
user_name=list_user
if user_name==assignee:
assignee_select="selected"
edit_assignee+="""<option value='""" + user_name + "' %s>" + user_name + """</option>"""
edit_assignee=edit_assignee%assignee_select
edit_assignee+="</select>"

edit_mode = '''<select name="edit_mode" id="edit_mode" onchange="do_save();">\n<option value="xml">xml</option>\n<option value="ether">spreadsheet</option>\n</select>'''
edit_mode = edit_mode.replace(mode+'"', mode+'" selected="selected"')

# Metadata
if theform.getvalue('metakey'):
metakey = theform.getvalue('metakey')
metavalue = theform.getvalue('metavalue').replace("\t","").replace("\n","").replace("\r","")
if user != "demo":
save_meta(int(doc_id),metakey.decode("utf8"),metavalue.decode("utf8"))
if theform.getvalue('metaid'):
metaid = theform.getvalue('metaid')
if user != "demo":
delete_meta(metaid, doc_id)
if theform.getvalue('corpus_metakey'):
metakey = theform.getvalue('corpus_metakey')
metavalue = theform.getvalue('corpus_metavalue').replace("\t","").replace("\n","").replace("\r","")
if user != "demo":
save_meta(int(doc_id),metakey.decode("utf8"),metavalue.decode("utf8"),corpus=True)
if theform.getvalue('corpus_metaid'):
metaid = theform.getvalue('corpus_metaid')
if user != "demo":
delete_meta(metaid, doc_id, corpus=True)

nlp_service = """<div class="button h128" name="nlp_xml_button" onclick="document.getElementById('nlp_xml').value='do_nlp_xml'; do_save();"> """ + xml_nlp_button + """</div>""" + \
"""<div class="button h128" name="nlp_ether_button" onclick="document.getElementById('nlp_spreadsheet').value='do_nlp_spreadsheet'; do_save();">"""+ spreadsheet_nlp_button + """</div>"""
nlp_service = nlp_service.decode("utf8")

disabled_nlp_service = """<div class="button disabled h128" name="nlp_xml_button">"""+xml_nlp_button+"""</div>""" + \
"""<div class="button disabled h128" name="nlp_ether_button">""" +spreadsheet_nlp_button + """</div>"""
disabled_nlp_service = disabled_nlp_service.decode("utf8")

# Disable NLP services in demo
if user == "demo":
nlp_service = disabled_nlp_service

# dict of variables we'll need to render the html
render_data = {}

# prepare embedded editor html
if mode == "ether":
render_data['ether_mode'] = True

ether_url += "gd_" + corpus + "_" + docname
render_data['ether_url'] = ether_url

stylesheet_select = get_ether_stylesheet_select()
render_data['ether_stylesheet_select_html'] = stylesheet_select
render_data['ether_stylesheets'] = get_ether_stylesheets()

if "file" in theform and user != "demo":
fileitem = theform["file"]
Expand Down Expand Up @@ -434,15 +373,6 @@ def load_page(user,admin,theform):
render_data['text_content'] = text_content
render_data['repo'] = repo_name

render_data['edit_status_html'] = edit_status
render_data['edit_assignee_html'] = edit_assignee
render_data['edit_mode_html'] = edit_mode
render_data['metadata_html'] = print_meta(doc_id)
render_data['corpus_metadata_html'] = print_meta(doc_id,corpus=True)

render_data['disabled_nlp_html'] = disabled_nlp_service
render_data['nlp_html'] = nlp_service

render_data["admin_gt_zero"] = int(admin) > 0
render_data["admin_eq_three"] = admin == "3"

Expand All @@ -457,10 +387,9 @@ def load_page(user,admin,theform):
doc_vars["name"] = doc[2]
render_data['docs'].append(doc_vars)

render_data["github_push_html"] = push_git

render_data["can_save"] = not (int(admin) < 3)
render_data["editor_help_link_html"] = editor_help_link
render_data["first_load"] = len(theform.keys()) == 1

return render("editor", render_data)

Expand Down
119 changes: 119 additions & 0 deletions editor_metadata_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
import cgi
import os
import platform
from modules.gitdox_sql import *
from modules.logintools import login

parameter = cgi.FieldStorage()
action = parameter.getvalue("action")
id = parameter.getvalue("id")
docid = parameter.getvalue("docid")
key = parameter.getvalue("key")
value = parameter.getvalue("value")
corpus = parameter.getvalue("corpus")

if platform.system() == "Windows":
prefix = "transc\\"
else:
prefix = ""

def read_options(**kwargs):
if "file" in kwargs:
kwargs["file"] = prefix + kwargs["file"]
names = open(kwargs["file"],'r').read().replace("\r","").split("\n")
names = list(name[:name.find("\t")] for name in names)
elif "names" in kwargs:
names = kwargs[names]
selected = kwargs["selected"] if "selected" in kwargs else None
return names

def row_to_dict(row):
return {'id': row[1],
'docid': row[0],
'key': row[2],
'value': row[3]}

def get_metadata():
resp = {}
try:
resp['Result'] = 'OK'
resp['Records'] = [row_to_dict(r) for r in get_doc_meta(docid, corpus=corpus)]
print json.dumps(resp)
except:
resp['Result'] = 'Error'
resp['Message'] = 'Could not fetch metadata'
print json.dumps(resp)

def get_default_key_options():
resp = {}
try:
resp['Result'] = 'OK'
if not corpus:
resp['Options'] = read_options(file='..' + os.sep + 'metadata_fields.tab')
else:
resp['Options'] = read_options(file='..' + os.sep + 'corpus_metadata_fields.tab')
print json.dumps(resp)
except:
resp['Result'] = 'Error'
resp['Message'] = 'Could not fetch metadata key options'
print json.dumps(resp)

def create_metadata():
resp = {}
try:
id = save_meta(int(docid), key.decode("utf8"), value.decode("utf8"), corpus=corpus)
resp['Result'] = 'OK'
resp['Record'] = {'id': id,
'docid': docid,
'key': key,
'value': value}
print json.dumps(resp)
except:
resp['Result'] = 'Error'
resp['Message'] = 'Could not create metadata'
print json.dumps(resp)

def delete_metadata():
resp = {}
try:
delete_meta(int(id), int(docid), corpus=corpus)
resp['Result'] = 'OK'
print json.dumps(resp)
except:
resp['Result'] = 'Error'
resp['Message'] = 'Could not delete metadata'
print json.dumps(resp)



def open_main_server():
thisscript = os.environ.get('SCRIPT_NAME', '')
loginaction = None
theform = cgi.FieldStorage()
scriptpath = os.path.dirname(os.path.realpath(__file__)) + os.sep
userdir = scriptpath + "users" + os.sep
loginaction, userconfig = login(theform, userdir, thisscript, loginaction)
user = userconfig["username"]
admin = userconfig["admin"]

print "Content-type:application/json\r\n\r\n"
if action == "list":
get_metadata()
elif action == "keys":
get_default_key_options()
elif user == "demo":
print json.dumps({'Result': 'Error', 'Message': 'Demo user may not make changes.'})
elif action == "create":
create_metadata()
elif action == "delete":
delete_metadata()
else:
print json.dumps({'Result': 'Error',
'Message': 'Unknown action: "' + str(action) + '"'})

if __name__ == '__main__':
open_main_server()
Loading

0 comments on commit fde7de0

Please sign in to comment.