Changeset - 10fcdc48c762
[Not reviewed]
Dennis Fink - 9 years ago 2016-02-27 12:38:02
dennis.fink@c3l.lu
WIP
6 files changed with 8 insertions and 238 deletions:
0 comments (0 inline, 0 general)
ennstatus/donate/functions.py
Show inline comments
 
# Ë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/>.
 

	
 
import os
 
import os.path
 
import csv
 
import statistics
 

	
 
from collections import defaultdict
 

	
 
from babel.numbers import parse_decimal
 

	
 

	
 
def load_csv_file(file):
 

	
 
    with open(file, encoding='utf-8', newline='') as csvfile:
 
        csvreader = csv.reader(csvfile, delimiter=',')
 
        rows = list(csvreader)
 
    return rows
 

	
 

	
 
def load_csv(date):
 

	
 
    filename = '.'.join([date, 'csv'])
 
    path = os.path.join('donations', filename)
 

	
 
    for row in load_csv_file(path):
 
    with open(path, encoding='utf-8', newline='') as csvfile:
 
        csvreader = csv.reader(csvfile, delimiter=',')
 

	
 
        for row in csvreader:
 
        yield row
 

	
 

	
 
def get_choices():
 

	
 
    files = os.listdir('donations')
 

	
 
    for file in files:
 
        if not file.startswith('.') \
 
           and file.endswith('.csv'):
 
            yield os.path.splitext(file)[0]
 

	
 

	
 
def get_all_csv():
 

	
 
    for file in os.listdir('donations'):
 
        path = os.path.join('donations', file)
 
        yield load_csv_file(path)
 

	
 

	
 
def get_all_rows():
 

	
 
    for csvfile in get_all_csv():
 
        for row in csvfile:
 
            yield row
 

	
 

	
 
def all_csv_to_dict():
 

	
 
    data = defaultdict(set)
 

	
 
    for row in get_all_rows():
 
        data[row[0]].add(parse_decimal(row[2], locale='de'))
 
    return data
 

	
 

	
 
def data_to_year(data):
 

	
 
    year_data = defaultdict(set)
 

	
 
    for date, values in data.items():
 
        year = date.split('-')[0]
 
        for value in values:
 
            year_data[year].add(value)
 

	
 
    return year_data
 

	
 

	
 
def data_to_month(data):
 

	
 
    month_data = defaultdict(set)
 

	
 
    for date, values in data.items():
 
        month = date.rsplit('-', maxsplit=1)[0]
 
        for value in values:
 
            month_data[month].add(value)
 

	
 
    return month_data
 

	
 

	
 
def year_sum(data):
 

	
 
    year_data = data_to_year(data)
 

	
 
    for year, values in year_data.items():
 
        year_data[year] = sum(values)
 

	
 
    return year_data
 

	
 

	
 
def month_sum(data):
 

	
 
    month_data = data_to_month(data)
 

	
 
    for month, values in month_data.items():
 
        month_data[month] = sum(values)
 

	
 
    return month_data
 

	
 

	
 
def get_all_years_mean(data):
 

	
 
    mean = set()
 
    year_data = year_sum(data)
 

	
 
    for year, values in year_data.items():
 
        mean.add(values)
 

	
 
    return statistics.mean(mean)
 

	
 

	
 
def get_months_mean(data):
 

	
 
    mean = set()
 
    month_data = month_sum(data)
 

	
 
    for year, values in month_data.items():
 
        mean.add(values)
 

	
 
    return statistics.mean(mean)
 

	
 

	
 
def get_best_year(data):
 

	
 
    year_data = year_sum(data)
 
    return max(year_data.items(), key=lambda x: x[1])
 

	
 

	
 
def get_best_month(data):
 

	
 
    month_data = month_sum(data)
 
    return max(month_data.items(), key=lambda x: x[1])
 

	
 

	
 
