def show_redbull_history_csv(message):
"""RedBull??????????CSV?????
:param message: slackbot?????????????class
"""
s = Session()
consume_hisotry = (s.query(RedbullHistory)
.filter(RedbullHistory.delta < 0)
.order_by(RedbullHistory.id.desc()))
# func.month??????????groupby count?????
# SQLite??MONTH()???????????Python?????
def grouper(item):
return item.ctime.year, item.ctime.month
ret = []
for ((year, month), items) in groupby(consume_hisotry, grouper):
count = -sum(item.delta for item in items)
ret.append(['{}/{}'.format(year, month), str(count)])
output = StringIO()
w = csv.writer(output)
w.writerows(ret)
param = {
'token': settings.API_TOKEN,
'channels': message.body['channel'],
'title': 'RedBull History Check'
}
requests.post(settings.FILE_UPLOAD_URL,
params=param,
files={'file': output.getvalue()})
评论列表
文章目录