Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added API to get userscores and added new serialisers. #621

Merged
merged 7 commits into from
Jun 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ env:

install:
- pip install pipenv
- pipenv install
- pipenv install --skip-lock


before_script:
Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ six = "*"
tablib = "*"
ua-parser = "*"
djangorestframework = "*"

django_filter = "*"

[dev-packages]

Expand Down
2 changes: 2 additions & 0 deletions bugheist/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'comments',
'annoying',
'rest_framework',
'django_filters',
)

MIDDLEWARE = (
Expand Down Expand Up @@ -328,5 +329,6 @@

REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend',),
'PAGE_SIZE': 50
}
3 changes: 2 additions & 1 deletion bugheist/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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),

]

Expand Down
41 changes: 36 additions & 5 deletions website/serializers.py
Original file line number Diff line number Diff line change
@@ -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")
router.register(r'issues', IssueViewSet, base_name="issues")
router.register(r'profile', UserProfileViewSet, base_name="profile")
router.register(r'domain', DomainViewSet, base_name="domain")
23 changes: 21 additions & 2 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1008,4 +1008,23 @@ def get_client_ip(request):
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip
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)