Skip to content

Commit

Permalink
Add most recent commit and link
Browse files Browse the repository at this point in the history
  * First stab at #148
  • Loading branch information
amir-zeldes committed Aug 15, 2019
1 parent b02c689 commit c6d4c6d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 13 deletions.
35 changes: 23 additions & 12 deletions editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,21 +269,30 @@ def load_page(user,admin,theform):
save_changes(doc_id,text_content)
cache.invalidate_by_doc(doc_id, "xml")

# Get GitHub repo info
git_status=False
git_last_commit = False
repo_name = generic_query("SELECT filename FROM docs WHERE id=?", (doc_id,))[0][0]
file_name = generic_query("SELECT name FROM docs WHERE id=?", (doc_id,))[0][0]
repo_info = repo_name.split('/')
git_account, git_repo = repo_info[0], repo_info[1]

# Get path for this document's serialized file in the repo
if len(repo_info) > 2:
subdir = '/'.join(repo_info[2:]) + "/"
else:
subdir = ""
if mode == "xml":
file_name = file_name.replace(" ","_") + ".xml"
else:
file_name = file_name.replace(" ","_") + "_ether.sgml"
saved_file = subdir + file_name

commit_message = ""
if theform.getvalue('commit_msg'):
commit_message = theform.getvalue('commit_msg')

if theform.getvalue('push_git') == "push_git":
repo_name = generic_query("SELECT filename FROM docs WHERE id=?", (doc_id,))[0][0]
file_name = generic_query("SELECT name FROM docs WHERE id=?", (doc_id,))[0][0]
repo_info = repo_name.split('/')
git_account, git_repo = repo_info[0], repo_info[1]
if len(repo_info) > 2:
subdir = '/'.join(repo_info[2:]) + "/"
else:
subdir = ""

# The user will indicate the subdir in the repo_name stored in the db.
# Therefore, a file may be associated with the target repo subdir zangsir/coptic-xml-tool/uploaded_commits,
Expand All @@ -300,12 +309,9 @@ def load_page(user,admin,theform):
text_content = generic_query("SELECT content FROM docs WHERE id=?", (doc_id,))[0][0]
serializable_content = build_meta_tag(doc_id) + text_content.strip() + "\n</meta>\n"
serializable_content = serializable_content.encode('utf8')
file_name = file_name.replace(" ","_") + ".xml"
else: # (mode == "ether")
text_content = ether_to_sgml(get_socialcalc(ether_url, "gd" + "_" + corpus + "_" + docname),doc_id)
serializable_content = text_content
file_name = file_name.replace(" ","_") + "_ether.sgml"
saved_file = subdir + file_name
serialize_file(serializable_content, saved_file)
git_status = push_update_to_git(git_username, git_token, saved_file, git_account, git_repo, commit_message)

Expand All @@ -325,6 +331,10 @@ def load_page(user,admin,theform):
resp = requests.post(api_call, data, auth=HTTPBasicAuth(nlp_user,nlp_password))
text_content=resp.text

# Get last commit info
if not (git_account=="account" and git_repo == "repo_name"): # Check this is not the fake default repo name
git_last_commit = get_last_commit(git_username, int(admin), git_account, git_repo, saved_file)

# Editing options
# Docname
# Filename
Expand All @@ -338,7 +348,8 @@ def load_page(user,admin,theform):
render_data['git_2fa'] = git_2fa == "true"
if git_status:
render_data['git_commit_response'] = git_status.replace('<','').replace('>','')

if git_last_commit:
render_data['git_last_commit'] = git_last_commit

# prepare embedded editor html
if mode == "ether":
Expand Down
31 changes: 31 additions & 0 deletions modules/gitdox_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,34 @@ def get_git_credentials(user, admin, code):
git_use2fa = user_dict['git_2fa'] if "git_2fa" in user_dict else "false"
return git_username, git_token, git_use2fa


def get_last_commit(user, admin, account, repo, path):
if admin==0:
return ""

git_username, git_token, _ = get_git_credentials(user,admin,None)
gh = github3.login(username=git_username, token=git_token, two_factor_callback=False)
repository = gh.repository(account, repo)
msg = False
url = ""
try:
for i, cmt in enumerate(repository.iter_commits(path=path)):
author = str(cmt.author)
msg = cmt.commit.message
#sha = cmt.commit.sha
date = cmt.commit._json_data["committer"]["date"]
url = cmt.html_url
if "T" in date:
day, time = date.split("T")
if ":" in time:
time_parts = time.split(":")
time = time_parts[0]+":"+time_parts[1]
date = day + ", " + time
break
msg = 'Latest commit ['+ date + ']: <a href="'+url+'">' + msg + "</a> (" + author + ")"
except:
pass
return msg



5 changes: 5 additions & 0 deletions templates/codemirror.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
<input type="text" name="commit_msg" id="commit_msg" placeholder="commit message" style="width:140px">
{{#git_2fa}}<input type="text" id="code_2fa" name="2fa" placeholder = "2-factor code" style="width:80px" autocomplete="off">{{/git_2fa}}
<div name="push_git" class="button h128" onclick="do_push();"> <i class="fa fa-github"></i> Commit </div>

{{#git_last_commit}}
<p><i class="fa fa-github"></i> {{{.}}}</p>
{{/git_last_commit}}

{{#git_commit_response}}
<p style="color:red;">{{.}} successful</p>
{{/git_commit_response}}
Expand Down
9 changes: 8 additions & 1 deletion templates/ethercalc.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,26 @@
<input type="text" name="commit_msg" id="commit_msg" placeholder="commit message" style="width:140px">
{{#git_2fa}}<input type="text" id="code_2fa" name="2fa" placeholder = "2-factor code" style="width:80px" autocomplete="off">{{/git_2fa}}
<div name="push_git" class="button h128" onclick="do_push();"> <i class="fa fa-github"></i> Commit </div>

{{#git_last_commit}}
<p><i class="fa fa-github"></i> {{{.}}}</p>
{{/git_last_commit}}


{{#git_commit_response}}
<p style="color:red;">{{.}} successful</p>
{{/git_commit_response}}


<h3>Export</h3>

<p>Generate XML from this spreadsheet using a stylesheet</p>
<div style="display: inline-block">
<select name="ether_stylesheet" id="ether_stylesheet">
<option>[CSV]</option>
{{#ether_stylesheets}}
<option value="{{.}}">{{.}}</option>
{{/ether_stylesheets}}
<option>[CSV]</option>
</select>
<input type="hidden" name="doc_id" id="doc_id" value="{{id}}">
<button onclick="export_ether();">Export</button>
Expand Down

0 comments on commit c6d4c6d

Please sign in to comment.