Skip to content

Commit

Permalink
✨ feat: 新增分页
Browse files Browse the repository at this point in the history
  • Loading branch information
121812 committed Jul 17, 2024
1 parent cb2b5aa commit b640dbd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
5 changes: 0 additions & 5 deletions SeeHarbergertaxsInDAW/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,6 @@

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 1
}

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = 'media/'
Expand Down
47 changes: 25 additions & 22 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
from django.shortcuts import get_object_or_404
from .verify import validate
import datetime
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import MultiPartParser, FormParser, FileUploadParser, JSONParser
from rest_framework.pagination import PageNumberPagination
from django.contrib.auth.models import User
from django.contrib.auth import login
from django.contrib.auth import logout

import uuid
from .models import Holder, Advertise, Image
from .models import Holder, Advertise
from .serializers import HolderSerializer, AdvertiseSerializer, AuditSerializer, AdvertiseListSerializer, ImageSerializer, UserSerializer

class MyPageNumberPagination(PageNumberPagination):
page_size = 2 # default page size
class StandardResultsSetPagination(PageNumberPagination):
page_size = 10 # default page size
page_size_query_param = 'size' # ?page=xx&size=??
max_page_size = 10 # max page size
max_page_size = 50 # max page size

# 用户相关视图集
class LoginViewSet(viewsets.ViewSet):
def create(self, request):
useraddr = request.data['useraddr']
Expand All @@ -32,7 +31,7 @@ def create(self, request):
try:
user = User.objects.get(username=useraddr)
except User.DoesNotExist:
# 如果用户不存在,可以在此处创建新用户
# 如果用户不存在,创建新用户
user = User.objects.create_user(username=useraddr, password='')
login(request, user)

Expand All @@ -41,7 +40,7 @@ def create(self, request):

class LogoutViewSet(viewsets.ViewSet):
def create(self, request):
useraddr = request.data['useraddr']
useraddr = str(request.user)
signature = request.data['signature']
message = request.data['message']
print(message)
Expand All @@ -58,13 +57,14 @@ def create(self, request):
serializer = UserSerializer(user)
return Response(serializer.data)



# 用户相关视图集
# 持有者相关视图集
class HolderViewSet(viewsets.ViewSet):
pagination_class = StandardResultsSetPagination
def list(self, request):
queryset = Holder.objects.all()
serializer = HolderSerializer(queryset, many=True)
paginator = self.pagination_class()
page = paginator.paginate_queryset(queryset, request)
serializer = HolderSerializer(page, many=True)
return Response(serializer.data)

def retrieve(self, request, pk=None):
Expand All @@ -83,13 +83,16 @@ def create(self, request):

# 广告相关视图集
class AdvertiseViewSet(viewsets.ViewSet):
pagination_class = MyPageNumberPagination
pagination_class = StandardResultsSetPagination

# 获取审核通过数据
def list(self, request):
queryset = Advertise.objects.filter(audstatus=0)
serializer = AdvertiseListSerializer(queryset, many=True, context={'request': request})
paginator = self.pagination_class()
page = paginator.paginate_queryset(queryset, request)
serializer = AdvertiseListSerializer(page, many=True, context={'request': request})
return Response(serializer.data)

# 筛选数据
def retrieve(self, request, pk=None):
queryset = Advertise.objects.all()
Expand Down Expand Up @@ -144,27 +147,27 @@ def partial_update(self, request, pk=None):
return Response('Signature Error', status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


# 审核相关视图集,审核通过在 AdvertiseViewSet.partial_update
class AuditViewSet(viewsets.ViewSet):
# 获取数据
pagination_class = StandardResultsSetPagination
def list(self, request):
# 审核地址则返回所有数据
if str(request.user) == "0xfF7ca7Fe8FdAF2a602191048E10A4b3B072aA1a0":
queryset = Advertise.objects.all()
serializer = AdvertiseListSerializer(queryset, many=True)
paginator = self.pagination_class()
page = paginator.paginate_queryset(queryset, request)
serializer = AdvertiseListSerializer(page, many=True)
return Response(serializer.data)
# 用户地址则返回用户数据
queryset = Advertise.objects.filter(useraddr=request.user)
serializer = AuditSerializer(queryset, many=True, context={'request': request}, )
paginator = self.pagination_class()
page = paginator.paginate_queryset(queryset, request)
serializer = AuditSerializer(page, many=True, context={'request': request}, )
print(request.user)
return Response(serializer.data)

# 图片上传
class ImageViewSet(viewsets.ViewSet):
parser_classes = (MultiPartParser, FormParser, JSONParser)
queryset = Image.objects.all()

def create(self, request):
serializer = ImageSerializer(data=request.data)
if serializer.is_valid():
Expand All @@ -175,4 +178,4 @@ def create(self, request):
serializer.save(image=imageFile)

return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

0 comments on commit b640dbd

Please sign in to comment.