From 17eabddd703231b6aa128049c3c6bed1b318e26d Mon Sep 17 00:00:00 2001 From: Matthew Scragg Date: Thu, 11 Sep 2014 17:35:17 -0500 Subject: [PATCH] fixes #2 history page bug some style changes --- realms/modules/wiki/models.py | 106 ++++++++++++++--------------- realms/modules/wiki/views.py | 6 +- realms/static/css/style.css | 9 +++ realms/templates/wiki/history.html | 2 + 4 files changed, 67 insertions(+), 56 deletions(-) diff --git a/realms/modules/wiki/models.py b/realms/modules/wiki/models.py index 96000a9..affacd2 100644 --- a/realms/modules/wiki/models.py +++ b/realms/modules/wiki/models.py @@ -12,41 +12,6 @@ from realms.lib.util import to_canonical 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(): path = None base_path = '/' @@ -54,13 +19,17 @@ class Wiki(): default_committer_name = 'Anon' default_committer_email = 'anon@anon.anon' index_page = 'home' + gittle = None repo = None def __init__(self, path): try: - self.repo = MyGittle(path) + self.gittle = Gittle(path) except NotGitRepository: - self.repo = MyGittle.init(path) + self.gittle = Gittle.init(path) + + # Dulwich repo + self.repo = self.gittle.repo self.path = path @@ -72,7 +41,7 @@ class Wiki(): if not page: # Page not found 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'] return self.write_page(name, page['data'], message=message, username=username) @@ -122,7 +91,7 @@ class Wiki(): f.write(content) if create: - self.repo.add(filename) + self.gittle.add(filename) if not message: message = "Updated %s" % name @@ -133,23 +102,36 @@ class Wiki(): if not email: email = self.default_committer_email - ret = self.repo.commit(name=username, - email=email, - message=message, - files=[filename]) + ret = self.gittle.commit(name=username, + email=email, + message=message, + files=[filename]) cache.delete(cname) return ret - def rename_page(self, old_name, new_name): - old_name, new_name = map(self.cname_to_filename, [old_name, new_name]) - self.repo.mv([(old_name, new_name)]) - self.repo.commit(name=self.default_committer_name, - email=self.default_committer_email, - message="Moving %s to %s" % (old_name, new_name), - files=[old_name]) - cache.delete_many(old_name, new_name) + def rename_page(self, old_name, new_name, user=None): + old_filename, new_filename = map(self.cname_to_filename, [old_name, new_name]) + if old_filename not in self.gittle.index: + # old doesn't exist + return None + + if new_filename in self.gittle.index: + # 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'): cached = cache.get(name) @@ -161,7 +143,7 @@ class Wiki(): sha = sha.encode('latin-1') 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: return None partials = {} @@ -194,7 +176,25 @@ class Wiki(): return ghdiff.diff(old['data'], new['data']) 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 def cname_to_filename(cname): diff --git a/realms/modules/wiki/views.py b/realms/modules/wiki/views.py index 09a671a..7f4b9da 100644 --- a/realms/modules/wiki/views.py +++ b/realms/modules/wiki/views.py @@ -45,8 +45,7 @@ def revert(): @blueprint.route("/_history/") def history(name): - history = wiki.get_history(name) - return render_template('wiki/history.html', name=name, history=history, wiki_home=url_for('wiki.page')) + return render_template('wiki/history.html', name=name, history=wiki.get_history(name)) @blueprint.route("/_edit/", methods=['GET', 'POST']) @@ -63,10 +62,11 @@ def edit(name): if edit_cname.lower() != cname.lower(): wiki.rename_page(cname, edit_cname) + """ wiki.write_page(edit_cname, request.form['content'], message=request.form['message'], - username=current_user.username) + username=current_user.username)""" else: if data: name = remove_ext(data['name']) diff --git a/realms/static/css/style.css b/realms/static/css/style.css index 8346bd3..8ad3ab2 100644 --- a/realms/static/css/style.css +++ b/realms/static/css/style.css @@ -179,4 +179,13 @@ textarea.parsley-error { .parsley-errors-list.filled { opacity: 1; +} + +a.label { + text-decoration: none !important; +} + +.diff { + font-size: 14px !important; + margin-bottom: 10px; } \ No newline at end of file diff --git a/realms/templates/wiki/history.html b/realms/templates/wiki/history.html index 90bf6ee..ebc6002 100644 --- a/realms/templates/wiki/history.html +++ b/realms/templates/wiki/history.html @@ -18,7 +18,9 @@ {% for h in history %} + {% if h.type != 'delete' %} + {% endif %} {{ h.author }} View {{ h.message }}