def withdrawals(): withdrawals = Withdrawal.select(Withdrawal, User) \ .where(Withdrawal.approved == False).order_by(Withdrawal.created_at).join(User) total_sum = Withdrawal.select(fn.COALESCE(fn.SUM(Withdrawal.amount), 0).alias('total_sum')) \ .where(Withdrawal.approved == False).execute() return render_template( 'withdrawals.html', total_sum=total_sum[0].total_sum, withdrawals=withdrawals, )
def statistics(): def get_chart_data_for_transactions(transactions, columns): statistics_data = {} for transaction in transactions: date = transaction.created_at.strftime("%d %B") if date not in statistics_data: statistics_data[date] = 0 statistics_data[date] += float(transaction.amount) chart_data = [columns] for day in statistics_data.keys(): chart_data.append([day, statistics_data[day]]) return chart_data now = datetime.datetime.now() month_ago = now - datetime.timedelta(days=30) withdrawals = Withdrawal.select() \ .where(Withdrawal.created_at < now) \ .where(Withdrawal.created_at > month_ago).order_by(Withdrawal.created_at) top_ups = TopUp.select() \ .where(TopUp.created_at < now) \ .where(TopUp.created_at > month_ago).order_by(TopUp.created_at) withdrawal_data = get_chart_data_for_transactions(withdrawals, ['Day', 'Withdrawals']) top_up_data = get_chart_data_for_transactions(top_ups, ['Day', 'TopUps']) registrations = User.select().where(User.created_at < now) \ .where(User.created_at > month_ago).order_by(User.created_at) registration_temp = {} for registration in registrations: date = registration.created_at.strftime("%d %B") if date not in registration_temp: registration_temp[date] = 0 registration_temp[date] += 1 registration_data = [['Day', 'Registrations']] for day in registration_temp.keys(): registration_data.append([day, registration_temp[day]]) return render_template('statistics.html', withdrawal_data=withdrawal_data, top_up_data=top_up_data, registration_data=registration_data)