Skip to content
This repository has been archived by the owner on Feb 21, 2019. It is now read-only.

Feature/806 add label filter and sort content by label in contents endpoint #45

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions backend/tracim_backend/lib/core/content.py
Expand Up @@ -884,7 +884,9 @@ def _get_all_query(
self,
parent_id: int = None,
content_type_slug: str = CONTENT_TYPES.Any_SLUG,
workspace: Workspace = None
workspace: Workspace = None,
label:str = None,
order_by_label=False,
) -> Query:
"""
Extended filter for better "get all data" query
Expand All @@ -910,11 +912,22 @@ def _get_all_query(
resultset = resultset.filter(Content.parent_id==parent_id)
if parent_id == 0 or parent_id is False:
resultset = resultset.filter(Content.parent_id == None)
if label:
resultset = resultset.filter(Content.label.ilike('%{}%'.format(label))) # nopep8

if order_by_label:
resultset = resultset.order_by(Content.label)
return resultset

def get_all(self, parent_id: int=None, content_type: str=CONTENT_TYPES.Any_SLUG, workspace: Workspace=None) -> typing.List[Content]:
return self._get_all_query(parent_id, content_type, workspace).all()
def get_all(
self,
parent_id: int=None,
content_type: str=CONTENT_TYPES.Any_SLUG,
workspace: Workspace=None,
label: str=None,
order_by_label=False
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about using something like order_by_property with a default value to None and:

if order_by_property:
  resultset = resultset.order_by(order_by_property) ?

The usage would be get_all(..., order_by_property=Content.label)?

@buxx @inkhey what do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

) -> typing.List[Content]:
return self._get_all_query(parent_id, content_type, workspace, label, order_by_label).all()

# TODO - G.M - 2018-07-17 - [Cleanup] Drop this method if unneeded
# def get_children(self, parent_id: int, content_types: list, workspace: Workspace=None) -> typing.List[Content]:
Expand Down
2 changes: 2 additions & 0 deletions backend/tracim_backend/models/context_models.py
Expand Up @@ -228,6 +228,7 @@ def __init__(
show_deleted: int = 0,
show_active: int = 1,
content_type: str = None,
label: str = None,
offset: int = None,
limit: int = None,
) -> None:
Expand All @@ -238,6 +239,7 @@ def __init__(
self.show_active = bool(show_active)
self.limit = limit
self.offset = offset
self.label = label
self.content_type = content_type


Expand Down
132 changes: 85 additions & 47 deletions backend/tracim_backend/tests/functional/test_workspaces.py
Expand Up @@ -1681,18 +1681,16 @@ def test_api__get_workspace_content__ok_200__get_default(self):
# TODO - G.M - 30-05-2018 - Check this test
assert len(res) == 3
content = res[0]
assert content['content_id'] == 1
assert content['content_type'] == 'folder'
assert content['content_id'] == 11
assert content['content_type'] == 'html-document'
assert content['is_archived'] is False
assert content['is_deleted'] is False
assert content['label'] == 'Tools'
assert content['parent_id'] is None
assert content['label'] == 'Current Menu'
assert content['parent_id'] == 2
assert content['show_in_ui'] is True
assert content['slug'] == 'tools'
assert content['slug'] == 'current-menu'
assert content['status'] == 'open'
assert len(content['sub_content_types']) > 1
assert 'comment' in content['sub_content_types']
assert 'folder' in content['sub_content_types']
assert set(content['sub_content_types']) == {'comment'}
assert content['workspace_id'] == 1
content = res[1]
assert content['content_id'] == 2
Expand All @@ -1709,16 +1707,18 @@ def test_api__get_workspace_content__ok_200__get_default(self):
assert 'folder' in content['sub_content_types']
assert content['workspace_id'] == 1
content = res[2]
assert content['content_id'] == 11
assert content['content_type'] == 'html-document'
assert content['content_id'] == 1
assert content['content_type'] == 'folder'
assert content['is_archived'] is False
assert content['is_deleted'] is False
assert content['label'] == 'Current Menu'
assert content['parent_id'] == 2
assert content['label'] == 'Tools'
assert content['parent_id'] is None
assert content['show_in_ui'] is True
assert content['slug'] == 'current-menu'
assert content['slug'] == 'tools'
assert content['status'] == 'open'
assert set(content['sub_content_types']) == {'comment'}
assert len(content['sub_content_types']) > 1
assert 'comment' in content['sub_content_types']
assert 'folder' in content['sub_content_types']
assert content['workspace_id'] == 1

def test_api__get_workspace_content__ok_200__get_default_html_documents(self):
Expand Down Expand Up @@ -1777,20 +1777,20 @@ def test_api__get_workspace_content__ok_200__get_all_root_content__legacy_html_s
).json_body # nopep8
# TODO - G.M - 30-05-2018 - Check this test
assert len(res) == 4
content = res[1]
content = res[0]
assert content['content_type'] == 'html-document'
assert content['content_id'] == 15
assert content['content_id'] == 17
assert content['is_archived'] is False
assert content['is_deleted'] is False
assert content['label'] == 'New Fruit Salad'
assert content['is_deleted'] is True
assert content['label'].startswith('Bad Fruit Salad')
assert content['parent_id'] is None
assert content['show_in_ui'] is True
assert content['slug'] == 'new-fruit-salad'
assert content['slug'].startswith('bad-fruit-salad')
assert content['status'] == 'open'
assert set(content['sub_content_types']) == {'comment'}
assert content['workspace_id'] == 3

content = res[2]
content = res[1]
assert content['content_type'] == 'html-document'
assert content['content_id'] == 16
assert content['is_archived'] is True
Expand All @@ -1805,13 +1805,13 @@ def test_api__get_workspace_content__ok_200__get_all_root_content__legacy_html_s

content = res[3]
assert content['content_type'] == 'html-document'
assert content['content_id'] == 17
assert content['content_id'] == 15
assert content['is_archived'] is False
assert content['is_deleted'] is True
assert content['label'].startswith('Bad Fruit Salad')
assert content['is_deleted'] is False
assert content['label'] == 'New Fruit Salad'
assert content['parent_id'] is None
assert content['show_in_ui'] is True
assert content['slug'].startswith('bad-fruit-salad')
assert content['slug'] == 'new-fruit-salad'
assert content['status'] == 'open'
assert set(content['sub_content_types']) == {'comment'}
assert content['workspace_id'] == 3
Expand Down Expand Up @@ -1840,20 +1840,20 @@ def test_api__get_workspace_content__ok_200__get_all_root_content(self):
).json_body # nopep8
# TODO - G.M - 30-05-2018 - Check this test
assert len(res) == 4
content = res[1]
content = res[0]
assert content['content_type'] == 'html-document'
assert content['content_id'] == 15
assert content['content_id'] == 17
assert content['is_archived'] is False
assert content['is_deleted'] is False
assert content['label'] == 'New Fruit Salad'
assert content['is_deleted'] is True
assert content['label'].startswith('Bad Fruit Salad')
assert content['parent_id'] is None
assert content['show_in_ui'] is True
assert content['slug'] == 'new-fruit-salad'
assert content['slug'].startswith('bad-fruit-salad')
assert content['status'] == 'open'
assert set(content['sub_content_types']) == {'comment'}
assert content['workspace_id'] == 3

content = res[2]
content = res[1]
assert content['content_type'] == 'html-document'
assert content['content_id'] == 16
assert content['is_archived'] is True
Expand All @@ -1868,13 +1868,51 @@ def test_api__get_workspace_content__ok_200__get_all_root_content(self):

content = res[3]
assert content['content_type'] == 'html-document'
assert content['content_id'] == 17
assert content['content_id'] == 15
assert content['is_archived'] is False
assert content['is_deleted'] is True
assert content['label'].startswith('Bad Fruit Salad')
assert content['is_deleted'] is False
assert content['label'] == 'New Fruit Salad'
assert content['parent_id'] is None
assert content['show_in_ui'] is True
assert content['slug'].startswith('bad-fruit-salad')
assert content['slug'] == 'new-fruit-salad'
assert content['status'] == 'open'
assert set(content['sub_content_types']) == {'comment'}
assert content['workspace_id'] == 3

def test_api__get_workspace_content__ok_200__get_all_root_content_filter_by_label(self): # nopep8
"""
Check obtain workspace all root contents
"""
params = {
'parent_id': 0,
'show_archived': 1,
'show_deleted': 1,
'show_active': 1,
'label': 'ew'
}
self.testapp.authorization = (
'Basic',
(
'bob@fsf.local',
'foobarbaz'
)
)
res = self.testapp.get(
'/api/v2/workspaces/3/contents',
status=200,
params=params,
).json_body # nopep8
# TODO - G.M - 30-05-2018 - Check this test
assert len(res) == 1
content = res[0]
assert content['content_type'] == 'html-document'
assert content['content_id'] == 15
assert content['is_archived'] is False
assert content['is_deleted'] is False
assert content['label'] == 'New Fruit Salad'
assert content['parent_id'] is None
assert content['show_in_ui'] is True
assert content['slug'] == 'new-fruit-salad'
assert content['status'] == 'open'
assert set(content['sub_content_types']) == {'comment'}
assert content['workspace_id'] == 3
Expand Down Expand Up @@ -2201,26 +2239,26 @@ def test_api__get_workspace_content__ok_200__get_all_filter_content_html_and_leg
assert content['content_id']
assert content['is_archived'] is False
assert content['is_deleted'] is False
assert content['label'] == 'test_page'
assert content['label'] == 'test_html_page'
assert content['parent_id'] == 1
assert content['show_in_ui'] is True
assert content['slug'] == 'test-page'
assert content['slug'] == 'test-html-page'
assert content['status'] == 'open'
assert set(content['sub_content_types']) == {'comment'} # nopep8
assert content['workspace_id'] == 1
assert res[0]['content_id'] != res[1]['content_id']
content = res[1]
assert content['content_type'] == 'html-document'
assert content['content_id']
assert content['is_archived'] is False
assert content['is_deleted'] is False
assert content['label'] == 'test_html_page'
assert content['label'] == 'test_page'
assert content['parent_id'] == 1
assert content['show_in_ui'] is True
assert content['slug'] == 'test-html-page'
assert content['slug'] == 'test-page'
assert content['status'] == 'open'
assert set(content['sub_content_types']) == {'comment'} # nopep8
assert content['workspace_id'] == 1
assert res[0]['content_id'] != res[1]['content_id']

def test_api__get_workspace_content__ok_200__get_all_folder_content(self):
"""
Expand Down Expand Up @@ -2248,13 +2286,13 @@ def test_api__get_workspace_content__ok_200__get_all_folder_content(self):
assert len(res) == 3
content = res[0]
assert content['content_type'] == 'html-document'
assert content['content_id'] == 12
assert content['content_id'] == 14
assert content['is_archived'] is False
assert content['is_deleted'] is False
assert content['label'] == 'New Fruit Salad'
assert content['is_deleted'] is True
assert content['label'].startswith('Bad Fruit Salad')
assert content['parent_id'] == 10
assert content['show_in_ui'] is True
assert content['slug'] == 'new-fruit-salad'
assert content['slug'].startswith('bad-fruit-salad')
assert content['status'] == 'open'
assert set(content['sub_content_types']) == {'comment'} # nopep8
assert content['workspace_id'] == 2
Expand All @@ -2274,13 +2312,13 @@ def test_api__get_workspace_content__ok_200__get_all_folder_content(self):

