def get_notifications(request, employee_id):
"""
Get all notifications for employee id
---
response_serializer: activities.serializers.NotificationSerializer
responseMessages:
- code: 401
message: Unauthorized. Authentication credentials were not provided. Invalid token.
- code: 403
message: Forbidden.
- code: 404
message: Not found
- code: 500
message: Internal Server Error
"""
if request.method == 'GET':
employee = get_object_or_404(Employee, pk=employee_id)
activities = Activity.objects.annotate(
profile=F('to_user')).values('datetime',
'text',
'profile').filter(to_user=employee)
messages = Message.objects.annotate(
profile=F('from_user')).values('datetime',
'text',
'profile').filter(Q(to_user='all') |
Q(to_user=employee.location.name) |
Q(to_user=employee.username))
notifications = list(chain(activities, messages))
notifications = sorted(notifications, reverse=True)
paginator = PageNumberPagination()
results = paginator.paginate_queryset(notifications, request)
serializer = NotificationSerializer(results, many=True)
return paginator.get_paginated_response(serializer.data)
评论列表
文章目录