diff --git a/.travis.yml b/.travis.yml index f267c21c8..5f07a75be 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,7 +17,7 @@ env: install: - pip install pipenv - - pipenv install + - pipenv install --skip-lock before_script: diff --git a/Pipfile b/Pipfile index 8b3e9bf57..974871cea 100644 --- a/Pipfile +++ b/Pipfile @@ -45,7 +45,7 @@ six = "*" tablib = "*" ua-parser = "*" djangorestframework = "*" - +django_filter = "*" [dev-packages] diff --git a/bugheist/settings.py b/bugheist/settings.py index 123293622..b079b3e3e 100644 --- a/bugheist/settings.py +++ b/bugheist/settings.py @@ -55,6 +55,7 @@ 'comments', 'annoying', 'rest_framework', + 'django_filters', ) MIDDLEWARE = ( @@ -328,5 +329,6 @@ REST_FRAMEWORK = { 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination', + 'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',), 'PAGE_SIZE': 50 } \ No newline at end of file diff --git a/bugheist/urls.py b/bugheist/urls.py index 1f6a19f7f..56a490848 100644 --- a/bugheist/urls.py +++ b/bugheist/urls.py @@ -14,7 +14,7 @@ from website.views import (UserProfileDetailView, IssueCreate, UploadCreate, EmailDetailView, InboundParseWebhookView, LeaderboardView, IssueView, AllIssuesView, SpecificIssuesView, HuntCreate, DomainDetailView, StatsDetailView, InviteCreate, CreateInviteFriend, - ScoreboardView) + ScoreboardView,get_score) favicon_view = RedirectView.as_view(url='/static/favicon.ico', permanent=True) @@ -73,6 +73,7 @@ url(r'^i18n/', include('django.conf.urls.i18n')), url(r'^domain_check/$', website.views.domain_check), url(r'^api/v1/', include(router.urls)), + url(r'^api/v1/userscore$', website.views.get_score), ] diff --git a/website/serializers.py b/website/serializers.py index 195a4dbb7..35a578ce8 100644 --- a/website/serializers.py +++ b/website/serializers.py @@ -1,16 +1,47 @@ -from website.models import Issue +from website.models import Issue, User , UserProfile,Points, Domain from rest_framework import routers, serializers, viewsets, filters +import django_filters + + +class UserSerializer(serializers.ModelSerializer): + class Meta: + model = User + fields = ('id','username','email') class IssueSerializer(serializers.ModelSerializer): + user = UserSerializer(read_only=True) class Meta: model = Issue - fields = "__all__" + fields = '__all__' class IssueViewSet(viewsets.ModelViewSet): - serializer_class = IssueSerializer - model = Issue queryset = Issue.objects.all() + serializer_class = IssueSerializer + filter_backends = (filters.SearchFilter,) + search_fields = ('url', 'description') + +class UserProfileSerializer(serializers.ModelSerializer): + user = UserSerializer(read_only=True) + class Meta: + model = UserProfile + fields = '__all__' + +class UserProfileViewSet(viewsets.ModelViewSet): + serializer_class = UserProfileSerializer + queryset = UserProfile.objects.all() + +class DomainSerializer(serializers.ModelSerializer): + class Meta: + model = Domain + fields = '__all__' +class DomainViewSet(viewsets.ModelViewSet): + serializer_class = DomainSerializer + queryset = Domain.objects.all() + filter_backends = (filters.SearchFilter,) + search_fields = ('url', 'name') router = routers.DefaultRouter() -router.register(r'issues', IssueViewSet, base_name="issues") \ No newline at end of file +router.register(r'issues', IssueViewSet, base_name="issues") +router.register(r'profile', UserProfileViewSet, base_name="profile") +router.register(r'domain', DomainViewSet, base_name="domain") \ No newline at end of file diff --git a/website/views.py b/website/views.py index a41ee7057..6d5ecce07 100644 --- a/website/views.py +++ b/website/views.py @@ -24,7 +24,7 @@ from django.core.mail import send_mail from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.urls import reverse, reverse_lazy - +from django.db import models from django.db.models import Sum, Count, Q from django.db.models.functions import ExtractMonth from django.dispatch import receiver @@ -1008,4 +1008,23 @@ def get_client_ip(request): ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR') - return ip \ No newline at end of file + return ip + +def get_score(request): + users = list() + temp_users = User.objects.annotate(total_score=Sum('points__score')).order_by( + '-total_score').filter(total_score__gt=0) + rank_user = 1; + for each in temp_users.all(): + temp = dict() + temp['rank'] = rank_user + temp['id'] = each.id + temp['User'] = each.username + temp['score'] = Points.objects.filter(user=each.id).aggregate(total_score=Sum('score')) + temp['image'] = list(UserProfile.objects.filter(user=each.id).values('user_avatar'))[0] + temp['title_type'] = list(UserProfile.objects.filter(user=each.id).values('title'))[0] + temp['follows'] = list(UserProfile.objects.filter(user=each.id).values('follows'))[0] + temp['savedissue'] = list(UserProfile.objects.filter(user=each.id).values('issue_saved'))[0] + rank_user = rank_user + 1 + users.append(temp) + return JsonResponse(users, safe=False)