def stars_top_employee_lists(request, top_number, kind, id):
"""
Returns stars top {top_number} list according to {kind} (category, keyword) {id} (kind_id)
---
serializer: stars.serializers.StarTopEmployeeLists
responseMessages:
- code: 401
message: Unauthorized. Authentication credentials were not provided. Invalid token.
- code: 403
message: Forbidden, authentication credentials were not provided
- code: 404
message: Not found
- code: 412
message: Precondition failed, kind should be category or subcategory
"""
try:
if request.method == 'GET':
if kind == 'category':
top_list = Star.objects.filter(category__id=id).values(
'to_user__pk',
'to_user__username',
'to_user__first_name',
'to_user__last_name',
'to_user__level'
'to_user__avatar').annotate(num_stars=Count('to_user')).order_by('-num_stars')[:top_number]
elif kind == 'keyword':
top_list = Star.objects.filter(keyword__id=id).values(
'to_user__pk',
'to_user__username',
'to_user__first_name',
'to_user__last_name',
'to_user__level',
'to_user__avatar').annotate(num_stars=Count('to_user')).order_by('-num_stars')[:top_number]
else:
return Response(status=status.HTTP_412_PRECONDITION_FAILED)
serializer = StarTopEmployeeLists(top_list, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
except Exception as e:
raise APIException(e)
评论列表
文章目录