fixes #2 history page bug

some style changes
This commit is contained in:
Matthew Scragg 2014-09-11 17:35:17 -05:00
parent 3419ee4f6c
commit 17eabddd70
4 changed files with 67 additions and 56 deletions

View file

@ -12,41 +12,6 @@ from realms.lib.util import to_canonical
from realms import cache from realms import cache
class MyGittle(Gittle):
def file_history(self, path):
"""Returns all commits where given file was modified
"""
versions = []
commits_info = self.commit_info()
seen_shas = set()
for commit in commits_info:
try:
files = self.get_commit_files(commit['sha'], paths=[path])
file_path, file_data = files.items()[0]
except IndexError:
continue
file_sha = file_data['sha']
if file_sha in seen_shas:
continue
else:
seen_shas.add(file_sha)
versions.append(dict(author=commit['author']['name'],
time=commit['time'],
file_sha=file_sha,
sha=commit['sha'],
message=commit['message']))
return versions
def mv_fs(self, file_pair):
old_name, new_name = file_pair
os.rename(self.path + "/" + old_name, self.path + "/" + new_name)
class Wiki(): class Wiki():
path = None path = None
base_path = '/' base_path = '/'
@ -54,13 +19,17 @@ class Wiki():
default_committer_name = 'Anon' default_committer_name = 'Anon'
default_committer_email = 'anon@anon.anon' default_committer_email = 'anon@anon.anon'
index_page = 'home' index_page = 'home'
gittle = None
repo = None repo = None
def __init__(self, path): def __init__(self, path):
try: try:
self.repo = MyGittle(path) self.gittle = Gittle(path)
except NotGitRepository: except NotGitRepository:
self.repo = MyGittle.init(path) self.gittle = Gittle.init(path)
# Dulwich repo
self.repo = self.gittle.repo
self.path = path self.path = path
@ -72,7 +41,7 @@ class Wiki():
if not page: if not page:
# Page not found # Page not found
return None return None
commit_info = gittle.utils.git.commit_info(self.repo[commit_sha.encode('latin-1')]) commit_info = gittle.utils.git.commit_info(self.gittle[commit_sha.encode('latin-1')])
message = commit_info['message'] message = commit_info['message']
return self.write_page(name, page['data'], message=message, username=username) return self.write_page(name, page['data'], message=message, username=username)
@ -122,7 +91,7 @@ class Wiki():
f.write(content) f.write(content)
if create: if create:
self.repo.add(filename) self.gittle.add(filename)
if not message: if not message:
message = "Updated %s" % name message = "Updated %s" % name
@ -133,23 +102,36 @@ class Wiki():
if not email: if not email:
email = self.default_committer_email email = self.default_committer_email
ret = self.repo.commit(name=username, ret = self.gittle.commit(name=username,
email=email, email=email,
message=message, message=message,
files=[filename]) files=[filename])
cache.delete(cname) cache.delete(cname)
return ret return ret
def rename_page(self, old_name, new_name): def rename_page(self, old_name, new_name, user=None):
old_name, new_name = map(self.cname_to_filename, [old_name, new_name]) old_filename, new_filename = map(self.cname_to_filename, [old_name, new_name])
self.repo.mv([(old_name, new_name)]) if old_filename not in self.gittle.index:
self.repo.commit(name=self.default_committer_name, # old doesn't exist
email=self.default_committer_email, return None
message="Moving %s to %s" % (old_name, new_name),
files=[old_name]) if new_filename in self.gittle.index:
cache.delete_many(old_name, new_name) # file is being overwritten, but that is ok, it's git!
pass
os.rename(os.path.join(self.path, old_filename), os.path.join(self.path, new_filename))
self.gittle.add(new_filename)
self.gittle.rm(old_filename)
self.gittle.commit(name=getattr(user, 'username', self.default_committer_name),
email=getattr(user, 'email', self.default_committer_email),
message="Moved %s to %s" % (old_name, new_name),
files=[old_filename, new_filename])
cache.delete_many(old_filename, new_filename)
def get_page(self, name, sha='HEAD'): def get_page(self, name, sha='HEAD'):
cached = cache.get(name) cached = cache.get(name)
@ -161,7 +143,7 @@ class Wiki():
sha = sha.encode('latin-1') sha = sha.encode('latin-1')
try: try:
data = self.repo.get_commit_files(sha, paths=[name]).get(name) data = self.gittle.get_commit_files(sha, paths=[name]).get(name)
if not data: if not data:
return None return None
partials = {} partials = {}
@ -194,7 +176,25 @@ class Wiki():
return ghdiff.diff(old['data'], new['data']) return ghdiff.diff(old['data'], new['data'])
def get_history(self, name): def get_history(self, name):
return self.repo.file_history(self.cname_to_filename(name)) file_path = self.cname_to_filename(name)
versions = []
walker = self.repo.get_walker(paths=[file_path], max_entries=100)
for entry in walker:
change_type = None
for change in entry.changes():
if change.old.path == file_path:
change_type = change.type
elif change.new.path == file_path:
change_type = change.type
author_name, author_email = entry.commit.author.split('<')
versions.append(dict(
author=author_name.strip(),
time=entry.commit.author_time,
message=entry.commit.message,
sha=entry.commit.id,
type=change_type))
return versions
@staticmethod @staticmethod
def cname_to_filename(cname): def cname_to_filename(cname):

View file

@ -45,8 +45,7 @@ def revert():
@blueprint.route("/_history/<name>") @blueprint.route("/_history/<name>")
def history(name): def history(name):
history = wiki.get_history(name) return render_template('wiki/history.html', name=name, history=wiki.get_history(name))
return render_template('wiki/history.html', name=name, history=history, wiki_home=url_for('wiki.page'))
@blueprint.route("/_edit/<name>", methods=['GET', 'POST']) @blueprint.route("/_edit/<name>", methods=['GET', 'POST'])
@ -63,10 +62,11 @@ def edit(name):
if edit_cname.lower() != cname.lower(): if edit_cname.lower() != cname.lower():
wiki.rename_page(cname, edit_cname) wiki.rename_page(cname, edit_cname)
"""
wiki.write_page(edit_cname, wiki.write_page(edit_cname,
request.form['content'], request.form['content'],
message=request.form['message'], message=request.form['message'],
username=current_user.username) username=current_user.username)"""
else: else:
if data: if data:
name = remove_ext(data['name']) name = remove_ext(data['name'])

View file

@ -179,4 +179,13 @@ textarea.parsley-error {
.parsley-errors-list.filled { .parsley-errors-list.filled {
opacity: 1; opacity: 1;
}
a.label {
text-decoration: none !important;
}
.diff {
font-size: 14px !important;
margin-bottom: 10px;
} }

View file

@ -18,7 +18,9 @@
{% for h in history %} {% for h in history %}
<tr> <tr>
<td class="checkbox-cell text-center"> <td class="checkbox-cell text-center">
{% if h.type != 'delete' %}
<input type="checkbox" name="versions[]" value="{{ h.sha }}" /> <input type="checkbox" name="versions[]" value="{{ h.sha }}" />
{% endif %}
</td> </td>
<td>{{ h.author }}</td> <td>{{ h.author }}</td>
<td><a href="{{ url_for('wiki.commit', name=name, sha=h.sha) }}" class='label label-primary'>View</a> {{ h.message }} </td> <td><a href="{{ url_for('wiki.commit', name=name, sha=h.sha) }}" class='label label-primary'>View</a> {{ h.message }} </td>