Files
@ e74dadda6adc
Branch filter:
Location: FVDE/ennstatus/ennstatus/donate/views.py
e74dadda6adc
4.6 KiB
text/x-python
Begin empty template for donation statistics
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | # Ë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
from ennstatus.root.forms import BPMForm
from ennstatus.root.constants import BPM_ADDRESSES
donate_page = Blueprint('donate', __name__)
@donate_page.route('/', methods=('GET', 'POST'))
def index():
current_app.logger.info('Handling index')
form = BPMForm()
country_choices = [choice[0] for choice in form.country.choices]
if request.method == 'POST':
current_app.logger.debug('Validating form')
if form.validate_on_submit():
country = form.country.data
return redirect(url_for('donate.index', country=country))
else:
if 'country' in request.args:
country = request.args['country']
if country in country_choices:
current_app.logger.info('Showing country %s' % country)
else:
current_app.logger.warn('Country %s not found' % country)
country = 'luxembourg'
else:
current_app.logger.info('Using default country')
country = 'luxembourg'
form.country.data = country
address = BPM_ADDRESSES[country]
return render_template('donate/index.html', form=form, address=address)
@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)
@donate_page.route('/statistics')
def statistics():
return render_template('donate/statistics.html')
|