Changeset - b91fc3493271
[Not reviewed]
default
0 3 0
Dennis Fink - 10 years ago 2014-12-12 14:56:34
dennis.fink@c3l.lu
Use diffent selectfields for year and month in donate.received
3 files changed with 50 insertions and 25 deletions:
0 comments (0 inline, 0 general)
ennstatus/donate/forms.py
Show inline comments
 
@@ -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()])
ennstatus/donate/views.py
Show inline comments
 
@@ -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)
ennstatus/templates/donate/received.html
Show inline comments
 
@@ -7,13 +7,20 @@
 
  <div class="col-md-12">
 
    <h2>Received Donations</h2>
 
    <div class="pull-left">
 
      <h3>{{ date }}</h3>
 
      {% if csv_file %}
 
        <h3>{{ '-'.join([year, month]) }}</h3>
 
      {% else %}
 
        <h3>Error!</h3>
 
      {% endif %}
 
    </div>
 
    <div class="pull-right">
 
      <form class="form-inline" role="form" method="POST" action="/donate/received">
 
        {{ form.hidden_tag() }}
 
        <div class="form-group">
 
          {{ form.date(_class="form-control") }}
 
          {{ form.year.label }}
 
          {{ form.year(_class="form-control") }}
 
          {{ form.month.label }}
 
          {{ form.month(_class="form-control") }}
 
        </div>
 
        <input type="submit" class="btn btn-primary btn-sm" value="Submit">
 
      </form> 
 
@@ -24,7 +31,11 @@
 
    {% if csv_file %}
 
      {{ macros.create_donations_table(csv_file) }}
 
    {% else %}
 
      <p>No donations found!</p>
 
      {% if year and month %}
 
        <p>No donations found for {{ '-'.join([year, month]) }}!</p>
 
      {% else %}
 
        <p>No donations found!</p>
 
      {% endif %}
 
    {% endif %}
 
  </div>
 
{% endblock %}
0 comments (0 inline, 0 general)