Skip to content

Commit

Permalink
Support uploading audit files, hide projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Zverik committed Dec 12, 2017
1 parent 0f0e0ae commit 23df1f1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
41 changes: 33 additions & 8 deletions www/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ def is_admin(user, project=None):
def front():
user = get_user()
projects = Project.select().order_by(Project.updated.desc())

def local_is_admin(proj):
return is_admin(user, proj)
return render_template('index.html', user=user, projects=projects,
admin=is_admin(user))
admin=is_admin(user), is_admin=local_is_admin)


@app.route('/login')
Expand Down Expand Up @@ -283,7 +286,7 @@ def add_project(pid=None):
return render_template('newproject.html', project=project)


def update_features(project, features):
def update_features(project, features, audit):
curfeats = Feature.select(Feature).where(Feature.project == project)
ref2feat = {f.ref: f for f in curfeats}
deleted = set(ref2feat.keys())
Expand All @@ -294,6 +297,7 @@ def update_features(project, features):
md5 = hashlib.md5()
md5.update(data.encode('utf-8'))
md5_hex = md5.hexdigest()

coord = f['geometry']['coordinates']
if coord[0] < minlon:
minlon = coord[0]
Expand All @@ -303,10 +307,12 @@ def update_features(project, features):
minlat = coord[1]
if coord[1] > maxlat:
maxlat = coord[1]

if 'ref_id' in f['properties']:
ref = f['properties']['ref_id']
else:
ref = '{}{}'.format(f['properties']['osm_type'], f['properties']['osm_id'])

update = False
if ref in ref2feat:
deleted.remove(ref)
Expand All @@ -317,13 +323,20 @@ def update_features(project, features):
feat = Feature(project=project, ref=ref)
feat.validates_count = 0
update = True

f_audit = audit.get(ref)
if f_audit and f_audit != feat.audit:
feat.audit = f_audit
update = True

if update:
feat.feature = data
feat.feature_md5 = md5_hex
feat.lon = round(coord[0] * 1e7)
feat.lat = round(coord[1] * 1e7)
feat.action = f['properties']['action'][0]
feat.save()

if deleted:
q = Feature.delete().where(Feature.ref << list(deleted))
q.execute()
Expand Down Expand Up @@ -363,7 +376,10 @@ def add_flash(pid, msg):
project.url = None
project.description = request.form['description'].strip()
project.can_validate = request.form.get('validate') is not None
project.owner = user
project.hidden = request.form.get('is_hidden') is not None
if not project.owner or user.uid not in config.ADMINS:
project.owner = user

if 'json' not in request.files or request.files['json'].filename == '':
if not pid:
return add_flash(pid, 'Would not create a project without features')
Expand All @@ -372,17 +388,27 @@ def add_flash(pid, msg):
try:
features = json.load(codecs.getreader('utf-8')(request.files['json']))
except ValueError as e:
return add_flash(pid, 'Error in the uploaded file: {}'.format(e))
return add_flash(pid, 'Error in the uploaded features file: {}'.format(e))
if 'features' not in features or not features['features']:
return add_flash(pid, 'No features found in the JSON file')
features = features['features']

project.updated = datetime.datetime.utcnow().date()
audit = None
if 'audit' in request.files and request.files['audit'].filename:
try:
audit = json.load(codecs.getreader('utf-8')(request.files['audit']))
except ValueError as e:
return add_flash(pid, 'Error in the uploaded audit file: {}'.format(e))
if not audit:
return add_flash(pid, 'No features found in the audit JSON file')

if features or audit or not project.updated:
project.updated = datetime.datetime.utcnow().date()
project.save()

if features:
with database.atomic():
update_features(project, features)
update_features(project, features, audit or {})

if project.feature_count == 0:
project.delete_instance()
Expand All @@ -400,8 +426,7 @@ def clear_skipped(pid):
query = Task.delete().where(
Task.user == user, Task.skipped == True,
Task.feature.in_(features))
deleted = query.execute()
print('Deleted:', deleted)
query.execute()
return redirect(url_for('project', name=project.name))


Expand Down
16 changes: 3 additions & 13 deletions www/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,14 @@ <h1>Auditing Tool for OSM Conflator</h1>
{% if admin %}
<p><a href="{{ url_for('add_project') }}">Create a project</a></p>
{% endif %}
<p>Current imports that need validating:</p>
<p>Imports that need validating:</p>
<ul>
{% for proj in projects %}
{% if not proj.complete %}
<li><a href="{{ url_for('project', name=proj.name) }}">{{ proj.title }}</a> ({{ proj.feature_count }})</li>
{% if not proj.hidden or is_admin(proj) %}
<li>{% if proj.hidden %}🕶️ {% endif %}<a href="{{ url_for('project', name=proj.name) }}">{{ proj.title }}</a> ({{ proj.feature_count }})</li>
{% endif %}
{% endfor %}
</ul>
{% if admin %}
<p>Archived validated imports:</p>
<ul>
{% for proj in projects %}
{% if proj.complete %}
<li><a href="{{ url_for('project', name=proj.name) }}">{{ proj.title }}</a> ({{ proj.feature_count }})</li>
{% endif %}
{% endfor %}
</ul>
{% endif %}
{% if not user %}
<p><a href="{{ url_for('login') }}">Login to validate imports</a></p>
{% else %}
Expand Down

0 comments on commit 23df1f1

Please sign in to comment.