def get_highest_donation():
 

	
 
    data = all_csv_to_dict()
 

	
 
    for date, values in data.items():
 
        data[date] = max(values)
 

	
 
    return max(
 
        sorted(
 
            data.items(),
 
            key=lambda x: x[0],
 
            reverse=True
 
        ),
 
        key=lambda x: x[1]
 
    )
 

	
 

	
 
def get_median_donation():
 
    data = all_csv_to_dict()
 
    donations = []
 

	
 
    for value in data.values():
 
        donations.extend(value)
 

	
 
    return statistics.median(donations)
 

	
 

	
 
def get_mode_donation():
 
    data = all_csv_to_dict()
 
    donations = []
 

	
 
    for value in data.values():
 
        donations.extend(value)
 

	
 
    return statistics.mode(donations)
ennstatus/donate/views.py
Show inline comments
 
# Ë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/>.
 

	
 
import json
 

	
 
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,
 
                                        all_csv_to_dict,
 
                                        get_all_years_mean,
 
                                        get_months_mean,
 
                                        get_best_year,
 
                                        get_best_month,
 
                                        get_highest_donation,
 
                                        get_median_donation,
 
                                        get_mode_donation,
 
                                        year_sum,
 
                                        month_sum,
 
                                        )
 
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)
 
@@ -114,91 +100,24 @@ def received():
 
                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():
 
    data = all_csv_to_dict()
 
    all_years_mean = format_decimal(
 
        get_all_years_mean(data),
 
        locale='de'
 
    )
 
    month_mean = format_decimal(
 
        get_months_mean(data),
 
        locale='de'
 
    )
 
    best_year = get_best_year(data)
 
    best_year = (best_year[0], format_decimal(best_year[1], locale='de'))
 

	
 
    best_month = get_best_month(data)
 
    best_month = (best_month[0], format_decimal(best_month[1], locale='de'))
 

	
 
    highest_donation = get_highest_donation()
 
    highest_donation = (highest_donation[0], format_decimal(highest_donation[1], locale='de'))
 

	
 
    median_donation = format_decimal(
 
        get_median_donation(),
 
        locale='de'
 
    )
 

	
 
    mode_donation = format_decimal(
 
        get_mode_donation(),
 
        locale='de'
 
    )
 

	
 
    return render_template('donate/statistics.html',
 
                           all_years_mean=all_years_mean,
 
                           month_mean=month_mean,
 
                           best_year=best_year,
 
                           best_month=best_month,
 
                           highest_donation=highest_donation,
 
                           median_donation=median_donation,
 
                           mode_donation=mode_donation
 
                           )
 

	
 
@donate_page.route('/data/year')
 
def yeardata():
 
    data = all_csv_to_dict() 
 
    data = year_sum(data)
 
    datalist = []
 

	
 
    for key, values in data.items():
 
        datalist.append({"key": key, "value": float(values)})
 
    datalist.sort(key=lambda x: x["key"])
 

	
 
    return json.dumps(datalist)
 

	
 

	
 
@donate_page.route('/data/month')
 
def monthdata():
 
    data = all_csv_to_dict()
 
    data = month_sum(data)
 
    data_list = []
 

	
 
    for key, values in data.items():
 
        data_list.append({"key": key, "value": float(values)})
 

	
 
    data_list.sort(key=lambda x: x["key"])
 

	
 
    return json.dumps(data_list)
ennstatus/templates/base.html
Show inline comments
 
