From cd2ded269699cb75b80f74f05192c464a68f8e5f Mon Sep 17 00:00:00 2001 From: lins05 Date: Tue, 14 Aug 2012 18:40:36 +0800 Subject: [PATCH] revert file from history --- templates/file_revisions.html | 2 +- templates/repo.html | 3 +++ urls.py | 3 ++- views.py | 44 +++++++++++++++++++++-------------- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/templates/file_revisions.html b/templates/file_revisions.html index 071f16e4be..823f4b52d5 100644 --- a/templates/file_revisions.html +++ b/templates/file_revisions.html @@ -53,7 +53,7 @@ {% if not commit.is_current_version %} {% if is_owner %} - 还原 + 还原 {% endif %} {% endif %} 下载 diff --git a/templates/repo.html b/templates/repo.html index 7eac2eb086..d350ca6390 100644 --- a/templates/repo.html +++ b/templates/repo.html @@ -185,6 +185,9 @@
  • 复制
  • 更新
  • {% endif %} + {% if view_history and request.user.is_authenticated %} +
  • 还原
  • + {% endif %} diff --git a/urls.py b/urls.py index b70b8823c1..6abfe5586d 100644 --- a/urls.py +++ b/urls.py @@ -11,7 +11,7 @@ from seahub.views import root, myhome, \ seafile_access_check, repo_history_changes, \ repo_upload_file, file_upload_progress_page, \ get_subdir, file_move, repo_new_dir, repo_new_file, repo_rename_file, validate_filename, \ - repo_create, repo_update_file, file_revisions, \ + repo_create, repo_update_file, repo_revert_file, file_revisions, \ get_shared_link, view_shared_file, remove_shared_link, send_shared_link, \ crocodoc_upload, crocodoc_status, crocodoc_session from seahub.notifications.views import notification_list @@ -55,6 +55,7 @@ urlpatterns = patterns('', (r'^repo/create/$', repo_create), (r'^repo/upload_check/$', validate_filename), (r'^repo/file_rename/$', repo_rename_file), + url(r'^repo/revert_file/(?P[^/]+)/$', repo_revert_file, name='repo_revert_file'), url(r'^repo/upload_file/(?P[^/]+)/$', repo_upload_file, name='repo_upload_file'), url(r'^repo/update_file/(?P[^/]+)/$', repo_update_file, name='repo_update_file'), url(r'^repo/file_revisions/(?P[^/]+)/$', file_revisions, name='file_revisions'), diff --git a/views.py b/views.py index 6a3d4f1f00..7f9ecedcf1 100644 --- a/views.py +++ b/views.py @@ -468,11 +468,8 @@ def repo_history_revert(request, repo_id): if not commit_id: return render_error(request, u'请指定历史记录 ID') - res = request.user.username.split('@') - user_name = res[0] - try: - seafserv_threaded_rpc.revert_on_server(repo_id, commit_id, user_name) + seafserv_threaded_rpc.revert_on_server(repo_id, commit_id, request.user.username) except SearpcError, e: if e.msg == 'Bad arguments': return render_error(request, u'非法参数') @@ -1599,6 +1596,30 @@ def render_file_revisions (request, repo_id): 'is_owner': is_owner, }, context_instance=RequestContext(request)) +@login_required +def repo_revert_file (request, repo_id): + commit_id = request.GET.get('commit') + path = request.GET.get('p') + + if not (commit_id and path): + return render_error(request, u"参数错误") + + try: + ret = seafserv_threaded_rpc.revert_file (repo_id, commit_id, + path.encode('utf-8'), request.user.username) + except Exception, e: + return render_error(request, str(e)) + else: + url = reverse('repo', args=[repo_id]) + if ret == 1: + msg = u"已经还原被删除的文件 %s 到根目录下" % path.lstrip('/') + messages.add_message(request, messages.INFO, msg) + else: + msg = u"已经还原文件 %s" % path.lstrip('/') + messages.add_message(request, messages.INFO, msg) + url += u'?p=%s' % os.path.dirname(path) + return HttpResponseRedirect(url) + @login_required def file_revisions(request, repo_id): if request.method != 'GET': @@ -1607,7 +1628,7 @@ def file_revisions(request, repo_id): op = request.GET.get('op') if not op: return render_file_revisions(request, repo_id) - elif op != 'revert' and op != 'download' and op != 'view': + elif op != 'download' and op != 'view': return render_error(request) commit_id = request.GET.get('commit') @@ -1616,18 +1637,7 @@ def file_revisions(request, repo_id): if not (commit_id and path): return render_error(request) - if op == 'revert': - try: - seafserv_threaded_rpc.revert_file (repo_id, commit_id, - path, request.user.username) - except Exception, e: - return render_error(request, str(e)) - else: - parent_dir = os.path.dirname(path) - url = reverse('repo', args=[repo_id]) + ('?p=%s' % parent_dir) - return HttpResponseRedirect(url) - - elif op == 'download': + if op == 'download': def handle_download(): parent_dir = os.path.dirname(path) file_name = os.path.basename(path)