Skip to content

Commit

Permalink
add sort by date for md_reports
Browse files Browse the repository at this point in the history
  • Loading branch information
bchartier committed Aug 26, 2016
1 parent f3a0ef6 commit 4093a2a
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 19 deletions.
Binary file modified mdchecker/db/mdchecker.db
Binary file not shown.
28 changes: 25 additions & 3 deletions mdchecker/inspirobot/Inspirobot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import logging
import datetime
import dateutil.parser
import hashlib
import json
import shapefile
Expand All @@ -34,7 +35,20 @@ def xmlGetTextNodes(doc, xpath, namespaces):
'gmd': u'http://www.isotc211.org/2005/gmd',
'gco': u'http://www.isotc211.org/2005/gco'
}))



def parse_string_for_max_date(dates_as_str):

dates_python = []
for date_str in dates_as_str.split(","):
date_str = date_str.strip()
if date_str != "":
date_python = dateutil.parser.parse(date_str, ignoretz=True)
dates_python.append(date_python)
if len(dates_python) > 0:
return max(dates_python)


################################################


Expand Down Expand Up @@ -345,12 +359,19 @@ def __init__(self, xml):
'/gmd:MD_Metadata/gmd:identificationInfo/'
'gmd:MD_DataIdentification/gmd:abstract/gco:CharacterString/text()',
self.namespaces)
self.date = xmlGetTextNodes(
dates_str = xmlGetTextNodes(
self.md,
'/gmd:MD_Metadata/gmd:identificationInfo/'
'gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/'
'gmd:date/gmd:CI_Date/gmd:date/gco:DateTime/text()',
self.namespaces).split('-')[0]
self.namespaces)
self.date = parse_string_for_max_date(dates_str)
md_dates_str = xmlGetTextNodes(
self.md,
'/gmd:MD_Metadata/gmd:dateStamp/'
'gco:DateTime/text()',
self.namespaces)
self.md_date = parse_string_for_max_date(md_dates_str)
self.contact = {
'mails': self.md.xpath(
'/gmd:MD_Metadata/gmd:contact/gmd:CI_ResponsibleParty/gmd:contactInfo/'
Expand Down Expand Up @@ -411,6 +432,7 @@ def asDict(self):
return {
'fileIdentifier': self.fileIdentifier,
'MD_Identifier': self.MD_Identifier,
'md_date': self.md_date,
'title': self.title,
'OrganisationName': self.OrganisationName,
'abstract': self.abstract,
Expand Down
54 changes: 51 additions & 3 deletions mdchecker/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ def runTests(args, mdUnitTests):
)

# Test session db record
test_datetime = datetime.datetime.utcnow()
ts = TestSession(
cat_url=cfg['cswurl'],
filter=constraintstr,
Expand Down Expand Up @@ -218,6 +217,8 @@ def runTests(args, mdUnitTests):
md = ResourceMd(
cat_url=cfg['cswurl'],
file_id=meta.fileIdentifier,
md_date=meta.md_date,
res_date=meta.date,
res_uri=meta.MD_Identifier,
res_title=meta.title,
res_abstract=meta.abstract,
Expand All @@ -226,6 +227,8 @@ def runTests(args, mdUnitTests):
db.session.add(md)
else:
md.file_id = meta.fileIdentifier
md.md_date = meta.md_date
md.res_date = meta.date
md.res_uri = meta.MD_Identifier
md.res_title = meta.title
md.res_abstract = meta.abstract
Expand Down Expand Up @@ -325,7 +328,6 @@ def index():
score = 0

# querystring parser
request_args = getArgsFromQuery(request)
args.update(getArgsFromQuery(request))

if doWeNeedToProcessRequest(request):
Expand Down Expand Up @@ -367,7 +369,53 @@ def session_by_id(id=None):
id=id
).first()

return object_list('session_id.html', session.md_reports, cfg=cfg, session=session)
sort_by = "score"
asc = True

request_sort_by = request.args.get('sort')
request_asc = request.args.get('asc')
request_desc = request.args.get('desc')
if request_sort_by:
request_sort_by = request_sort_by.lower().strip()
if request_sort_by in ("id", "title", "score", "organisation", "date"):
sort_by = request_sort_by

if request_asc:
asc = True
elif request_desc:
asc = False

if sort_by == "id":
if asc:
query = session.md_reports.join(ResourceMd).order_by("file_id")
else:
query = session.md_reports.join(ResourceMd).order_by("file_id desc")

elif sort_by == "score":
if asc:
query = session.md_reports.order_by(MdReport.score)
else:
query = session.md_reports.order_by(MdReport.score.desc())

elif sort_by == "title":
if asc:
query = session.md_reports.join(ResourceMd).order_by("res_title")
else:
query = session.md_reports.join(ResourceMd).order_by("res_title desc")

elif sort_by == "organisation":
if asc:
query = session.md_reports.join(ResourceMd).order_by("res_organisation_name")
else:
query = session.md_reports.join(ResourceMd).order_by("res_organisation_name desc")

elif sort_by == "date":
if asc:
query = session.md_reports.join(ResourceMd).order_by("md_date")
else:
query = session.md_reports.join(ResourceMd).order_by("md_date desc")

return object_list('session_id.html', query, cfg=cfg, session=session, sort_by=sort_by, asc=asc)


@app.route("/tests/")
Expand Down
2 changes: 2 additions & 0 deletions mdchecker/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class ResourceMd(db.Model):
id = db.Column(db.Integer, primary_key=True)
cat_url = db.Column(db.String(512))
file_id = db.Column(db.String(128))
md_date = db.Column(db.DateTime)
res_date = db.Column(db.DateTime)
res_uri = db.Column(db.String(128))
res_title = db.Column(db.String(128))
res_abstract = db.Column(db.String(1024))
Expand Down
2 changes: 1 addition & 1 deletion mdchecker/templates/include_page_links.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{% if page == object_list.page %}
<a href="#">{{ page }}<span class="sr-only">(current)</span></a>
{% else %}
<a href="./?page={{ page }}">{{ page }}</a>
<a href="./?page={{ page }}&sort={{ sort_by }}{% if not asc %}&desc=yes{% endif %}">{{ page }}</a>
{% endif %}
{% else %}
<a class="disabled">...</a>
Expand Down
47 changes: 35 additions & 12 deletions mdchecker/templates/session_id.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
{% extends "base.html" %}

{% block extra_styles %}
<style rel="stylesheet" type="text/css">
.nowrap {
white-space: nowrap;
}
</style>
{% endblock %}

{% block content %}
<div class="panel panel-default">
<div class="panel-heading">Session {{ session.id }}</div>
Expand All @@ -16,21 +24,32 @@
</div>
</div>

<br>
formulaire de recherche<br>
liste des organisations

<form class="form-horizontal" method="GET" action=".">

<!-- sortby -->
<div class="form-group">
<label for="sortby" class="col-sm-2 control-label">Tri&eacute; par</label>
<div class="col-sm-4">
<select name="sortby" class="form-control">
<option value="score" {% if sort_by == "score" %}selected{% endif %}>Score</option>
<option value="organisation" {% if sort_by == "organisation" %}selected{% endif %}>Nom de l'organisme</option>
<option value="id" {% if sort_by == "id" %}selected{% endif %}>File identifier des métadonnées</option>
<option value="title" {% if sort_by == "title" %}selected{% endif %}>Titre des métadonnées</option>
<option value="date" {% if sort_by == "date" %}selected{% endif %}>Date des métadonnées</option>
</select>
</div>
</div>

chaine de caractères dans titre
chaine de caractères dans résumé
erreur/criticité
tri par titre, organisation, score
nombre d'enregistrements par page

<br>
résultats<br>
<!-- submit -->
<!--div class="form-group">
<div class="col-sm-offset-2 col-sm-3>
<div class="input-group">
<button type="submit" class="btn btn-default">Analyser</button>
</div>
</div>
</div-->

</form>

<div class="panel panel-default">
<div class="panel-heading">Résultats des tests</div>
Expand All @@ -41,6 +60,8 @@
<thead>
<tr>
<th>Métadonnées</th>
<th>Date md</th>
<th>Organisme</th>
<th>Titre</th>
<th>Score</th>
<th>Niveau</th>
Expand All @@ -64,6 +85,8 @@
{% endif %}
<tr>
<td>{{ md.file_id }}</td>
<td class="nowrap">{% if md.md_date %}{{ md.md_date.strftime('%Y-%m-%d') }}{% endif %}</td>
<td>{{ md.res_organisation_name }}</td>
<td>{{ md.res_title }}</td>
<td>{{ report.score }}</td>
<td>{{ error_level }}</td>
Expand Down

0 comments on commit 4093a2a

Please sign in to comment.