@@ -73,50 +73,48 @@
 
            <li class="divider"></li>
 
            <li><a href="{{ url_for('root.about') }}">Organization</a></li>
 
            <li><a href="{{ url_for('root.partners') }}">Partners</a></li>
 
          </ul>
 
        </li>
 
        <li class="dropdown">
 
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Services <b class="caret"></b></a>
 
          <ul class="dropdown-menu">
 
            <li><a href="{{ url_for('status.index') }}">Tor Servers</a></li>
 
            <li class="divider"></li>
 
            <li><a href="{{ url_for('root.mirrors') }}">Mirrors</a></li>
 
            <li><a href="//{{ wiki_url }}">Wiki</a></li>
 
            <li class="divider"></li>
 
            <li><a href="{{ url_for('root.ennstatus') }}">About Ënnstatus</a></li>
 
          </ul>
 
        </li>
 
        <li class="dropdown">
 
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Support <b class="caret"></b></a>
 
          <ul class="dropdown-menu">
 
            <li><a href="{{ url_for('donate.index') }}">Donate</a></li>
 
            <li><a href="{{ url_for('donate.received') }}">Donation history</a></li>
 
            <li><a href="{{ url_for('root.bridgeprogram')}}">Adopt a Bridge</a></li>
 
            <li class="divider"></li>
 
            <li><a href="{{ url_for('root.member') }}">Join Us</a></li>
 
            <li class="divider"></li>
 
            <li><a href="{{ url_for('donate.statistics') }}">Donation statistics</a></li>
 
            <li class="dropdown-submenu">
 
            </li>
 
          </ul>
 
        </li>
 
        <li class="dropdown">
 
          <a href="#" class="dropdown-toggle" data-toggle="dropdown">Contact <b class="caret"></b></a>
 
          <ul class="dropdown-menu">
 
            <li><a href="{{ url_for('root.contact') }}">General</a></li>
 
            <li><a href="{{ url_for('root.abuse') }}">Abuse</a></li>
 
            <li class="divider"></li>
 
            <li><a href="https://twitter.com/FrennVunDerEnn" target="blank">Twitter</a></li>
 
            <li><a href="http://lists.enn.lu/listinfo/discuss" target="blank">Mailing List</a></li>
 
          </ul>
 
        </li>
 
      </ul>
 
    </div>
 
  </div>
 
  {% endblock %}
 
  <div class="row" id="content">
 
  {% with messages = get_flashed_messages(with_categories=True) %}
 
    {% if messages %}
 
      <div class="col-md-12">
 
        {% for category, message in messages %}
 
          {{ base_macros.display_error(category, message) }}
ennstatus/templates/donate/statistics.html
Show inline comments
 
@@ -30,43 +30,45 @@
 
  </div>
 
  <div class="col-md-12">
 
    <dl class="dl-horizontal">
 
      <dt>Mean per year:</dt>
 
      <dd>{{ all_years_mean }}</dd>
 
      <dt>Mean per month:</dt>
 
      <dd>{{ month_mean }}</dd>
 
      <dt>Best year:</dt>
 
      <dd>{{ best_year[0] }} {{ best_year[1] }}</dd>
 
      <dt>Best month:</dt>
 
      <dd>{{ best_month[0] }} {{ best_month[1] }}</dd>
 
      <dt>Highest donation:</dt>
 
      <dd>{{ highest_donation[0] }} {{ highest_donation[1] }}</dd>
 
      <dt>Median donation:</dt>
 
      <dd>{{ median_donation }}</dd>
 
      <dt>Mode donation:</dt>
 
      <dd>{{ mode_donation }}</dd>
 
    </dl>
 
  </div>
 
  <div class="col-md-12">
 
    <div class="panel panel-default">
 
      <div class="panel-heading">Donations per year</div>
 
      <div class="panel-body">
 
        <div id="yearchart" class="center-block"></div>
 
        <div id="yearslider"></div>
 
      </div>
 
    </div>
 
  </div>
 
  <div class="col-md-12">
 
    <div class="panel panel-default">
 
      <div class="panel-heading">Donations per month</div>
 
      <div class="panel-body">
 
        <div id="monthchart"></div>
 
        <div id="montslider"></div>
 
      </div>
 
    </div>
 
  </div>
 
{% endblock %}
 

	
 
{% block scripts %}
 
  {{ super() }}
 
  <script src="{{ url_for('static', filename='js/d3/d3.min.js') }}"></script>
 
  <script src="{{ url_for('static', filename='js/d3-tip/index.js') }}"></script>
 
  <script src="{{ url_for('static', filename='js/barcharts.js') }}"></script>
 
{% endblock %}
ennstatus/templates/root/ennstatus.html
Show inline comments
 
