Another Step in the Quest to clean up the code base.
This commit is contained in:
42
src/mahjong_ranking/management/commands/export_ranking.py
Normal file
42
src/mahjong_ranking/management/commands/export_ranking.py
Normal file
@@ -0,0 +1,42 @@
|
||||
"""Export Mahjong Rankings as excel files."""
|
||||
|
||||
from operator import itemgetter
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from openpyxl import Workbook
|
||||
|
||||
from mahjong_ranking.models import SeasonRanking
|
||||
|
||||
|
||||
def geneate_seasonexcel(json_data):
|
||||
"""Generate an excel .xlsx spreadsheet from json data of the kyu/dan
|
||||
rankings.
|
||||
|
||||
:param json_data: The ladder ranking as JSON export."""
|
||||
workbook = Workbook()
|
||||
worksheet = workbook.active
|
||||
|
||||
worksheet.append([
|
||||
'Rang', 'Spitzname',
|
||||
'⌀ Platz', '⌀ Punkte',
|
||||
'Hanchans', 'Gut', 'Gewonnen'
|
||||
])
|
||||
|
||||
json_data = sorted(json_data, key=itemgetter('placement'))
|
||||
for row in json_data:
|
||||
worksheet.append([
|
||||
row['placement'], row['username'],
|
||||
row['avg_placement'], row['avg_score'],
|
||||
row['hanchan_count'],
|
||||
row['good_hanchans'], row['won_hanchans']
|
||||
])
|
||||
workbook.save("sample.xlsx")
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""Exports the SeasonRankings"""
|
||||
|
||||
def handle(self, *args, **options):
|
||||
"""Exports the current ladder ranking in a spreadsheet.
|
||||
This is useful as a backup in form of a hardcopy."""
|
||||
geneate_seasonexcel(SeasonRanking.objects.json_data())
|
||||
Reference in New Issue
Block a user