Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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: 2 additions & 0 deletions bugheist/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import comments.views
import website.views
from website.views import (
EachmonthLeaderboardView,
UserProfileDetailView,
IssueCreate,
UploadCreate,
Expand Down Expand Up @@ -279,6 +280,7 @@
re_path(r"^label_activity/$", SpecificIssuesView.as_view(), name="all_activity"),
re_path(r"^leaderboard/$", LeaderboardView.as_view(), name="leaderboard"),
re_path(r"^leaderboard/monthly/$", MonthlyLeaderboardView.as_view(), name="leaderboard_monthly"),
re_path(r"^leaderboard/each-month/$", EachmonthLeaderboardView.as_view(), name="leaderboard_eachmonth"),
re_path(r"^leaderboard/api/$", LeaderboardApiViewSet.as_view(), name="leaderboard_api"),
re_path(r"^api/v1/issue/like/(?P<id>\w+)/$", LikeIssueApiView.as_view(), name="like_issue"),
re_path(r"^api/v1/issue/flag/(?P<id>\w+)/$", FlagIssueApiView.as_view(), name="flag_issue"),
Expand Down
88 changes: 88 additions & 0 deletions website/templates/leaderboard_eachmonth.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{% extends "base.html" %}
{% load gravatar %}

{% block style %}
<style>
.list-group-item img {
width: 50px;
height: 50px;
}

.list-group-item .badge {
margin-top: 15px;
}

.list-group-item a {
text-decoration: none;
margin-left: 10px;
}

.silver {
background-color: lightblue;
color: #333;
}

.gold {
background-color: #D4AF37;
color: #333;
}

.bronze {
background-color: #C9AE5D;
color: #333;
}

.profileimage {
border-radius: 50%;
}

.titleuser {
position: absolute;
margin-top: 12px;
margin-left: 5px;
}
</style>
{% endblock %}

{% block content %}
<div class="row">
<div class="col-lg-6" style="display: flex; justify-content: center; align-items: center;">
<h1 class="page-header">Monthly Leaderboard</h1>
</div>
</div>
<div class="row">
<div class="col-lg-9">
<div class="list-group">
{%if not leaderboard%}
<p style="color: red;">No data for this month</p>
{% else %}
{% for month in leaderboard %}
<div class="list-group-item" style="margin-top: 30px;">
<button disabled class="btn btn-primary">{{month.month}}</button>
</div>
<div class="list-group-item">
{% if not month.user %}
<p style="color: red;">no bugs found this month</p>
{% else %}
{% if month.user.userprofile.avatar %}
<img src="{{ month.user.userprofile.avatar }}" class="img-responsive img-thumbnail profileimage">
{% elif month.user.socialaccount_set.all.0.get_avatar_url %}
<img src="{{ month.user.socialaccount_set.all.0.get_avatar_url }}" class="profileimage">
{% else %}
<img src="{% gravatar_url month.user.email 50 %}" class="profileimage">
{% endif %}
<a href="/profile/{{ month.user.username }}">{{ month.user.username }}</a>
<span class="pull-right badge">{{ month.user.total_score }} Points</span>
{% if month.user.userprofile.winnings %}
<span class="pull-right badge">${{ month.user.userprofile.winnings|default:""|floatformat }}</span>
{% endif %}

<span><kbd
class="{{ month.user.userprofile.get_title_display }} titleuser">{{ month.user.userprofile.get_title_display }}</kbd></span> {% endif %}
</div>
{% endfor %}
{%endif%}
</div>
</div>
</div>
{% endblock %}
53 changes: 43 additions & 10 deletions website/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,13 @@ def curr_month_leaderboard(self):
points__created__year=datetime.now().year
)
)



def month_leaderboard(self,month,year,api=True):
'''
return: list of dict of current/specified month users leaderboard
'''
data = (
User.objects
.annotate(total_score=Sum('points__score'))
Expand All @@ -1181,7 +1187,7 @@ def month_leaderboard(self,month,year,api=True):

return data

def monthly_leaderboard(self,year) -> list:
def monthly_leaderboard(self,year,api=False) -> list:

'''
return: list of dict of monthly winners ordered from 1-12 for queried year
Expand All @@ -1191,7 +1197,7 @@ def monthly_leaderboard(self,year) -> list:

# iterating over months 1-12
for month in range(1,13):
month_winner = self.month_leaderboard(month,year)
month_winner = self.month_leaderboard(month,year,api)
monthly_winner.append(month_winner)


Expand All @@ -1206,17 +1212,44 @@ def get_context_data(self, *args, **kwargs):
if self.request.user.is_authenticated:
context["wallet"] = Wallet.objects.get(user=self.request.user)

context["leaderboard"] = (
User.objects
.annotate(total_score=Sum('points__score'))
.order_by('-total_score')
.filter(
total_score__gt=0,
)
)
context["leaderboard"] = self.monthly_leaderboard()
return context


class EachmonthLeaderboardView(LeaderboardView,ListView):
model = User
template_name = "leaderboard_eachmonth.html"

def get_context_data(self, *args, **kwargs):

context = super(LeaderboardView, self).get_context_data(*args, **kwargs)

if self.request.user.is_authenticated:
context["wallet"] = Wallet.objects.get(user=self.request.user)

year = self.request.GET.get("year")

if not year: year = datetime.now().year

if isinstance(year,str) and not year.isdigit():
raise Http404(f"Invalid query passed | Year:{year}")

year = int(year)

leaderboard = self.monthly_leaderboard(year)
month_winners = []

months = ["January","February","March","April","May","June","July","August","September","October","Novermber","December"]

for indx,month in enumerate(leaderboard):

month = {"user":month.first(),"month":months[indx]}
month_winners.append(month)

context["leaderboard"] = month_winners

return context

class MonthlyLeaderboardView(LeaderboardView,ListView):
model = User
template_name = "leaderboard_monthly.html"
Expand Down