@@ -22,33 +22,32 @@
 
{% block content %}
 
  <div class="col-md-offset-1 col-md-11">
 
    <h2>About Ënnstatus</h2>
 
    <div class="col-md-6">
 
      <h3>API</h3>
 
      <p>We provide a <a href="http://json.org/">JSON</a> API for exporting the current status of our servers</p>
 
      <h4>Exporting all servers</h4>
 
      <p>Under the endpoint <a href="{{ url_for('api.export') }}">/api/export</a>, you can find an export of all our servers</p>
 
    </div>
 
    <div class="col-md-6">
 
      <h3>Used libraries:</h3>
 
      <ul>
 
        <li><a href="http://flask.pocoo.org/">Flask</a></li>
 
        <li><a href="https://github.com/mbr/flask-bootstrap">Flask-Bootstrap</a></li>
 
        <li><a href="http://flask-httpauth.readthedocs.org/en/latest/">Flask-HTTPAuth</a></li>
 
        <li><a href="http://packages.python.org/Flask-Mail/">Flask-Mail</a></li>
 
        <li><a href="http://https://github.com/miguelgrinberg/flask-moment/">Flask-Moment</a></li>
 
        <li><a href="https://flask-wtf.readthedocs.org/en/latest/">Flask-WTF</a></li>
 
        <li><a href="https://pypi.python.org/pypi/pygeoip/">pygeoip</a></li>
 
        <li><a href="https://pypi.python.org/pypi/jsonschema/">jsonschema</a></li>
 
        <li><a href="https://pypi.python.org/pypi/strict-rfc3339">strict-rfc3339</a></li>
 
        <li><a href="http://docs.python-requests.org/en/latest/">requests</a></li>
 
        <li><a href="https://github.com/isislovecruft/python-gnupg">python-gnupg</a></li>
 
        <li><a href="http://click.pocoo.org/">click</a></li>
 
        <li><a href="http://babel.pocoo.org/">Babel</a></li>
 
        <li><a href="http://videojs.com/">video.js</a></li>
 
      </ul>
 
      <p>Flag icons provided by: <a href="http://www.famfamfam.com/lab/icons/flags/">famfamfam</a></p>
 
      <p>Other icons provided by: <a href="http://glyphicons.com/">Glyphicons</a></p>
 
      <p>Fork us on <a href="https://bitbucket.org/fvde/ennstatus-relaunched">Bitbucket</a></p>
 
    </div>
 
  </div>
 
{% endblock %}
setup.py
Show inline comments
 
from setuptools import setup, find_packages
 

	
 

	
 
def _get_requirements():
 

	
 
    with open('requirements.in', encoding='utf-8') as f:
 
        lines = f.readlines()
 

	
 
    lines = [line[:-1] for line in lines if not line.startswith('#')]
 
    return lines
 

	
 

	
 
setup(name='Ennstatus',
 
      version='5.3.0-dev',
 
      version='5.3.0',
 
      description=('Ennstatus provides the user with vital information about '
 
                   'the status of the organizations Tor servers.'),
 
      author='Frënn vun der Ënn',
 
      author_email='info@enn.lu',
 
      url='https://bitbucket.org/fvde/ennstatus',
 
      license='GPLv3+',
 
      packages=find_packages(),
 
      install_requires=_get_requirements(),
 
      include_package_data=True,
 
      entry_points={
 
          'console_scripts': [
 
              'ennstatuscli = ennstatus.cli:cli',
 
          ]
 
      },
 
      classifiers=['Development Status :: 5 - Production/Stable',
 
                   'Environment :: Web Environment',
 
                   'Operating System :: POSIX',
 
                   'Programming Language :: Python',
 
                   'Programming Language :: Python :: 3',
 
                   'Programming Language :: Python :: 3.3',
 
                   'Topic :: Internet',
 
                   'Topic :: Internet :: WWW/HTTP',
 
                   'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
 
                   'Topic :: Internet :: WWW/HTTP :: WSGI',
0 comments (0 inline, 0 general)