Skip to content

Commit

Permalink
Adicionado filtro de usuarios, mais seguidos e que mais seguem
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaiva31 committed Mar 4, 2023
1 parent 0bec29f commit dd1bb7a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
9 changes: 9 additions & 0 deletions apps/core/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@
(TWEEK_FILTER_LIKES, ('Likes')),
(TWEEK_FILTER_SHARES, ('Shares')),
)

USER_FILTER_ALPHABETICAL = '0'
USER_FILTER_FOLLOWERS = '1'
USER_FILTER_FOLLOWING = '2'
USER_FILTER_TYPE = (
(USER_FILTER_ALPHABETICAL, ('Alphabetical')),
(USER_FILTER_FOLLOWERS, ('Followers')),
(USER_FILTER_FOLLOWING, ('Following')),
)
28 changes: 19 additions & 9 deletions apps/core/views.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import os
from rest_framework import generics
from django.views.decorators.csrf import csrf_exempt
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import action
import requests
import json
from django.contrib.auth.models import User

from apps.core.models import Image
from django.db.models import Q

from apps.core import defaults
from apps.twikkerprofile.models import TwikkerProfile
from apps.twikkerprofile.serializers import TwikkerProfileSerializer
from django.db.models import Q
from .serializers import UserSerializer
from django.conf import settings
from django.contrib.auth import logout
from django.db.models import Count
from django.http import JsonResponse
from cloudinary import uploader
import cloudinary
from django.conf import settings

from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.decorators import action
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.authentication import TokenAuthentication
Expand All @@ -42,11 +43,20 @@ class UsersList(generics.ListAPIView):
serializer_class = UserSerializer

def get_queryset(self):
queryset = User.objects.all()
queryset = User.objects.annotate(
num_following=Count('twikkerprofile__follows'),
num_followers=Count('twikkerprofile__followed_by')
)
if self.request.GET.get('query', False):
queryset = queryset.filter(username__icontains=self.request.GET.get('query', False))
if self.request.GET.get('query', False):
queryset = queryset.filter(username__icontains=self.request.GET.get('query', False))
if self.request.GET.get('order_by') == defaults.USER_FILTER_FOLLOWERS:
queryset = queryset.order_by('-num_followers')
elif self.request.GET.get('order_by') == defaults.USER_FILTER_FOLLOWING:
queryset = queryset.order_by('-num_following')
return queryset

class FollowersList(generics.ListAPIView):
serializer_class = TwikkerProfileSerializer

Expand Down Expand Up @@ -81,4 +91,4 @@ class VerifyTokenView(APIView):

def get(self, request):
token = Token.objects.get(key=request.auth.key)
return Response({'detail': 'Token is valid'})
return Response({'detail': 'Token is valid'})
10 changes: 9 additions & 1 deletion frontend/src/pages/Users.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
v-model="searchQuery" placeholder="Search" />
<button><i class="p-1 fa-solid fa-magnifying-glass"></i></button>
</div>
<div class="mb-2">
<select v-model="selectedOption" @change="getUsers()">
<option value="0">Ordem alfabética</option>
<option value="1">Mais Seguidos</option>
<option value="2">Mais Seguindo</option>
</select>
</div>
</form>
</div>
<router-link :to="`/profile/${user.username}`" v-for="user in users"
Expand Down Expand Up @@ -41,10 +48,11 @@ import DefaultHeader from "../components/DefaultHeader.vue";
const users = ref([]);
const isLoading = ref(true);
const searchQuery = ref('');
const selectedOption = ref(0);
async function getUsers() {
isLoading.value = true;
await axios
.get(`/api/users/?query=${searchQuery.value}`)
.get(`/api/users/?query=${searchQuery.value}&order_by=${selectedOption.value}`)
.then((response) => {
users.value = response.data;
})
Expand Down

0 comments on commit dd1bb7a

Please sign in to comment.