|
7 | 7 | from .utils import convert_to_array |
8 | 8 | from .presenters import Presenters |
9 | 9 |
|
10 | | -DB = firestore.Client(project=os.environ.get('PROJECT'), database=os.environ.get('DATABASE')) |
| 10 | +DB = firestore.Client( |
| 11 | + project=os.environ.get("PROJECT"), database=os.environ.get("DATABASE") |
| 12 | +) |
11 | 13 |
|
12 | 14 | def list_data(params): |
13 | | - onlyname = False |
14 | | - ref = DB.collection('technologies') |
15 | | - |
16 | | - query = ref |
17 | | - |
18 | | - if 'technology' in params: |
19 | | - arfilters = [] |
20 | | - params_array = convert_to_array(params['technology']) |
21 | | - for tech in params_array: |
22 | | - arfilters.append(FieldFilter('technology', '==', tech)) |
23 | | - |
24 | | - or_filter = Or(filters=arfilters) |
25 | | - |
26 | | - query = query.where(filter=or_filter) |
27 | | - |
28 | | - if 'category' in params: |
29 | | - params_array = convert_to_array(params['category']) |
30 | | - query = query.where(filter=FieldFilter('category_obj', 'array_contains_any', params_array)) |
31 | | - |
32 | | - if 'client' in params: |
33 | | - query = query.where(filter=FieldFilter('client', '==', params['client'])) |
34 | | - |
35 | | - if 'onlyname' in params: |
36 | | - onlyname = True |
37 | | - |
38 | | - if 'sort' not in params: |
39 | | - query = query.order_by('technology', direction=firestore.Query.ASCENDING) |
40 | | - else: |
41 | | - if params['sort'] == 'origins': |
42 | | - query = query.order_by('origins', direction=firestore.Query.DESCENDING) |
43 | | - |
44 | | - |
45 | | - documents = query.stream() |
46 | | - |
47 | | - data = [] |
48 | | - if onlyname and 'client' not in params: |
49 | | - appended_technologies = set() |
50 | | - for doc in documents: |
51 | | - technology = doc.get('technology') |
52 | | - if technology not in appended_technologies: |
53 | | - appended_technologies.add(technology) |
54 | | - data.append(technology) |
55 | | - |
56 | | - else: |
57 | | - for doc in documents: |
58 | | - data.append(Presenters.technology(doc.to_dict())) |
59 | | - |
60 | | - return Result(result=data) |
| 15 | + ref = DB.collection("technologies") |
| 16 | + |
| 17 | + query = ref.order_by("technology", 'asc') |
| 18 | + |
| 19 | + if "technology" in params: |
| 20 | + arfilters = [] |
| 21 | + params_array = convert_to_array(params["technology"]) |
| 22 | + for tech in params_array: |
| 23 | + arfilters.append(FieldFilter("technology", "==", tech)) |
| 24 | + query = query.where(filter=Or(filters=arfilters)) |
| 25 | + |
| 26 | + if "category" in params: |
| 27 | + params_array = convert_to_array(params["category"]) |
| 28 | + query = query.where( |
| 29 | + filter=FieldFilter("category_obj", "array_contains_any", params_array) |
| 30 | + ) |
| 31 | + |
| 32 | + if "client" in params: |
| 33 | + query = query.where(filter=FieldFilter("client", "==", params["client"])) |
| 34 | + |
| 35 | + documents = query.stream() |
| 36 | + data = [] |
| 37 | + |
| 38 | + if "onlyname" in params and "client" not in params: |
| 39 | + appended_tech = set() |
| 40 | + for doc in documents: |
| 41 | + tech = doc.get("technology") |
| 42 | + if tech not in appended_tech: |
| 43 | + appended_tech.add(tech) |
| 44 | + data.append(tech) |
| 45 | + |
| 46 | + else: |
| 47 | + for doc in documents: |
| 48 | + data.append(Presenters.technology(doc.to_dict())) |
| 49 | + |
| 50 | + return Result(result=data) |
0 commit comments