Another Step in the Quest to clean up the code base.
This commit is contained in:
@@ -1,100 +1,150 @@
|
||||
__author__ = 'christian'
|
||||
|
||||
"""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, **kwargs):
|
||||
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, **kwargs)
|
||||
return self.user_hanchans(user, confirmed=True, **filter_args)
|
||||
else:
|
||||
return self.filter(confirmed=True, **kwargs)
|
||||
return self.filter(confirmed=True, **filter_args)
|
||||
|
||||
def dan_hanchans(self, user, **kwargs):
|
||||
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, **kwargs)
|
||||
).filter(confirmed=True, **filter_args)
|
||||
queryset = queryset.select_related().order_by('-start')
|
||||
for hanchan in queryset:
|
||||
hanchan.get_playerdata(user)
|
||||
[ hanchan.get_playerdata(user) for hanchan in queryset ]
|
||||
return queryset
|
||||
|
||||
def kyu_hanchans(self, user, **kwargs):
|
||||
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, **kwargs)
|
||||
).filter(confirmed=True, **filter_args)
|
||||
queryset = queryset.select_related().order_by('-start')
|
||||
for hanchan in queryset:
|
||||
hanchan.get_playerdata(user)
|
||||
[ 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, **kwargs):
|
||||
"""
|
||||
|
||||
:param user: User Object, or an player_id as integer
|
||||
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
|
||||
:return:
|
||||
: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)
|
||||
if kwargs:
|
||||
queryset = queryset.filter(**kwargs)
|
||||
queryset = queryset.filter(start__gte=since, **filter_args)
|
||||
else:
|
||||
queryset = queryset.filter(**filter_args)
|
||||
queryset = queryset.select_related().order_by('-start')
|
||||
for hanchan in queryset:
|
||||
hanchan.get_playerdata(user)
|
||||
[ hanchan.get_playerdata(user) for hanchan in queryset ]
|
||||
return queryset
|
||||
|
||||
def hanchan_stats(self, queryset=None):
|
||||
queryset = queryset or self.get_query_set()
|
||||
self.num_hanchans = queryset.count()
|
||||
self.won_hanchans = queryset.filter(placement=1).count()
|
||||
self.good_hanchans = queryset.filter(placement__lt=3).count()
|
||||
def unconfirmed_hanchans(self, user=None, **filter_args):
|
||||
""" Return all Hanchans that have been set to unconfirmed.
|
||||
|
||||
def unconfirmed_hanchans(self, user=None, **kwargs):
|
||||
: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, **kwargs)
|
||||
return self.user_hanchans(user, confirmed=False, **filter_args)
|
||||
else:
|
||||
return self.filter(confirmed=False, **kwargs)
|
||||
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 = {}
|
||||
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',
|
||||
'user__first_name', 'user__last_name',
|
||||
'avg_placement', 'avg_score',
|
||||
'hanchan_count', 'good_hanchans', 'won_hanchans')
|
||||
for user in values:
|
||||
json_data[user['user_id']] = {
|
||||
json_data.append({
|
||||
'placement': user['placement'],
|
||||
'user_id': user['user_id'],
|
||||
'username': user['user__username'],
|
||||
@@ -105,5 +155,5 @@ class SeasonRankingManager(models.Manager):
|
||||
'hanchan_count': user['hanchan_count'],
|
||||
'good_hanchans': user['good_hanchans'],
|
||||
'won_hanchans': user['won_hanchans']
|
||||
}
|
||||
})
|
||||
return json_data
|
||||
|
||||
Reference in New Issue
Block a user