Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: #91 applying filters criteria on query parameter
  • Loading branch information
enriquezrene committed Apr 23, 2020
commit beb8c84f36be17eac9e88979211cb01ec53573df
10 changes: 9 additions & 1 deletion time_tracker_api/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from azure.cosmos.exceptions import CosmosResourceExistsError, CosmosResourceNotFoundError, CosmosHttpResponseError
from faker import Faker
from flask import current_app as app
from flask_restplus import Api, fields
from flask_restplus import Api, fields, reqparse
from flask_restplus._http import HTTPStatus

from commons.data_access_layer.cosmos_db import CustomError
Expand All @@ -18,6 +18,14 @@
security="TimeTracker JWT",
)

# Filters
def create_attributes_filter(attributes_filter):
filter_attributes_parser = reqparse.RequestParser()
for attribute in attributes_filter:
filter_attributes_parser.add_argument(f'filters[{attribute}]', location='args')

return filter_attributes_parser

# For matching UUIDs
UUID_REGEX = '[0-9a-f]{8}\-[0-9a-f]{4}\-4[0-9a-f]{3}\-[89ab][0-9a-f]{3}\-[0-9a-f]{12}'

Expand Down
16 changes: 16 additions & 0 deletions time_tracker_api/collections/dictionary_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import re

def remove_none_values(dictionary):
dictionary_with_values = {}
for key, value in dictionary.items():
if value is not None:
dictionary_with_values.update({key: value})
return dictionary_with_values


def remove_filters_wrapper_from_keys(dictionary):
dictionary_with_unwrapped_keys = {}
for key, value in dictionary.items():
unwrapped_key = re.search('\\[(.+?)\\]', key).groups()[0]
dictionary_with_unwrapped_keys.update({unwrapped_key: value})
return dictionary_with_unwrapped_keys
10 changes: 7 additions & 3 deletions time_tracker_api/projects/projects_namespace.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from faker import Faker
from flask_restplus import Namespace, Resource, fields
from flask_restplus._http import HTTPStatus
from flask import request

from time_tracker_api.api import common_fields, UUID_REGEX
from time_tracker_api.api import common_fields, UUID_REGEX, create_attributes_filter

from time_tracker_api.collections.dictionary_utils import remove_none_values, remove_filters_wrapper_from_keys
from time_tracker_api.projects.projects_model import create_dao

faker = Faker()
Expand Down Expand Up @@ -55,14 +56,17 @@

project_dao = create_dao()

attributes_filter = create_attributes_filter(['customer_id'])

@ns.route('')
class Projects(Resource):
@ns.doc('list_projects')
@ns.marshal_list_with(project)
@ns.expect(attributes_filter)
def get(self):
"""List all projects"""
return project_dao.get_all(conditions=request.args)
conditions = remove_none_values(attributes_filter.parse_args())
return project_dao.get_all(conditions=remove_filters_wrapper_from_keys(conditions))

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