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,6 +1,12 @@
import configparser
from os import path
"""
Reads the config files and stores the config into settings dictionary.
This dictionary can be then used in the application to access config values.
"""
# parse base config file
config = configparser.ConfigParser()
config_path = path.dirname(path.abspath(__file__))

View File

@@ -1,10 +1,15 @@
from flask import g
from werkzeug.local import LocalProxy
from pymongo import MongoClient
from .config import settings
def connect_db():
"""
Creates the connection to the MongoDB using the config file data.
Thos function is used by flask application as well as the scraper script
:return: connection to the collection in the mongodb
"""
client = MongoClient(settings["mongodb_uri"])
db = client[settings["mongodb_db"]]
collection = db[settings["mongodb_collection"]]
@@ -12,12 +17,17 @@ def connect_db():
def disconnect_db(conn):
"""
Disconnects open connection
:param conn: open db connection
"""
conn.database.client.close()
def get_db():
"""
Configuration method to return db instance
Get collection instance for flask application. If no instance stored in global variables, then create connection
and store it in g
"""
collection = getattr(g, "_database", None)
@@ -25,7 +35,3 @@ def get_db():
collection = g._database = connect_db()
return collection
# Use LocalProxy to read the global db instance with just `db`
db = LocalProxy(get_db)

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