From 1a3aca68e37fa86776969d2ac3bdac706895f28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=81=A5=E8=BE=89?= <40563566+jianhw@users.noreply.github.com> Date: Mon, 15 Jun 2020 12:56:29 +0800 Subject: [PATCH] improve file history list (#4586) --- seahub/api2/endpoints/file_history.py | 30 +++++++++++++++++++++++---- seahub/utils/__init__.py | 4 ++-- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/seahub/api2/endpoints/file_history.py b/seahub/api2/endpoints/file_history.py index 5558eca84e..94ac02faa9 100644 --- a/seahub/api2/endpoints/file_history.py +++ b/seahub/api2/endpoints/file_history.py @@ -1,5 +1,6 @@ # Copyright (c) 2012-2016 Seafile Ltd. import logging +from datetime import datetime from rest_framework.authentication import SessionAuthentication from rest_framework.permissions import IsAuthenticated @@ -108,6 +109,14 @@ class FileHistoryView(APIView): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) + # get repo history limit + try: + keep_days = seafile_api.get_repo_history_limit(repo_id) + except Exception as e: + logger.error(e) + error_msg = 'Internal Server Error' + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) + # get file history limit = request.GET.get('limit', 50) try: @@ -125,9 +134,14 @@ class FileHistoryView(APIView): result = [] for commit in file_revisions: - info = get_file_history_info(commit, avatar_size) - info['path'] = path - result.append(info) + present_time = datetime.utcnow() + history_time = datetime.utcfromtimestamp(commit.ctime) + if (keep_days == -1) or ((present_time - history_time).days < keep_days): + info = get_file_history_info(commit, avatar_size) + info['path'] = path + result.append(info) + + next_start_commit = next_start_commit if result else False return Response({ "data": result, @@ -182,11 +196,19 @@ class NewFileHistoryView(APIView): error_msg = 'Permission denied.' return api_error(status.HTTP_403_FORBIDDEN, error_msg) + # get repo history limit + try: + history_limit = seafile_api.get_repo_history_limit(repo_id) + except Exception as e: + logger.error(e) + error_msg = 'Internal Server Error' + return api_error(status.HTTP_500_INTERNAL_SERVER_ERROR, error_msg) + start = (page - 1) * per_page count = per_page try: - file_revisions, total_count = get_file_history(repo_id, path, start, count) + file_revisions, total_count = get_file_history(repo_id, path, start, count, history_limit) except Exception as e: logger.error(e) error_msg = 'Internal Server Error' diff --git a/seahub/utils/__init__.py b/seahub/utils/__init__.py index 0ec17b50ec..df3a4700f5 100644 --- a/seahub/utils/__init__.py +++ b/seahub/utils/__init__.py @@ -712,11 +712,11 @@ if EVENTS_CONFIG_FILE: def get_org_user_events(org_id, username, start, count): return _get_events(username, start, count, org_id=org_id) - def get_file_history(repo_id, path, start, count): + def get_file_history(repo_id, path, start, count, history_limit=-1): """Return file histories """ with _get_seafevents_session() as session: - res = seafevents.get_file_history(session, repo_id, path, start, count) + res = seafevents.get_file_history(session, repo_id, path, start, count, history_limit) return res def get_log_events_by_time(log_type, tstart, tend):