Merge pull request #87 from plers-org/sm/#72
✏️ Fix: [#72] 페이지네이션에 total_page 추가
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import requests
|
import requests, math
|
||||||
|
|
||||||
from .models import *
|
from .models import *
|
||||||
from projects.models import *
|
from projects.models import *
|
||||||
@@ -133,4 +133,7 @@ class GithubTokenService:
|
|||||||
"scope": data.get("scope", ""),
|
"scope": data.get("scope", ""),
|
||||||
"token_type": data.get("token_type", "bearer"),
|
"token_type": data.get("token_type", "bearer"),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def cal_github_total_page(list_count, page_size):
|
||||||
|
return math.ceil(list_count / page_size)
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
@@ -36,7 +38,7 @@ from google.auth.transport import requests
|
|||||||
from github import Github, GithubException
|
from github import Github, GithubException
|
||||||
|
|
||||||
CACHE_TIMEOUT = 60 * 60
|
CACHE_TIMEOUT = 60 * 60
|
||||||
COMMIT_PAGE_SIZE = 20
|
PAGE_SIZE = 20
|
||||||
|
|
||||||
class RefreshAPIView(APIView):
|
class RefreshAPIView(APIView):
|
||||||
permission_classes = [AllowAny]
|
permission_classes = [AllowAny]
|
||||||
@@ -129,7 +131,7 @@ class GithubAPIViewSet(viewsets.ViewSet):
|
|||||||
token = self.request.user.github_token.access_token
|
token = self.request.user.github_token.access_token
|
||||||
cache.set(cache_key, token, timeout=CACHE_TIMEOUT)
|
cache.set(cache_key, token, timeout=CACHE_TIMEOUT)
|
||||||
|
|
||||||
return Github(token, per_page=COMMIT_PAGE_SIZE)
|
return Github(token, per_page=PAGE_SIZE)
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def github(self):
|
def github(self):
|
||||||
@@ -158,11 +160,17 @@ class GithubAPIViewSet(viewsets.ViewSet):
|
|||||||
return Response({"is_page_gte_1": False, "detail": "page and page_size must be >= 1"}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({"is_page_gte_1": False, "detail": "page and page_size must be >= 1"}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
user = self.github.get_user()
|
user = self.github.get_user()
|
||||||
data = [
|
repo_list = user.get_repos()
|
||||||
{'full_name': r.full_name}
|
total_page = GithubTokenService.cal_github_total_page(repo_list.totalCount, PAGE_SIZE)
|
||||||
for r in user.get_repos().get_page(page-1)
|
repos = repo_list.get_page(page - 1)
|
||||||
]
|
data = [{'full_name': r.full_name} for r in repos]
|
||||||
return Response(data, status=status.HTTP_200_OK)
|
|
||||||
|
return Response({
|
||||||
|
"page": page,
|
||||||
|
"page_size": PAGE_SIZE,
|
||||||
|
"total_page": total_page,
|
||||||
|
"results": data
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@action(detail=False, methods=["get"], url_path="commits")
|
@action(detail=False, methods=["get"], url_path="commits")
|
||||||
def commits(self, request):
|
def commits(self, request):
|
||||||
@@ -180,7 +188,9 @@ class GithubAPIViewSet(viewsets.ViewSet):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
repo = self.github.get_repo(repo_full_name)
|
repo = self.github.get_repo(repo_full_name)
|
||||||
commits = repo.get_commits().get_page(page-1)
|
commit_list = repo.get_commits()
|
||||||
|
total_page = GithubTokenService.cal_github_total_page(commit_list.totalCount, PAGE_SIZE)
|
||||||
|
commits = commit_list.get_page(page - 1)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
@@ -191,7 +201,8 @@ class GithubAPIViewSet(viewsets.ViewSet):
|
|||||||
|
|
||||||
return Response({
|
return Response({
|
||||||
"page": page,
|
"page": page,
|
||||||
"page_size": COMMIT_PAGE_SIZE,
|
"page_size": PAGE_SIZE,
|
||||||
|
"total_page": total_page,
|
||||||
"results": data
|
"results": data
|
||||||
}, status=status.HTTP_200_OK)
|
}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@@ -233,9 +244,17 @@ class GithubAPIViewSet(viewsets.ViewSet):
|
|||||||
return Response({"is_page_gte_1": False, "detail": "page and page_size must be >= 1"}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({"is_page_gte_1": False, "detail": "page and page_size must be >= 1"}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
user = self.github.get_user()
|
user = self.github.get_user()
|
||||||
orgs = user.get_orgs().get_page(page-1)
|
org_list = user.get_orgs()
|
||||||
data = {"org_login": [org.login for org in orgs]}
|
total_page = GithubTokenService.cal_github_total_page(org_list.totalCount, PAGE_SIZE)
|
||||||
return Response(data, status=status.HTTP_200_OK)
|
orgs = org_list.get_page(page - 1)
|
||||||
|
data = [{'org_login': org.login} for org in orgs]
|
||||||
|
|
||||||
|
return Response({
|
||||||
|
"page": page,
|
||||||
|
"page_size": PAGE_SIZE,
|
||||||
|
"total_page": total_page,
|
||||||
|
"results": data
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
@action(detail=False, methods=["get"], url_path="organization-repos")
|
@action(detail=False, methods=["get"], url_path="organization-repos")
|
||||||
def organization_repos(self, request):
|
def organization_repos(self, request):
|
||||||
@@ -248,13 +267,21 @@ class GithubAPIViewSet(viewsets.ViewSet):
|
|||||||
return Response({"is_page_gte_1": False, "detail": "page and page_size must be >= 1"}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({"is_page_gte_1": False, "detail": "page and page_size must be >= 1"}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
org_login = request.query_params.get("org")
|
org_login = request.query_params.get("org")
|
||||||
repos = self.github.get_organization(org_login).get_repos().get_page(page-1)
|
|
||||||
|
|
||||||
data = [
|
if not org_login:
|
||||||
{'full_name': r.full_name}
|
return Response({"is_query_param": False, "detail": "org parameter is required"}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
for r in repos
|
|
||||||
]
|
repo_list = self.github.get_organization(org_login).get_repos()
|
||||||
return Response(data, status=status.HTTP_200_OK)
|
total_page = GithubTokenService.cal_github_total_page(repo_list.totalCount, PAGE_SIZE)
|
||||||
|
repos = repo_list.get_page(page - 1)
|
||||||
|
data = [{'full_name': r.full_name} for r in repos]
|
||||||
|
|
||||||
|
return Response({
|
||||||
|
"page": page,
|
||||||
|
"page_size": PAGE_SIZE,
|
||||||
|
"total_page": total_page,
|
||||||
|
"results": data
|
||||||
|
}, status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
class JoinAPIView(APIView):
|
class JoinAPIView(APIView):
|
||||||
|
|||||||
Reference in New Issue
Block a user