Files @ 60636de1c5f5
Branch filter:

Location: FVDE/ennstatus/ennstatus/donate/views.py

Dennis Fink
Fix total message in countries subcommand
from flask import (Blueprint, render_template, request,
                   redirect, url_for, current_app)

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)

    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)
        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)

        current_app.logger.info('Return result')
        return render_template('donate/received.html',
                               form=form, csv_file=csv_file,
                               year=year, month=month)