Skip to content

Commit

Permalink
Re-structure app to more generic architecture.
Browse files Browse the repository at this point in the history
  • Loading branch information
skasberger committed Oct 26, 2020
1 parent 4a140b2 commit bdabc50
Show file tree
Hide file tree
Showing 58 changed files with 196 additions and 985 deletions.
2 changes: 1 addition & 1 deletion .readthedocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ version: 2

# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: fhe_collector/docs/source/conf.py
configuration: app/docs/source/conf.py

# Optionally build your docs in additional formats such as PDF and ePub
formats: all
Expand Down
File renamed without changes.
File renamed without changes.
107 changes: 107 additions & 0 deletions app/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""API functions."""
from flask import render_template
from flask import request
from flask import jsonify
from flask import Blueprint

api = Blueprint("api", __name__)


@api.route("/")
def homepage():
return jsonify({"status": "on", "api_version": "1.0"})


@api.route("/add_data", methods=["POST", "GET"])
def add_data():
"""Add data via an API endpoint to the database.
Required: doi
Optional: url, date
"""
response_status = "error"
url_type_list = [
"ojs",
"doi_new",
"doi_old",
"doi_new_landingpage",
"unpaywall",
"pubmed",
"pubmedcentral",
]

json_data = request.get_json()
token = json_data["token"]

if request.method == "POST":
try:
if "X-API-Key" in request.headers:
if app.config["API_TOKEN"] == request.headers["X-API-Key"]:
if request.headers["Content-Type"] == "application/json":
json_data = request.get_json()
if isinstance(json_data, list):
is_data_valid = True
for entry in data:
# Validate entry
if "doi" in entry:
if not isinstance(entry["doi"], str):
response = "DOI {} is no string.".format(
entry["doi"]
)
is_data_valid = False
if "url" in entry:
if not isinstance(entry["url"], str):
response = "URL {} is no string.".format(
entry["url"]
)
is_data_valid = False
else:
print("URL is missing")
is_data_valid = False
if "url_type" in entry:
if not isinstance(entry["url_type"], str):
response = "URL type {} is no string.".format(
entry["url_type"]
)
is_data_valid = False
if entry["url_type"] not in url_type_list:
response = "URL type {} is not one of the allowed types.".format(
entry["url_type"]
)
is_data_valid = False
else:
response = "URL type is missing."
is_data_valid = False
if "date" in entry:
if not isinstance(entry["date"], str):
response = "Date {} is no string.".format(
entry["date"]
)
is_data_valid = False
else:
response = "Date is missing."
is_data_valid = False
else:
is_data_valid = False
response = "DOI is missing in {}.".format(entry)
if is_data_valid:
resp_func = import_dois_from_api(data)
if resp_func:
response = resp_func
response_status = "ok"
else:
response = "Error: JSON from API could not be stored in database."
else:
response = "No list of data in JSON."
else:
response = "No JSON delivered."
else:
response = "Authentication token not right."
else:
response = "Authentication token not passed."
except:
response = "Undefined error."

return jsonify({"status": response_status, "content": response})
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions app/main/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""main functions."""

from flask import Blueprint

main = Blueprint("main", __name__)

from . import views, errors
13 changes: 13 additions & 0 deletions app/main/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from flask import render_template
from . import main


@main.app_errorhandler(404)
def not_found_error(error):
return render_template("404.html"), 404


@main.app_errorhandler(500)
def internal_error(error):
db.session.rollback()
return render_template("500.html"), 500
27 changes: 27 additions & 0 deletions app/main/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from flask import render_template, current_app
from flask_sqlalchemy import get_debug_queries
from . import main


@main.after_app_request
def after_request(response):
for query in get_debug_queries():
if query.duration >= current_app.config["FLASKY_SLOW_DB_QUERY_TIME"]:
current_app.logger.warning(
"Slow query: %s\nParameters: %s\nDuration: %fs\nContext: %s\n"
% (query.statement, query.parameters, query.duration, query.context)
)
return response


@main.route("/")
@main.route("/index")
def index():
"""Homepage."""
return render_template("index.html", title="Home")


@main.route("/api")
def api():
"""Api page."""
return render_template("api.html", title="API")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 19 additions & 0 deletions fhe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from flask_migrate import Migrate, upgrade
from app import create_app, db
from app.models import Url, Doi
import os
from dotenv import load_dotenv


dotenv_path = os.path.join(os.path.dirname(__file__), ".env")
if os.path.exists(dotenv_path):
load_dotenv(dotenv_path)


app = create_app(os.getenv("FLASK_CONFIG") or "default")
migrate = Migrate(app, db)


@app.shell_context_processor
def make_shell_context():
return {"db": db, "Doi": Doi, "Url": Url}
File renamed without changes.
50 changes: 0 additions & 50 deletions migrations/versions/0d85a668b602_re_factor_models.py

This file was deleted.

39 changes: 0 additions & 39 deletions migrations/versions/125bfc6141de_add_fbrequest_model.py

This file was deleted.

28 changes: 0 additions & 28 deletions migrations/versions/1298d4abb0d2_add_date_to_doi.py

This file was deleted.

42 changes: 0 additions & 42 deletions migrations/versions/1a7a23887ebf_.py

This file was deleted.

45 changes: 0 additions & 45 deletions migrations/versions/1f25d2e2ed47_users_table.py

This file was deleted.

Loading

0 comments on commit bdabc50

Please sign in to comment.