diff --git a/functions/adoption/libs/network.py b/functions/adoption/libs/network.py new file mode 100644 index 0000000..59532bc --- /dev/null +++ b/functions/adoption/libs/network.py @@ -0,0 +1,28 @@ + +""" +Network + +Handles formatting responses to match the tuple pattern required by +the flask/GCP wrapper for Cloud Functions. +""" + +PREFLIGHT_HEADERS = { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Methods": "GET", + "Access-Control-Allow-Headers": "Content-Type", + "Access-Control-Max-Age": "3600", + } + +HEADERS = {"Access-Control-Allow-Origin": "*", "Content-Type": "application/json"} + +def respond_cors(): + """ + To be used to return OPTIONS responses to satisfy CORS preflight requests. + """ + return ("", 204, PREFLIGHT_HEADERS) + +def respond(data, status=200): + """ + To be used to return responses to satisfy CORS requests. + """ + return (data, status, HEADERS) diff --git a/functions/adoption/main.py b/functions/adoption/main.py index b7e43b0..5f7148f 100644 --- a/functions/adoption/main.py +++ b/functions/adoption/main.py @@ -2,24 +2,13 @@ from .libs.validator import Validator from .libs.utils import output from .libs.queries import list_data +from .libs.network import respond_cors, respond @functions_framework.http def dispatcher(request): - # For more information about CORS and CORS preflight requests, see: - # https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request - # Set CORS headers for the preflight request if request.method == "OPTIONS": - # Allows GET requests from any origin with the Content-Type - # header and caches preflight response for an 3600s - headers = { - "Access-Control-Allow-Origin": "*", - "Access-Control-Allow-Methods": "GET", - "Access-Control-Allow-Headers": "Content-Type", - "Access-Control-Max-Age": "3600", - } - - return ("", 204, headers) + return respond_cors() # Set CORS headers for the main request headers = {"Access-Control-Allow-Origin": "*"} diff --git a/functions/categories/libs/queries.py b/functions/categories/libs/queries.py index 3cff327..b6f94ac 100644 --- a/functions/categories/libs/queries.py +++ b/functions/categories/libs/queries.py @@ -27,8 +27,14 @@ def list_data(params): category_array = convert_to_array(params['category']) for category in category_array: - results = DB.collection(u'categories').where("category", "==", category).stream() + results = query.where("category", "==", category).stream() for doc in results: data.append(doc.to_dict()) + else: + documents = query.stream() + + for doc in documents: + data.append(doc.to_dict()) + return Result(result=data) diff --git a/functions/categories/libs/validator.py b/functions/categories/libs/validator.py index 836d0cd..ff19ca9 100644 --- a/functions/categories/libs/validator.py +++ b/functions/categories/libs/validator.py @@ -9,9 +9,9 @@ def __init__(self, params): def validate(self): result = Result(status="ok", result="()") - if 'onlyname' not in self.params: - if 'category' not in self.params: - self.add_error("category", "missing category parameter") + # if 'onlyname' not in self.params: + # if 'category' not in self.params: + # self.add_error("category", "missing category parameter") return Result(errors=self.errors, result=self.params) diff --git a/functions/technologies/libs/presenters.py b/functions/technologies/libs/presenters.py new file mode 100644 index 0000000..c4b03bf --- /dev/null +++ b/functions/technologies/libs/presenters.py @@ -0,0 +1,12 @@ + +class Presenters: + @staticmethod + def technology(item): + return { + 'client': item['client'], + 'similar_technologies': item['similar_technologies'], + 'description': item['description'], + 'origins': item['origins'], + 'technology': item['technology'], + 'category': item['category'] + } diff --git a/functions/technologies/libs/queries.py b/functions/technologies/libs/queries.py index 9c8386f..f84c2d9 100644 --- a/functions/technologies/libs/queries.py +++ b/functions/technologies/libs/queries.py @@ -1,35 +1,53 @@ import os import json from google.cloud import firestore +from google.cloud.firestore_v1.base_query import FieldFilter, Or + from .result import Result from .utils import convert_to_array +from .presenters import Presenters + DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE')) def list_data(params): - ref = DB.collection(u'technologies') + onlyname = False + ref = DB.collection('technologies') query = ref - if 'start' in params: - query = query.where('date', '>=', params['start']) - if 'end' in params: - query = query.where('date', '<=', params['end']) - if 'geo' in params: - query = query.where('geo', '==', params['geo']) if 'technology' in params: + arfilters = [] params_array = convert_to_array(params['technology']) - query = query.where('technology', 'in', params_array) - if 'rank' in params: - query = query.where('rank', '==', params['rank']) + for tech in params_array: + arfilters.append(FieldFilter('technology', '==', tech)) + + or_filter = Or(filters=arfilters) + + query = query.where(filter=or_filter) + if 'category' in params: params_array = convert_to_array(params['category']) - query = query.where('category', 'in', params_array) + query = query.where(filter=FieldFilter('category_obj', 'array_contains_any', params_array)) + + if 'onlyname' in params: + onlyname = True + + if 'sort' not in params: + query = query.order_by('technology', direction=firestore.Query.ASCENDING) + else: + if params['sort'] == 'origins': + query = query.order_by('origins', direction=firestore.Query.DESCENDING) + documents = query.stream() data = [] for doc in documents: - data.append(doc.to_dict()) + item = doc.to_dict() + if onlyname: + data.append(item['technology']) + else: + data.append(Presenters.technology(doc.to_dict())) return Result(result=data) diff --git a/functions/technologies/libs/validator.py b/functions/technologies/libs/validator.py index a9864da..29d6a89 100644 --- a/functions/technologies/libs/validator.py +++ b/functions/technologies/libs/validator.py @@ -9,8 +9,8 @@ def __init__(self, params): def validate(self): result = Result(status="ok", result="()") - if 'technology' not in self.params: - self.add_error("technology", "missing technology parameter") + # if 'technology' not in self.params: + # self.add_error("technology", "missing technology parameter") return Result(errors=self.errors, result=self.params)