# Ënnstatus
# Copyright (C) 2015 Dennis Fink
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from flask import (Blueprint, render_template, request,
redirect, url_for, current_app)
from babel.numbers import parse_decimal, format_decimal
from ennstatus.donate.forms import DateForm
from ennstatus.donate.functions import load_csv, get_choices
donate_page = Blueprint('donate', __name__)
@donate_page.route('/')
def index():
return render_template('donate/index.html')
@donate_page.route('/received',
methods=('GET', 'POST'))
def received():
current_app.logger.info('Handling received')
form = DateForm()
current_app.logger.debug('Creating choices')
files = [name for name in get_choices()]
files.sort()
year_choices = list({name.split('-')[0] for name in files})
year_choices.sort()
form.year.choices = [(name, name) for name in year_choices]
if not year_choices:
current_app.logger.warn('No donations found!')
return render_template('donate/received.html',
form=form, csv_file=None,
year=None, month=None, total=None)
if request.method == 'POST':
current_app.logger.debug('Validating form')
if form.validate_on_submit():
year = form.year.data
month = form.month.data
return redirect(url_for('donate.received', year=year, month=month))
else:
if 'year' in request.args and 'month' in request.args:
year = request.args['year']
month = request.args['month']
form.year.data = year
form.month.data = '{:02d}'.format(int(month))
filename = '-'.join([year, month])
if filename in files:
current_app.logger.info('Showing date %s' % filename)
csv_file = load_csv(filename)
else:
current_app.logger.warn('Date %s not found' % filename)
return render_template('donate/received.html',
form=form, csv_file=None,
year=year, month=month, total=None)
else:
filename = files[-1]
current_app.logger.info('Showing last date %s' % filename)
year, month = filename.split('-')
form.year.data = year
form.month.data = '{:02d}'.format(int(month))
csv_file = load_csv(filename)
total = format_decimal(
sum(
parse_decimal(row[2], locale='de') for row in csv_file
),
locale='de'
)
csv_file = load_csv(filename)
current_app.logger.info('Return result')
return render_template('donate/received.html',
form=form, csv_file=csv_file,
year=year, month=month, total=total)