comments and README.md

This commit is contained in:
2023-09-28 17:08:50 +02:00
parent 68311135bf
commit 945b9c2195
6 changed files with 298 additions and 58 deletions

View File

@@ -1,17 +1,92 @@
from flask import current_app as app
from flask import request
from .db import db
from flask import request, url_for
from .db import get_db
@app.route("/detail", methods=["GET"])
def detail():
ico = request.args.get("ico")
if "ico" is None:
return "missing ico"
"""
GET a detial of one record in json format
:args: ico: integer value of IČO
:return: json object representing record with ico
"""
return f"ICO je {ico}"
ico = request.args.get("ico")
if ico is None:
return {}
try:
ico = int(ico)
except ValueError:
return{}
collection = get_db()
data = collection.find_one({"ico.value":ico})
data.pop("_id")
return data
@app.route("/list", methods=["GET"])
def list_data():
return "list"
"""
GET a list of ORSR records.
Pagination is inspired by official MongoDB resources/tutorials
The results are paginated using the `page` parameter.
:return: json object with list of records and links to other pages
"""
page = int(request.args.get("page", 1))
per_page = 50 # A const value.
collection = get_db()
# For pagination, we sort by ICO
# then skip the number of docs that earlier pages would have displayed,
# and then to limit to the fixed page size, ``per_page``.
records = collection.find().sort("ico.value").skip(per_page * (page - 1)).limit(per_page)
records_count = collection.count_documents({})
links = {
"self": {"href": url_for(".list_data", page=page, _external=True)},
"last": {
"href": url_for(
".list_data", page=(records_count // per_page) + 1, _external=True
)
},
}
# Add a 'prev' link if it's not on the first page:
if page > 1:
links["prev"] = {
"href": url_for(".list_data", page=page - 1, _external=True)
}
# Add a 'next' link if it's not on the last page:
if page - 1 < records_count // per_page:
links["next"] = {
"href": url_for(".list_data", page=page + 1, _external=True)
}
return {
"records": [transform_for_list(record) for record in records], # get only
"_links": links,
}
def transform_for_list(record_in):
"""
retrieve ico and obchodneMeno from record
:param record_in: record with all data
:return: dictionary of ico and obchodneMeno
"""
if (obch_meno := record_in["obchodneMeno"].get("value")) is None:
obch_meno_old = record_in["obchodneMeno"].get("old_values")
if len(obch_meno_old) == 0:
obch_meno = ""
else:
obch_meno = obch_meno_old[0].get("value", "")
record = {
"ico": record_in["ico"]["value"],
"obchodneMeno": obch_meno
}
return record