Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support pagination when get repo share links #6726

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions seahub/api2/endpoints/repo_share_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from seahub.api2.throttling import UserRateThrottle
from seahub.api2.authentication import TokenAuthentication

from seahub.avatar.settings import AVATAR_DEFAULT_SIZE
from seahub.avatar.templatetags.avatar_tags import api_avatar_url
from seahub.base.templatetags.seahub_tags import email2nickname, \
email2contact_email
from seahub.utils import gen_shared_link
Expand All @@ -27,7 +29,7 @@
logger = logging.getLogger(__name__)


def get_share_link_info(fileshare):
def get_share_link_info(fileshare, avatar_size=AVATAR_DEFAULT_SIZE):

data = {}
token = fileshare.token
Expand All @@ -53,6 +55,9 @@ def get_share_link_info(fileshare):
data['creator_name'] = email2nickname(creator_email)
data['creator_contact_email'] = email2contact_email(creator_email)

url, _, _ = api_avatar_url(creator_email, avatar_size)
data['creator_avatar'] = url

data['path'] = path
data['obj_name'] = obj_name
data['is_dir'] = True if fileshare.s_type == 'd' else False
Expand Down Expand Up @@ -90,11 +95,25 @@ def get(self, request, repo_id):
error_msg = 'Permission denied.'
return api_error(status.HTTP_403_FORBIDDEN, error_msg)

try:
current_page = int(request.GET.get('page', '1'))
per_page = int(request.GET.get('per_page', '25'))
except ValueError:
current_page = 1
per_page = 25

offset = per_page * (current_page - 1)

try:
avatar_size = int(request.GET.get('avatar_size', AVATAR_DEFAULT_SIZE))
except ValueError:
avatar_size = AVATAR_DEFAULT_SIZE

result = []
fileshares = FileShare.objects.filter(repo_id=repo_id)
fileshares = FileShare.objects.filter(repo_id=repo_id)[offset:offset + per_page]

for fileshare in fileshares:
link_info = get_share_link_info(fileshare)
link_info = get_share_link_info(fileshare, avatar_size)
link_info['repo_id'] = repo_id
link_info['repo_name'] = repo.name
result.append(link_info)
Expand Down
Loading