diff --git a/ennstatus/donate/forms.py b/ennstatus/donate/forms.py --- a/ennstatus/donate/forms.py +++ b/ennstatus/donate/forms.py @@ -4,4 +4,7 @@ from wtforms.validators import DataRequi class DateForm(Form): - date = SelectField('date', validators=[DataRequired()]) + year = SelectField('Year', validators=[DataRequired()]) + month = SelectField('Month', + choices=[('{:02d}'.format(i), str(i)) for i in range(1, 13)], + validators=[DataRequired()]) diff --git a/ennstatus/donate/views.py b/ennstatus/donate/views.py --- a/ennstatus/donate/views.py +++ b/ennstatus/donate/views.py @@ -78,39 +78,50 @@ def received(): form = DateForm() current_app.logger.debug('Creating choices') - choices = [name for name in get_choices()] - choices.sort() - form.date.choices = [(name, name) for name in choices] + + files = [name for name in get_choices()] + files.sort() - if not choices: + 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, - date=None) + year=None, month=None) if request.method == 'POST': current_app.logger.debug('Validating form') if form.validate_on_submit(): - date = form.date.data - return redirect(url_for('donate.received', date=date)) + year = form.year.data + month = form.month.data + return redirect(url_for('donate.received', year=year, month=month)) else: - if 'date' in request.args: - date = request.args['date'] - if date in choices: - current_app.logger.info('Showing date %s' % date) - form.date.data = date - csv_file = load_csv(date) + 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' % date) - return ('Date %s not found!' % date, 500, - {'Content-Type': 'text/plain'}) + 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: - current_app.logger.info('Showing last date %s' % choices[-1]) - form.date.data = choices[-1] - csv_file = load_csv(choices[-1]) - date = choices[-1] + 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, - date=date) + year=year, month=month) diff --git a/ennstatus/templates/donate/received.html b/ennstatus/templates/donate/received.html --- a/ennstatus/templates/donate/received.html +++ b/ennstatus/templates/donate/received.html @@ -7,13 +7,20 @@
No donations found!
+ {% if year and month %} +No donations found for {{ '-'.join([year, month]) }}!
+ {% else %} +No donations found!
+ {% endif %} {% endif %}