Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
*.pyo
*.pyd
README.md
.admin/tokens
.admin/tokens
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@
]
}
]
}
}
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"python.linting.pylintEnabled": true,
"[python]": {
"editor.insertSpaces": true,
"editor.tabSize": 4
"editor.tabSize": 4
},
"todo-tree.highlights.customHighlight": {
"TODO": {
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 changes: 6 additions & 6 deletions cannlytics_api/api/auth.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

from firebase_admin import auth


def authenticate(request):
""" Identify the user's Firebase account using an ID token. """
"""Identify the user's Firebase account using an ID token."""
authorization = request.headers["Authorization"]
token = authorization.split(' ')[1]
token = authorization.split(" ")[1]
claims = auth.verify_id_token(token)
uid = claims['uid']
request.session['uid'] = uid # Save user's custom claims in a session?
return claims
uid = claims["uid"]
request.session["uid"] = uid # Save user's custom claims in a session?
return claims
14 changes: 8 additions & 6 deletions cannlytics_api/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
VERSION = "v1"


@api_view(['GET'])
@api_view(["GET"])
def index(request, format=None):
"""Informational base endpoint."""
message = f"Welcome to the Cannlytics API. The current version is {VERSION} and is located at {BASE}/{VERSION}."
return Response({ "data": message}, content_type="application/json")
message = "Welcome to the Cannlytics API."
message += f"The current version is {VERSION} and is located at {BASE}/{VERSION}."
return Response({"data": message}, content_type="application/json")


@api_view(['GET'])
@api_view(["GET"])
def base(request, format=None):
"""Informational version endpoint."""
message = f"Welcome to {VERSION} of the Cannlytics API. Available endpoints:\n\n"
message = f"Welcome to {VERSION} of the Cannlytics API."
message += "Available endpoints:\n\n"
for endpoint in ENDPOINTS:
message += f"{endpoint}\n"
return Response({ "data": message}, content_type="application/json")
return Response({"data": message}, content_type="application/json")
30 changes: 17 additions & 13 deletions cannlytics_api/api/cannlypedia.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
from rest_framework.decorators import api_view
from rest_framework.response import Response

@api_view(['GET'])

@api_view(["GET"])
def scholars(request, format=None):
"""Get information about scholars."""

if request.method == 'GET':
author = request.query_params.get('q', None)
if request.method == "GET":
author = request.query_params.get("q", None)
if author:
from scholarly import scholarly

search_query = scholarly.search_author(author)
author_source = next(search_query)
author_data = {
'author': author,
'affiliation': author_source['affiliation'],
'cited_by': author_source['citedby'],
'email_domain': author_source['email_domain'],
'interests': author_source['interests'],
'photo_url': author_source['url_picture'],
}
"author": author,
"affiliation": author_source["affiliation"],
"cited_by": author_source["citedby"],
"email_domain": author_source["email_domain"],
"interests": author_source["interests"],
"photo_url": author_source["url_picture"],
}
return Response(author_data, content_type="application/json")

# Return an error if no author is specified.
error_message = 'Author not found in request. Specify ?q={url_encoded_author_name}'
return Response({ "error": error_message}, content_type="application/json")
error_message = (
"Author not found in request. Specify ?q={url_encoded_author_name}"
)
return Response({"error": error_message}, content_type="application/json")
52 changes: 26 additions & 26 deletions cannlytics_api/api/labs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,41 @@
from .auth import authenticate


@api_view(['GET'])
@api_view(["GET"])
def lab(request, format=None):
"""Get or update information about a lab."""

# Query labs.
if request.method == 'GET':
if request.method == "GET":
limit = request.query_params.get("limit", None)
order_by = request.query_params.get("order_by", "state")
# TODO: Get any filters from dict(request.query_params)
labs = get_collection('labs', order_by=order_by, limit=limit, filters=[])
return Response({ "data": labs}, content_type="application/json")
labs = get_collection("labs", order_by=order_by, limit=limit, filters=[])
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
labs = get_collection("labs", order_by=order_by, limit=limit, filters=[])
labs = get_collection('labs', order_by=order_by, limit=limit, filters=[])

return Response({"data": labs}, content_type="application/json")


@api_view(['GET', 'POST'])
@api_view(["GET", "POST"])
def labs(request, format=None):
"""Get or update information about labs."""

# Query labs.
if request.method == 'GET':
if request.method == "GET":
limit = request.query_params.get("limit", None)
order_by = request.query_params.get("order_by", "state")
# TODO: Get any filters from dict(request.query_params)
labs = get_collection('labs', order_by=order_by, limit=limit, filters=[])
return Response({ "data": labs}, content_type="application/json")
labs = get_collection("labs", order_by=order_by, limit=limit, filters=[])
Copy link
Copy Markdown
Author

@mathematicalmichael mathematicalmichael Aug 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

usage on line 90 uses double quotes, assuming that was the intent. leaving this

return Response({"data": labs}, content_type="application/json")

# Update a lab given a valid Firebase token.
elif request.method == 'POST':
elif request.method == "POST":

# Check token.
try:
claims = authenticate(request)
except:
return Response({"error": "Could not authenticate."}, status=status.HTTP_400_BAD_REQUEST)
except Exception: # TODO: what is the correct exception?
return Response(
{"error": "Could not authenticate."}, status=status.HTTP_400_BAD_REQUEST
)

# Get the posted lab data.
lab = request.data
Expand All @@ -56,7 +58,7 @@ def labs(request, format=None):
before = existing_data[key]
if before != after:
changes.append({"key": key, "before": before, "after": after})

# Get a timestamp.
timestamp = datetime.now().isoformat()
lab["updated_at"] = timestamp
Expand All @@ -80,31 +82,29 @@ def labs(request, format=None):
return Response(log_entry, status=status.HTTP_201_CREATED)


@api_view(['GET', 'POST'])
@api_view(["GET", "POST"])
def lab_logs(request, org_id, format=None):
"""Get or create lab logs."""

if request.method == 'GET':
if request.method == "GET":
data = get_collection(f"labs/{org_id}/logs")
return Response({ "data": data}, content_type="application/json")

elif request.method == 'POST':
# TODO: Create a log.
return Response({ "data": "Under construction"}, content_type="application/json")
return Response({"data": data}, content_type="application/json")

elif request.method == "POST":
# TODO: Create a log.
return Response({"data": "Under construction"}, content_type="application/json")


@api_view(['GET', 'POST'])
@api_view(["GET", "POST"])
def lab_analyses(request, org_id, format=None):
"""
Get or update (TODO) lab analyses.
"""

if request.method == 'GET':
if request.method == "GET":
data = get_collection(f"labs/{org_id}/analyses")
return Response({ "data": data}, content_type="application/json")

elif request.method == 'POST':
# TODO: Create an analysis.
return Response({ "data": "Under construction"}, content_type="application/json")
return Response({"data": data}, content_type="application/json")

elif request.method == "POST":
# TODO: Create an analysis.
return Response({"data": "Under construction"}, content_type="application/json")
28 changes: 16 additions & 12 deletions cannlytics_api/api/leaf_test_api.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,43 @@
from rest_framework import status
# from rest_framework import status
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# from rest_framework import status

from rest_framework.decorators import api_view
from rest_framework.response import Response

from utils.firebase import get_collection, get_document, update_document
# from utils.firebase import get_collection, get_document, update_document
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# from utils.firebase import get_collection, get_document, update_document

from utils.firebase import get_collection


@api_view(['GET', 'POST'])
@api_view(["GET", "POST"])
def lab_results(request, format=None):
"""Get lab results data."""

if request.method == 'GET':
if request.method == "GET":
limit = request.query_params.get("limit", 1000)
if limit:
limit = int(limit)
order_by = request.query_params.get("order_by", "")
# TODO: Get any filters from dict(request.query_params)
docs = get_collection('tests/leaf/lab_results', order_by=order_by, limit=limit, filters=[])
docs = get_collection(
"tests/leaf/lab_results", order_by=order_by, limit=limit, filters=[]
)
Comment on lines +19 to +21
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like I recall another get_collection with double quotes.

return Response(docs, content_type="application/json")
if request.method == 'POST':
print('TODO: Create lab results')

if request.method == "POST":
print("TODO: Create lab results")
return NotImplementedError


@api_view(['GET'])
@api_view(["GET"])
def mmes(request, format=None):
"""Get licensee (MME) data."""

if request.method == 'GET':
if request.method == "GET":
limit = request.query_params.get("limit", None)
if limit:
limit = int(limit)
order_by = request.query_params.get("order_by", "")
# TODO: Get any filters from dict(request.query_params)
# e.g. {"key": "name", "operation": "==", "value": "xyz"}
docs = get_collection('tests/leaf/mmes', order_by=order_by, limit=limit, filters=[])
docs = get_collection(
"tests/leaf/mmes", order_by=order_by, limit=limit, filters=[]
)
return Response(docs, content_type="application/json")

22 changes: 12 additions & 10 deletions cannlytics_api/api/users.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@

from datetime import datetime
from rest_framework import status

# from rest_framework import status
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# from rest_framework import status

from rest_framework.decorators import api_view
from rest_framework.response import Response
from .auth import authenticate
from utils.firebase import get_document, update_document

# from .auth import authenticate
# from utils.firebase import get_document, update_document
Comment on lines +6 to +8
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# from .auth import authenticate
# from utils.firebase import get_document, update_document



@api_view(['GET', 'POST'])
@api_view(["GET", "POST"])
def users(request, format=None):
"""Get, update, or create users."""
# try:
# claims = authenticate(request)
# except:
# return Response({"error": "Could not authenticate."}, status=status.HTTP_400_BAD_REQUEST)
# return Response({"error": "Could not authenticate."},
# status=status.HTTP_400_BAD_REQUEST)

# Get user(s).
if request.method == 'GET':
if request.method == "GET":
print("Getting user...")
# limit = request.query_params.get("limit", None)
# order_by = request.query_params.get("order_by", "state")
# # TODO: Get any filters from dict(request.query_params)
# labs = get_collection('labs', order_by=order_by, limit=limit, filters=[])
user = {}
return Response({ "data": user}, content_type="application/json")
return Response({"data": user}, content_type="application/json")

# Update or create user(s).
elif request.method == 'POST':
elif request.method == "POST":
# TODO: Check if user already exists.
# get_document
print("Creating a user...")
Expand All @@ -39,4 +41,4 @@ def users(request, format=None):
"photo_url": f"https://robohash.org/${email}?set=set5",
}
print(user)
return Response({ "success": True}, content_type="application/json")
return Response({"success": True}, content_type="application/json")
Loading