content = res[2]
assert content['content_type'] == 'html-document'
assert content['content_id'] == 14
assert content['content_id'] == 12
assert content['is_archived'] is False
assert content['is_deleted'] is True
assert content['label'].startswith('Bad Fruit Salad')
assert content['is_deleted'] is False
assert content['label'] == 'New Fruit Salad'
assert content['parent_id'] == 10
assert content['show_in_ui'] is True
assert content['slug'].startswith('bad-fruit-salad')
assert content['slug'] == 'new-fruit-salad'
assert content['status'] == 'open'
assert set(content['sub_content_types']) == {'comment'} # nopep8
assert content['workspace_id'] == 2
Expand Down
6 changes: 6 additions & 0 deletions backend/tracim_backend/views/core_api/schemas.py
Expand Up @@ -421,6 +421,12 @@ class FilterContentQuerySchema(marshmallow.Schema):
default=CONTENT_TYPES.Any_SLUG,
validate=all_content_types_validator
)
label = marshmallow.fields.String(
example='myfilename',
default=None,
allow_none=True,
description='Filter by content label'
)

@post_load
def make_content_filter(self, data):
Expand Down
2 changes: 2 additions & 0 deletions backend/tracim_backend/views/core_api/workspace_controller.py
Expand Up @@ -386,6 +386,8 @@ def workspace_content(
parent_id=content_filter.parent_id,
workspace=request.current_workspace,
content_type=content_filter.content_type or CONTENT_TYPES.Any_SLUG,
label=content_filter.label,
order_by_label=True
)
contents = [
api.get_content_in_context(content) for content in contents
Expand Down