Files
kasu/src/mahjong_ranking/managers.py

160 lines
6.5 KiB
Python

"""ObjectManagers for the Django Models used in the Mahjong-Ranking."""
from datetime import date
from django.db import models
class HanchanManager(models.Manager):
"""
The ObjectManager for models.Hanchan QuerySets.
It adds many specific filters that makes many queries much easier.
"""
use_for_related_fields = True
def confirmed_hanchans(self, user=None, **filter_args):
""" Return all valid and confirmed Hanchans.
:param user: Only return Hanchans where this user participated.
:param filter_args: To add specific arguments to the Django filter.
:return: QuerySet Object
"""
if user:
return self.user_hanchans(user, confirmed=True, **filter_args)
else:
return self.filter(confirmed=True, **filter_args)
def dan_hanchans(self, user, **filter_args):
""" Return all Hanchans where a specific user has participated and had
gain dan points and make his gamestats availabale.
:param user: Only return Hanchans where this user participated.
:param filter_args: To add specific arguments to the Django filter.
:return: QuerySet Object
"""
queryset = self.filter(
models.Q(player1=user, player1_dan_points__isnull=False) |
models.Q(player2=user, player2_dan_points__isnull=False) |
models.Q(player3=user, player3_dan_points__isnull=False) |
models.Q(player4=user, player4_dan_points__isnull=False)
).filter(confirmed=True, **filter_args)
queryset = queryset.select_related().order_by('-start')
[ hanchan.get_playerdata(user) for hanchan in queryset ]
return queryset
def kyu_hanchans(self, user, **filter_args):
""" Return all Hanchans where a specific user has participated and had
gain kyū points and make his gamestats availabale.
:param user: Only return Hanchans where this user participated.
:param filter_args: To add specific arguments to the Django filter.
:return: QuerySet Object
"""
queryset = self.filter(
models.Q(player1=user, player1_kyu_points__isnull=False) |
models.Q(player2=user, player2_kyu_points__isnull=False) |
models.Q(player3=user, player3_kyu_points__isnull=False) |
models.Q(player4=user, player4_kyu_points__isnull=False)
).filter(confirmed=True, **filter_args)
queryset = queryset.select_related().order_by('-start')
[ hanchan.get_playerdata(user) for hanchan in queryset ]
return queryset
def season_hanchans(self, user=None, season=None):
"""Return all Hanchans that belong to a given or the current season.
:param user: Only return Hanchans where this user participated.
:param season: the year of the wanted season, current year if None.
:return: QuerySet Object
"""
season = season or date.today().year
return self.confirmed_hanchans(user=user, season=season)
def user_hanchans(self, user, since=None, **filter_args):
"""Return all Hanchans where a specific user has participated.
:param user: Return Hanchans where this user participated.
:param since: optional a date value since when you want to hanchans
:param filter_args: To add specific arguments to the Django filter.
:return: a QuerySet Object
"""
queryset = self.filter(
models.Q(player1=user) | models.Q(player2=user) |
models.Q(player3=user) | models.Q(player4=user)
)
if since:
queryset = queryset.filter(start__gte=since, **filter_args)
else:
queryset = queryset.filter(**filter_args)
queryset = queryset.select_related().order_by('-start')
[ hanchan.get_playerdata(user) for hanchan in queryset ]
return queryset
def unconfirmed_hanchans(self, user=None, **filter_args):
""" Return all Hanchans that have been set to unconfirmed.
:param user: Only return Hanchans where this user participated.
:param filter_args: To add specific arguments to the Django filter.
:return: QuerySet Object
"""
if user:
return self.user_hanchans(user, confirmed=False, **filter_args)
else:
return self.filter(confirmed=False, **filter_args)
class SeasonRankingManager(models.Manager):
"""
The ObjectManager for models.SeasonRanking QuerySets to handle the
Ladderrankings.
It adds many specific filters that makes many queries much easier.
"""
def current_rankings(self):
""" Returns the Rankings for the current year/season.
:return: models.QuerySet
"""
current_season = date.today().year
return self.filter(season=current_season)
@property
def season_list(self):
""" Return a list of all availables years/seasons.
It's useful for select boxes and choice fields.
:return: list()
"""
values_list = self.model.objects.values_list('season', flat=True)
return values_list.order_by('season').distinct()
def json_data(self, season=None):
""" Get all Rankings for a given Season and return them as a list of
dict objects, suitable for JSON exports and other processings.
:param season: Season that should be exported, current season if empty
:return: a list() of dict() objects suiteable for JSON export.
"""
season = season or date.today().year
json_data = list()
values = self.filter(season=season, placement__isnull=False)
values = values.values('placement', 'user_id', 'user__username',
'user__first_name', 'user__last_name',
'avg_placement', 'avg_score',
'hanchan_count', 'good_hanchans', 'won_hanchans')
for user in values:
json_data.append({
'placement': user['placement'],
'user_id': user['user_id'],
'username': user['user__username'],
'first_name': user['user__first_name'],
'last_name': user['user__last_name'],
'avg_placement': user['avg_placement'],
'avg_score': user['avg_score'],
'hanchan_count': user['hanchan_count'],
'good_hanchans': user['good_hanchans'],
'won_hanchans': user['won_hanchans']
})
return json_data