-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from HTTPArchive/development
New version
- Loading branch information
Showing
7 changed files
with
84 additions
and
31 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
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
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
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
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,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'] | ||
} |
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 |
---|---|---|
@@ -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) |
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