generated from Code-Institute-Org/gitpod-full-template
-
Notifications
You must be signed in to change notification settings - Fork 23
/
views.py
47 lines (43 loc) · 1.54 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from django.db.models import Count
from rest_framework import generics, filters
from django_filters.rest_framework import DjangoFilterBackend
from drf_api.permissions import IsOwnerOrReadOnly
from .models import Profile
from .serializers import ProfileSerializer
class ProfileList(generics.ListAPIView):
"""
List all profiles.
No create view as profile creation is handled by django signals.
"""
queryset = Profile.objects.annotate(
posts_count=Count('owner__post', distinct=True),
followers_count=Count('owner__followed', distinct=True),
following_count=Count('owner__following', distinct=True)
).order_by('-created_at')
serializer_class = ProfileSerializer
filter_backends = [
filters.OrderingFilter,
DjangoFilterBackend,
]
filterset_fields = [
'owner__following__followed__profile',
'owner__followed__owner__profile',
]
ordering_fields = [
'posts_count',
'followers_count',
'following_count',
'owner__following__created_at',
'owner__followed__created_at',
]
class ProfileDetail(generics.RetrieveUpdateAPIView):
"""
Retrieve or update a profile if you're the owner.
"""
permission_classes = [IsOwnerOrReadOnly]
queryset = Profile.objects.annotate(
posts_count=Count('owner__post', distinct=True),
followers_count=Count('owner__followed', distinct=True),
following_count=Count('owner__following', distinct=True)
).order_by('-created_at')
serializer_class = ProfileSerializer