From 281828761788ce19414416fbd4e8e9b761d9448b Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Wed, 6 Dec 2023 21:37:40 -0500 Subject: [PATCH 1/4] technologies added params and onlyname --- functions/technologies/libs/presenters.py | 12 ++++++++ functions/technologies/libs/queries.py | 34 +++++++++++++++-------- functions/technologies/libs/validator.py | 4 +-- 3 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 functions/technologies/libs/presenters.py 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..c59170c 100644 --- a/functions/technologies/libs/queries.py +++ b/functions/technologies/libs/queries.py @@ -1,35 +1,47 @@ 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): + onlyname = False ref = DB.collection(u'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 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) From afb43692fdc7697db6530cb706006c1ff73a2b94 Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Thu, 21 Dec 2023 10:25:22 -0600 Subject: [PATCH 2/4] fixed asc desc query --- functions/technologies/libs/queries.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/functions/technologies/libs/queries.py b/functions/technologies/libs/queries.py index c59170c..f84c2d9 100644 --- a/functions/technologies/libs/queries.py +++ b/functions/technologies/libs/queries.py @@ -12,7 +12,7 @@ def list_data(params): onlyname = False - ref = DB.collection(u'technologies') + ref = DB.collection('technologies') query = ref @@ -26,7 +26,6 @@ def list_data(params): query = query.where(filter=or_filter) - if 'category' in params: params_array = convert_to_array(params['category']) query = query.where(filter=FieldFilter('category_obj', 'array_contains_any', params_array)) @@ -34,6 +33,13 @@ def list_data(params): 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 = [] From 39b9f19fe79c3f72bb3883506e55147bb1edf9c2 Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Sat, 10 Feb 2024 09:36:19 -0500 Subject: [PATCH 3/4] refactoring preflight and cors --- functions/adoption/libs/network.py | 28 ++++++++++++++++++++++++++++ functions/adoption/main.py | 15 ++------------- 2 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 functions/adoption/libs/network.py 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": "*"} From 8ea3b4419a21f9eb0518166ba88b2aaffb39fcc5 Mon Sep 17 00:00:00 2001 From: Martin Aceto Date: Sat, 10 Feb 2024 09:36:51 -0500 Subject: [PATCH 4/4] categories filter no mandatory --- functions/categories/libs/queries.py | 8 +++++++- functions/categories/libs/validator.py | 6 +++--- 2 files changed, 10 insertions(+), 4 deletions(-) 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)