Changeset - 0ecebd0f7370
[Not reviewed]
dev
0 3 0
Dennis Fink - 9 years ago 2015-10-27 17:27:31
dennis.fink@c3l.lu
Added total to received donations
3 files changed with 12 insertions and 5 deletions:
0 comments (0 inline, 0 general)
ennstatus/donate/views.py
Show inline comments
 
@@ -51,47 +51,50 @@ def received():
 

	
 
    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)
 
                               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)
 
                                       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 = str(sum(int(row[2]) for row in csv_file))
 
        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)
 
                               year=year, month=month, total=total)
ennstatus/templates/donate/macros.html
Show inline comments
 
{% macro colorize_numbers(number) %}
 
  {% if number.startswith('-') %}
 
    {% set color = "text-danger" %}
 
  {% else %}
 
    {% if not number.startswith('+') %}
 
      {% set number = ''.join(['+', number]) %}
 
    {% endif %}
 
    {% set color = "text-success" %}
 
  {% endif %}
 
  <span class="{{ color }}">{{ number }}</span>
 
{% endmacro %}
 

	
 
{% macro create_donations_table(csv_file) %}
 
{% macro create_donations_table(csv_file, total) %}
 
  <div class="table-responsive">
 
    <table class="table table-bordered table-striped">
 
      <thead>
 
        <tr>
 
          <th>#</th>
 
          <th>Date</th>
 
          <th>Statement</th>
 
          <th>Amount</th>
 
        </tr>
 
      </thead>
 
      <tbody>
 
        {% for row in csv_file %}
 
          <tr>
 
            <td>{{ loop.index }}</td>
 
            <td>{{ row[0] }}</td>
 
            <td>{{ row[1] }}</td>
 
            <td>{{ colorize_numbers(row[2]) }}</td>
 
          </tr>
 
        {% endfor %}
 
        <tr>
 
          <td colspan="3" class="text-right"><b>Total:</b></td>
 
          <td>{{ colorize_numbers(total) }}</td>
 
        </tr>
 
      </tbody>
 
    </table>
 
  </div>
 
{% endmacro %}
ennstatus/templates/donate/received.html
Show inline comments
 
@@ -21,22 +21,22 @@
 
        <div class="form-group">
 
          {{ form.year.label }}
 
          {{ form.year(class_='form-control input-sm') }}
 
          {{ form.month.label }}
 
          {{ form.month(class_='form-control input-sm') }}
 
          {{ form.submit(class_='btn btn-enn btn-sm') }}
 
        </div>
 
      </form> 
 
    </div>
 
  </div>
 
  <div class="col-md-12">
 
    {% if csv_file %}
 
      {{ macros.create_donations_table(csv_file) }}
 
      {{ macros.create_donations_table(csv_file, total) }}
 
    {% else %}
 
      {% 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)