Skip to content

Commit beb8c84

Browse files
committed
fix: #91 applying filters criteria on query parameter
1 parent a1b1a6a commit beb8c84

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

time_tracker_api/api.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from azure.cosmos.exceptions import CosmosResourceExistsError, CosmosResourceNotFoundError, CosmosHttpResponseError
22
from faker import Faker
33
from flask import current_app as app
4-
from flask_restplus import Api, fields
4+
from flask_restplus import Api, fields, reqparse
55
from flask_restplus._http import HTTPStatus
66

77
from commons.data_access_layer.cosmos_db import CustomError
@@ -18,6 +18,14 @@
1818
security="TimeTracker JWT",
1919
)
2020

21+
# Filters
22+
def create_attributes_filter(attributes_filter):
23+
filter_attributes_parser = reqparse.RequestParser()
24+
for attribute in attributes_filter:
25+
filter_attributes_parser.add_argument(f'filters[{attribute}]', location='args')
26+
27+
return filter_attributes_parser
28+
2129
# For matching UUIDs
2230
UUID_REGEX = '[0-9a-f]{8}\-[0-9a-f]{4}\-4[0-9a-f]{3}\-[89ab][0-9a-f]{3}\-[0-9a-f]{12}'
2331

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import re
2+
3+
def remove_none_values(dictionary):
4+
dictionary_with_values = {}
5+
for key, value in dictionary.items():
6+
if value is not None:
7+
dictionary_with_values.update({key: value})
8+
return dictionary_with_values
9+
10+
11+
def remove_filters_wrapper_from_keys(dictionary):
12+
dictionary_with_unwrapped_keys = {}
13+
for key, value in dictionary.items():
14+
unwrapped_key = re.search('\\[(.+?)\\]', key).groups()[0]
15+
dictionary_with_unwrapped_keys.update({unwrapped_key: value})
16+
return dictionary_with_unwrapped_keys

time_tracker_api/projects/projects_namespace.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from faker import Faker
22
from flask_restplus import Namespace, Resource, fields
33
from flask_restplus._http import HTTPStatus
4-
from flask import request
54

6-
from time_tracker_api.api import common_fields, UUID_REGEX
5+
from time_tracker_api.api import common_fields, UUID_REGEX, create_attributes_filter
6+
7+
from time_tracker_api.collections.dictionary_utils import remove_none_values, remove_filters_wrapper_from_keys
78
from time_tracker_api.projects.projects_model import create_dao
89

910
faker = Faker()
@@ -55,14 +56,17 @@
5556

5657
project_dao = create_dao()
5758

59+
attributes_filter = create_attributes_filter(['customer_id'])
5860

5961
@ns.route('')
6062
class Projects(Resource):
6163
@ns.doc('list_projects')
6264
@ns.marshal_list_with(project)
65+
@ns.expect(attributes_filter)
6366
def get(self):
6467
"""List all projects"""
65-
return project_dao.get_all(conditions=request.args)
68+
conditions = remove_none_values(attributes_filter.parse_args())
69+
return project_dao.get_all(conditions=remove_filters_wrapper_from_keys(conditions))
6670

6771
@ns.doc('create_project')
6872
@ns.response(HTTPStatus.CONFLICT, 'This project already exists')

0 commit comments

Comments
 (0)