styling and filters
This commit is contained in:
parent
2017f9a251
commit
e16856d638
3 changed files with 93 additions and 6 deletions
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/python
|
||||
import os, json, peewee
|
||||
from flask import Flask, request, render_template
|
||||
from flask import Flask, request, render_template, url_for
|
||||
from flask.ext.compress import Compress
|
||||
import config
|
||||
|
||||
|
@ -23,6 +23,22 @@ class Change(peewee.Model):
|
|||
address = peewee.BooleanField(default=False)
|
||||
changes = peewee.TextField()
|
||||
|
||||
def changed_coord(self):
|
||||
return json.loads(self.changes)[0]
|
||||
|
||||
def changed_tags(self):
|
||||
tags = json.loads(self.changes)[1]
|
||||
for t in tags:
|
||||
if tags[t][0] is None:
|
||||
tags[t].append('create')
|
||||
elif tags[t][1] is None:
|
||||
tags[t].append('delete')
|
||||
elif tags[t][0] != tags[t][1]:
|
||||
tags[t].append('modify')
|
||||
else:
|
||||
tags[t].append('nothing')
|
||||
return tags
|
||||
|
||||
class Meta:
|
||||
database = database
|
||||
db_table = 'mapsme_change'
|
||||
|
@ -36,13 +52,38 @@ def teardown(exception):
|
|||
if not database.is_closed():
|
||||
database.close()
|
||||
|
||||
def purl(params, **kwargs):
|
||||
p2 = params.copy()
|
||||
for k, v in kwargs.iteritems():
|
||||
p2[k] = v
|
||||
return url_for('the_one_and_only_page', **p2)
|
||||
|
||||
@app.route('/')
|
||||
def the_one_and_only_page():
|
||||
changes = Change.select().order_by(Change.id.desc()).paginate(1, config.PAGE_SIZE)
|
||||
# Parse query params
|
||||
params = {}
|
||||
page = request.args.get('page', '1')
|
||||
params['page'] = int(page) if page.isdigit() else 1
|
||||
user = request.args.get('user', None)
|
||||
if user is not None:
|
||||
params['user'] = user
|
||||
|
||||
# Construct queries
|
||||
changes = Change.select().order_by(Change.id.desc()).paginate(params['page'], config.PAGE_SIZE)
|
||||
users = Change.select(Change.user, peewee.fn.Count(Change.id).alias('count')).group_by(Change.user).order_by(peewee.fn.Count(Change.id).desc()).limit(config.TOP)
|
||||
tags = Change.select(Change.main_tag, peewee.fn.Count(Change.id).alias('count')).group_by(Change.main_tag).order_by(peewee.fn.Count(Change.id).desc()).limit(config.TOP)
|
||||
versions = Change.select(Change.version, peewee.fn.Count(Change.id).alias('count')).group_by(Change.version).order_by(peewee.fn.Count(Change.id).desc()).limit(config.TOP)
|
||||
stat_src = Change.select(Change.action, Change.obj_type, peewee.fn.Count(Change.id).alias('count')).group_by(Change.action, Change.obj_type).order_by(peewee.fn.Count(Change.id).desc()).limit(config.TOP)
|
||||
stat_src = Change.select(Change.action, Change.obj_type, peewee.fn.Count(Change.id).alias('count')).group_by(Change.action, Change.obj_type).order_by(peewee.fn.Count(Change.id).desc())
|
||||
|
||||
# Apply filters
|
||||
if user:
|
||||
changes = changes.where(Change.user == user)
|
||||
users = users.where(Change.user == user)
|
||||
tags = tags.where(Change.user == user)
|
||||
versions = versions.where(Change.user == user)
|
||||
stat_src = stat_src.where(Change.user == user)
|
||||
|
||||
# Calculate statistics
|
||||
stats = {}
|
||||
stats['created'] = stats['deleted'] = stats['modified'] = stats['anomalies'] = 0
|
||||
stats['nodes'] = stats['ways'] = stats['relations'] = stats['total'] = 0
|
||||
|
@ -62,8 +103,9 @@ def the_one_and_only_page():
|
|||
stats['ways'] += stat.count
|
||||
elif stat.obj_type == 'r':
|
||||
stats['relations'] += stat.count
|
||||
stats['pages'] = (stats['total'] + config.PAGE_SIZE - 1) / config.PAGE_SIZE
|
||||
|
||||
return render_template('index.html', stats=stats, changes=changes, users=users, tags=tags, versions=versions)
|
||||
return render_template('index.html', stats=stats, changes=changes, users=users, tags=tags, versions=versions, params=params, purl=purl)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(threaded=True)
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
body {
|
||||
font-family: sans-serif;
|
||||
font-size: 12pt;
|
||||
}
|
||||
|
||||
.change {
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.change table {
|
||||
margin-left: 2em;
|
||||
font-size: 10pt;
|
||||
}
|
||||
|
||||
tr.create {
|
||||
background-color: lightgreen;
|
||||
}
|
||||
|
||||
tr.delete {
|
||||
background-color: pink;
|
||||
}
|
||||
|
||||
tr.modify {
|
||||
background-color: yellow;
|
||||
}
|
||||
|
||||
tr.nothing {
|
||||
font-size: 8pt;
|
||||
font-style: italic;
|
||||
}
|
|
@ -20,7 +20,11 @@
|
|||
<h2>Top Users</h2>
|
||||
<ol>
|
||||
{% for user in users %}
|
||||
<li>{{ user.user }} ({{user.count}})</li>
|
||||
{% if params.user %}
|
||||
<li>{{ user.user }} ({{user.count}}) <a href="{{ purl(params, user=None) }}">X</a></li>
|
||||
{% else %}
|
||||
<li><a href="{{ purl(params, user=user.user) }}">{{ user.user }}</a> ({{user.count}})</li>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</ol>
|
||||
</div>
|
||||
|
@ -43,8 +47,19 @@
|
|||
<div>
|
||||
<h2>Changes</h2>
|
||||
{% for change in changes %}
|
||||
<div>
|
||||
<div class="change">
|
||||
<a href="https://www.openstreetmap.org/user/{{ change.user }}">{{ change.user }}</a> at {{ change.timestamp }} in <a href="https://www.openstreetmap.org/changeset/{{ change.changeset }}">{{ change.changeset }}</a>: {{ change.main_tag }}
|
||||
<table>
|
||||
{% set coord = change.changed_coord() %}
|
||||
{% if coord %}
|
||||
<tr class="modify"><td>coord</td><td>{{ coord[0] }}</td><td>{{ coord[1] }}</td></tr>
|
||||
{% endif %}
|
||||
{% for key, value in change.changed_tags().iteritems() %}
|
||||
<tr class="{{ value[2] }}"><td>{{ key }}</td><td>{{ value[0] or '' }}</td><td>{{ value[1] }}</td></tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% if params.page > 1 %}<a href="{{ purl(params, page=params.page-1) }}">« Newer</a>{% else %}« Newer{% endif %}
|
||||
{% if params.page < stats.pages %}<a href="{{ purl(params, page=params.page+1) }}">Older »</a>{% else %}Older »{% endif %}
|
||||
</div>
|
||||
|
|
Loading…
Add table
Reference in a new issue