Skip to content

Commit

Permalink
Updated Ladder Entries to better indicate rank. (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kraust authored Jun 15, 2024
1 parent 29ff17b commit 92bf6e4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 0 additions & 1 deletion ladder/filters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .ladder import *
from .ladder_entry import *
from .variant import *

18 changes: 16 additions & 2 deletions ladder/serializers/ladder_entry.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
""" LadderEntry Serializers """

import logging

from rest_framework import serializers

from ladder.models import LadderEntry

LOGGER = logging.getLogger("django")


class LadderEntrySerializer(serializers.ModelSerializer):
"""LadderEntry Serializer"""
Expand All @@ -17,9 +21,19 @@ def get_date(self, obj):

def get_rank(self, obj) -> int:
"""Return the rank of the ladder entry with respect to the ladder"""
key = f"data__{obj.ladder.metric}__gte"
key = f"data__{obj.ladder.metric}__gt"
flt = {key: obj.data.get(obj.ladder.metric, 0)}
return LadderEntry.objects.filter(ladder=obj.ladder).filter(**flt).count()
query = {}
for k, v in self.context["request"].GET.items():
if k not in ["ordering", "page", "page_size"] and v:
query[k] = v
return (
LadderEntry.objects.filter(**query)
.filter(visible=True)
.filter(**flt)
.count()
+ 1
)

class Meta:
model = LadderEntry
Expand Down
2 changes: 1 addition & 1 deletion ladder/templates/ladder_entry.html
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
<tbody>
{% for entry in ladderentry_list %}
<tr scope="row">
<td>{{entry|rank}}</td>
<td>{% rank entry request %}</td>
<td>{{entry.combatlog.metadata.date_time|date:"DATE_FORMAT"}}</td>
<td>{{entry.player}}</td>
<td>{{entry.ladder.variant.name}}</td>
Expand Down
19 changes: 15 additions & 4 deletions ladder/templatetags/ladder_entry.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
"""Custom Template Tags for Ladder Entries"""

import logging

from django import template

from ladder.models import Ladder, LadderEntry

register = template.Library()

LOGGER = logging.getLogger("django")


@register.filter
def rank(obj):
@register.simple_tag
def rank(obj, request):
"""Return the ladder entry's rank"""
key = f"data__{obj.ladder.metric}__gte"
key = f"data__{obj.ladder.metric}__gt"
flt = {key: obj.data.get(obj.ladder.metric, 0)}
return LadderEntry.objects.filter(ladder=obj.ladder).filter(**flt).count()
query = {}
for k, v in request.GET.items():
if k not in ["ordering", "page", "page_size"] and v:
query[k] = v
return (
LadderEntry.objects.filter(**query).filter(visible=True).filter(**flt).count()
+ 1
)


@register.simple_tag
Expand Down

0 comments on commit 92bf6e4

Please sign in to comment.