-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-structure app to more generic architecture.
- Loading branch information
1 parent
4a140b2
commit bdabc50
Showing
58 changed files
with
196 additions
and
985 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.