Added a setting where the exported excel files should be stored.
Added a option to send the exported excel as mail attachment.
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
"""Export Mahjong Rankings as excel files."""
|
||||
|
||||
import openpyxl
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.utils.dateparse import parse_date
|
||||
from openpyxl.styles import Border
|
||||
import os
|
||||
from datetime import date, time, datetime
|
||||
|
||||
import openpyxl
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.utils import timezone
|
||||
from django.utils.dateparse import parse_date
|
||||
from django.core.mail import EmailMessage
|
||||
|
||||
from mahjong_ranking.models import SeasonRanking, KyuDanRanking
|
||||
|
||||
THIN_BORDER = openpyxl.styles.Side(style='thin', color="d3d7cf")
|
||||
@@ -38,7 +42,19 @@ DATE_STYLE.font = DEFAULT_STYLE.font
|
||||
DATE_STYLE.border = DEFAULT_STYLE.border
|
||||
DATE_STYLE.number_format = 'dd.mm.yyyy'
|
||||
|
||||
MAIL_BODY = """
|
||||
Hallo! Ich bin's dein Server.
|
||||
|
||||
Ich habe gerade die Mahjong Rankings als Excel exportiert und dachte mir das
|
||||
ich sie dir am besten gleich schicke.
|
||||
|
||||
Bitte versuche nicht auf diese E-Mail zu antworten.
|
||||
Ich bin nur ein dummes Programm.
|
||||
|
||||
mit lieben Grüßen
|
||||
|
||||
Der Kasu Server
|
||||
"""
|
||||
def geneate_excel():
|
||||
"""Generate an excel .xlsx spreadsheet from json data of the kyu/dan
|
||||
rankings.
|
||||
@@ -108,7 +124,7 @@ def export_season_rankings(workbook, until):
|
||||
'style': 'content',
|
||||
'width': 25},
|
||||
{'col': 'C', 'title': '⌀ Platz', 'attr': 'avg_placement',
|
||||
'style': 'int', 'width': 8},
|
||||
'style': 'float', 'width': 8},
|
||||
{'col': 'D', 'title': '⌀ Punkte', 'attr': 'avg_score',
|
||||
'style': 'float', 'width': 12},
|
||||
{'col': 'E', 'title': 'Hanchans', 'attr': 'hanchan_count',
|
||||
@@ -156,17 +172,43 @@ def export_kyu_dan_rankings(workbook, until):
|
||||
|
||||
class Command(BaseCommand):
|
||||
"""Exports the SeasonRankings"""
|
||||
filename = str()
|
||||
until = datetime
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('--until', nargs='?', type=parse_date)
|
||||
parser.add_argument(
|
||||
'--until', nargs='?', type=parse_date,
|
||||
default=date.today(), metavar='YYYY-MM-DD',
|
||||
help='Calculate and export rankings until the given date.')
|
||||
parser.add_argument(
|
||||
'--mail', nargs='*', type=str, metavar='user@example.com',
|
||||
help='Send the spreadsheet via eMail to the given recipient.')
|
||||
|
||||
def handle(self, *args, **options):
|
||||
"""Exports the current ladder ranking in a spreadsheet.
|
||||
This is useful as a backup in form of a hardcopy."""
|
||||
until = timezone.make_aware(
|
||||
datetime.combine(options['until'] or date.today(),
|
||||
time(23, 59, 59)))
|
||||
self.until = timezone.make_aware(datetime.combine(
|
||||
options['until'], time(23, 59, 59)
|
||||
))
|
||||
|
||||
self.filename = os.path.join(
|
||||
settings.RANKING_EXPORT_PATH,
|
||||
'mahjong_rankings_{:%Y-%m-%d}.xlsx'.format(self.until)
|
||||
)
|
||||
workbook = geneate_excel()
|
||||
export_season_rankings(workbook, until=until)
|
||||
export_kyu_dan_rankings(workbook, until=until)
|
||||
workbook.save('mahjong_rankings_{:%Y-%m-%d}.xlsx'.format(until))
|
||||
export_season_rankings(workbook, until=self.until)
|
||||
export_kyu_dan_rankings(workbook, until=self.until)
|
||||
os.makedirs(settings.RANKING_EXPORT_PATH, exist_ok=True)
|
||||
workbook.save(self.filename)
|
||||
if options['mail']:
|
||||
self.send_mail(options['mail'])
|
||||
|
||||
def send_mail(self, recipients):
|
||||
mail = EmailMessage(
|
||||
subject='Mahjong Rankings vom {:%d.%m.%Y}'.format(self.until),
|
||||
body=MAIL_BODY,
|
||||
from_email=settings.DEFAULT_FROM_EMAIL,
|
||||
to=recipients)
|
||||
mail.attach_file(self.filename)
|
||||
mail.send()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user