export_ranking now exports KyuDanRankings and SeasonRankings.
This commit is contained in:
@@ -1,36 +1,148 @@
|
||||
"""Export Mahjong Rankings as excel files."""
|
||||
|
||||
from datetime import date
|
||||
from operator import itemgetter
|
||||
|
||||
import openpyxl
|
||||
from django.core.management.base import BaseCommand
|
||||
from openpyxl import Workbook
|
||||
from openpyxl.styles import Border
|
||||
|
||||
from mahjong_ranking.models import SeasonRanking
|
||||
from mahjong_ranking.models import SeasonRanking, KyuDanRanking
|
||||
|
||||
THIN_BORDER = openpyxl.styles.Side(style='thin', color="d3d7cf")
|
||||
|
||||
HEADING_STYLE = openpyxl.styles.NamedStyle(name="heading")
|
||||
HEADING_STYLE.font = openpyxl.styles.Font(name='Philosopher', size=11,
|
||||
bold=True, color='ffffff')
|
||||
HEADING_STYLE.fill = openpyxl.styles.PatternFill(fill_type='solid',
|
||||
start_color='a40000',
|
||||
end_color='a40000')
|
||||
|
||||
DEFAULT_STYLE = openpyxl.styles.NamedStyle(name='content')
|
||||
DEFAULT_STYLE.font = openpyxl.styles.Font(name='Philosopher', size=10,
|
||||
bold=False, color='000000')
|
||||
DEFAULT_STYLE.border = openpyxl.styles.Border(bottom=THIN_BORDER,
|
||||
top=THIN_BORDER)
|
||||
|
||||
INT_STYLE = openpyxl.styles.NamedStyle(name='int')
|
||||
INT_STYLE.font = DEFAULT_STYLE.font
|
||||
INT_STYLE.border = DEFAULT_STYLE.border
|
||||
INT_STYLE.number_format = '#,##0'
|
||||
|
||||
FLOAT_STYLE = openpyxl.styles.NamedStyle(name='float')
|
||||
FLOAT_STYLE.font = DEFAULT_STYLE.font
|
||||
FLOAT_STYLE.border = DEFAULT_STYLE.border
|
||||
FLOAT_STYLE.number_format = '#,##0.00'
|
||||
|
||||
|
||||
def geneate_seasonexcel(json_data):
|
||||
def geneate_excel():
|
||||
"""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
|
||||
workbook = openpyxl.Workbook()
|
||||
workbook.add_named_style(HEADING_STYLE)
|
||||
workbook.add_named_style(DEFAULT_STYLE)
|
||||
workbook.add_named_style(INT_STYLE)
|
||||
workbook.add_named_style(FLOAT_STYLE)
|
||||
for sheet in workbook.worksheets:
|
||||
print(sheet)
|
||||
workbook.remove(sheet)
|
||||
return workbook
|
||||
|
||||
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")
|
||||
def generate_sheet(workbook, title, columns_settings, json_data):
|
||||
row = 1
|
||||
ws = workbook.create_sheet()
|
||||
ws.title = title
|
||||
|
||||
# setup print orientation
|
||||
ws.page_setup.orientation = ws.ORIENTATION_PORTRAIT
|
||||
ws.page_setup.paperSize = ws.PAPERSIZE_A4
|
||||
ws.page_setup.fitToWidth = True
|
||||
ws.print_options.horizontalCentered = True
|
||||
|
||||
# setup page header
|
||||
ws.oddHeader.left.text = title
|
||||
ws.oddHeader.left.size = 14
|
||||
ws.oddHeader.left.font = "Amerika Sans"
|
||||
ws.oddHeader.left.color = "000000"
|
||||
|
||||
ws.oddHeader.right.text = str(date.today())
|
||||
ws.oddHeader.right.size = 14
|
||||
ws.oddHeader.right.font = "Amerika Sans"
|
||||
ws.oddHeader.right.color = "000000"
|
||||
|
||||
# write table header
|
||||
for column, data in enumerate(columns_settings, 1):
|
||||
cell = ws.cell(column=column, row=row, value=data['title'])
|
||||
cell.style = 'heading'
|
||||
|
||||
# write the table content
|
||||
for line in json_data:
|
||||
row += 1
|
||||
for column, settings in enumerate(columns_settings, 1):
|
||||
cell = ws.cell(column=column, row=row, value=line[settings['attr']])
|
||||
cell.style = settings['style']
|
||||
|
||||
# set column widths
|
||||
for settings in columns_settings:
|
||||
ws.column_dimensions[settings['col']].width = settings['width']
|
||||
|
||||
|
||||
def export_season_rankings(workbook):
|
||||
json_data = sorted(SeasonRanking.objects.json_data(),
|
||||
key=itemgetter('placement'))
|
||||
title = "Mahjong Ladder - {}".format(date.today().year)
|
||||
columns_settings = (
|
||||
{'col': 'A', 'title': 'Rang', 'attr': 'placement', 'style': 'int',
|
||||
'width': 8},
|
||||
{'col': 'B', 'title': 'Spitzname', 'attr': 'username',
|
||||
'style': 'content',
|
||||
'width': 25},
|
||||
{'col': 'C', 'title': '⌀ Platz', 'attr': 'avg_placement',
|
||||
'style': 'int', 'width': 8},
|
||||
{'col': 'D', 'title': '⌀ Punkte', 'attr': 'avg_score',
|
||||
'style': 'float', 'width': 12},
|
||||
{'col': 'E', 'title': 'Hanchans', 'attr': 'hanchan_count',
|
||||
'style': 'int', 'width': 10},
|
||||
{'col': 'F', 'title': 'Gut', 'attr': 'good_hanchans',
|
||||
'style': 'int', 'width': 5},
|
||||
{'col': 'G', 'title': 'Gewonnen', 'attr': 'won_hanchans',
|
||||
'style': 'int', 'width': 10},
|
||||
)
|
||||
|
||||
generate_sheet(
|
||||
workbook=workbook,
|
||||
title=title,
|
||||
columns_settings=columns_settings,
|
||||
json_data=json_data)
|
||||
|
||||
|
||||
def export_kyu_dan_rankings(workbook):
|
||||
json_data = KyuDanRanking.objects.json_data()
|
||||
title = "Kyū & Dan Rankings"
|
||||
columns_settings = (
|
||||
{'col': 'A', 'title': 'Spitzname', 'attr': 'username',
|
||||
'style': 'content', 'width': 14},
|
||||
{'col': 'B', 'title': 'Voller Name', 'attr': 'full_name',
|
||||
'style': 'content', 'width': 20},
|
||||
{'col': 'C', 'title': 'Rang', 'attr': 'rank',
|
||||
'style': 'content', 'width': 8},
|
||||
{'col': 'D', 'title': 'Punkte', 'attr': 'points',
|
||||
'style': 'int', 'width': 8},
|
||||
{'col': 'E', 'title': 'Hanchans', 'attr': 'hanchan_count',
|
||||
'style': 'int', 'width': 10},
|
||||
{'col': 'F', 'title': 'Gut', 'attr': 'good_hanchans',
|
||||
'style': 'int', 'width': 5},
|
||||
{'col': 'G', 'title': 'Gewonnen', 'attr': 'won_hanchans',
|
||||
'style': 'int', 'width': 10},
|
||||
)
|
||||
generate_sheet(
|
||||
workbook=workbook,
|
||||
title=title,
|
||||
columns_settings=columns_settings,
|
||||
json_data=json_data)
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
@@ -39,4 +151,8 @@ class Command(BaseCommand):
|
||||
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())
|
||||
workbook = geneate_excel()
|
||||
export_season_rankings(workbook)
|
||||
export_kyu_dan_rankings(workbook)
|
||||
workbook.save('sample.x')
|
||||
workbook.save('mahjong_rankings_{}.xlsx'.format(str(date.today())))
|
||||
|
||||
Reference in New Issue
Block a user