Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable to get items considering with Alias name #1355

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## In development

### Added
* Enable to get items considering with Alias name (by simple solution)
Contributed by @userlocalhost

### Changed

Expand Down
2 changes: 1 addition & 1 deletion apiclient/typescript-fetch/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dmm-com/airone-apiclient-typescript-fetch",
"version": "0.2.2",
"version": "0.3.0",
"description": "AirOne APIv2 client in TypeScript",
"main": "src/autogenerated/index.ts",
"scripts": {
Expand Down
20 changes: 19 additions & 1 deletion entity/api_v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,29 @@ def destroy(self, request: Request, *args, **kwargs) -> Response:
return Response(status=status.HTTP_202_ACCEPTED)


class AliasSearchFilter(filters.SearchFilter):
def get_search_fields(self, view, request):
original_fields = super().get_search_fields(view, request)

# update search_fields when "with_alias" parameter was specified
# to consier aliases that are related with target item
# filtered by specified "search" parameter
if request.query_params.get("with_alias"):
return original_fields + ["aliases__name"]
else:
return original_fields


@extend_schema(
parameters=[
OpenApiParameter("with_alias", OpenApiTypes.STR, OpenApiParameter.QUERY),
Copy link
Member Author

Choose a reason for hiding this comment

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

It would be better to use OpenApiTypes.BOOL instead of OpenApiTypes.STR

Copy link
Member Author

Choose a reason for hiding this comment

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

On second thought, OpenApiTypes.STR is better than BOOL because this is a GET parameter.

The request.query_params.get("with_alias") contains string typed value even through extend_schema configuration.

        if request.query_params.get("with_alias"):
            return original_fields + ["aliases__name"]
        else:
            return original_fields

(c.f. https://github.com/dmm-com/pagoda/pull/1355/files#diff-1322718f3873c6b8312fafb3c1eab3172964dd9a3eeeacd3ce3e5f39fadb01bfR202)

So that condition would be True when user specified with_alias=false.

],
)
class EntityEntryAPI(viewsets.ModelViewSet):
queryset = Entry.objects.all()
pagination_class = PageNumberPagination
permission_classes = [IsAuthenticated & EntityPermission]
filter_backends = [DjangoFilterBackend, filters.OrderingFilter, filters.SearchFilter]
filter_backends = [DjangoFilterBackend, filters.OrderingFilter, AliasSearchFilter]
filterset_fields = ["is_active"]
ordering_fields = ["name", "updated_time"]
search_fields = ["name"]
Expand Down
17 changes: 17 additions & 0 deletions entity/tests/test_api_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2902,6 +2902,23 @@ def test_list_entry_with_invalid_param(self):
self.assertEqual(resp.status_code, 404)
self.assertEqual(resp.json(), {"code": "AE-230000", "message": "Not found."})

def test_list_entry_when_alias_is_related(self):
# create items and set alias at one of them
ALIAS_NAME = "test alias"
items = [self.add_entry(self.user, "e-%i" % i, self.entity) for i in range(3)]
items[0].add_alias(ALIAS_NAME)

# search item by alias name
resp = self.client.get(
"/entity/api/v2/%d/entries/?search=%s&with_alias=1" % (self.entity.id, ALIAS_NAME)
)
self.assertEqual(resp.status_code, 200)

# then only item that alias is set was returned
self.assertEqual(resp.json()["count"], 1)
self.assertEqual(resp.json()["results"][0]["name"], "e-0")
self.assertEqual(resp.json()["results"][0]["aliases"][0]["name"], ALIAS_NAME)

@mock.patch("entry.tasks.create_entry_v2.delay", mock.Mock(side_effect=create_entry_v2))
def test_create_entry(self):
attr = {}
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/pages/ListAliasEntryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ export const ListAliasEntryPage: FC = ({}) => {
}, [entityId]);

useEffect(() => {
aironeApiClient.getEntries(entityId, true, page, query).then((res) => {
setEntries(res.results);
setTotalCount(res.count);
});
aironeApiClient
.getEntries(entityId, true, page, query, true)
.then((res) => {
setEntries(res.results);
setTotalCount(res.count);
});
}, [page, query]);

const handleChangeQuery = (newQuery?: string) => {
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/repository/AironeApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,10 @@
const groupTrees = await this.group.groupApiV2GroupsTreeList();

// typing children here because API side type definition will break API client generation.
const toTyped = (groupTree: { [key: string]: any }): GroupTree => ({

Check warning on line 523 in frontend/src/repository/AironeApiClient.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
id: groupTree["id"],
name: groupTree["name"],
children: groupTree["children"].map((child: { [key: string]: any }) =>

Check warning on line 526 in frontend/src/repository/AironeApiClient.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
toTyped(child)
),
});
Expand All @@ -531,7 +531,7 @@
return groupTrees.map((groupTree) => ({
id: groupTree.id,
name: groupTree.name,
children: groupTree.children.map((child: { [key: string]: any }) =>

Check warning on line 534 in frontend/src/repository/AironeApiClient.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
toTyped(child)
),
}));
Expand Down Expand Up @@ -624,7 +624,8 @@
entityId: number,
isActive = true,
pageNumber = 1,
keyword: string
keyword: string,
withAlias?: boolean
): Promise<PaginatedEntryBaseList> {
//return await this.entry.entryApiV2EntriesList(entityId, isActive, pageNumber);
// ToDo: This method must pass "isActive" parameter by manupirating DRF API's declaration.
Expand All @@ -634,6 +635,7 @@
isActive: isActive,
search: keyword,
ordering: "name",
withAlias: withAlias ? "1" : "",
});
}

Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@babel/preset-env": "^7.12.11",
"@babel/preset-react": "^7.12.10",
"@date-io/date-fns": "^1.3.13",
"@dmm-com/airone-apiclient-typescript-fetch": "^0.2.2",
"@dmm-com/airone-apiclient-typescript-fetch": "^0.3.0",
"@dnd-kit/core": "^6.1.0",
"@dnd-kit/modifiers": "^7.0.0",
"@dnd-kit/sortable": "^8.0.0",
Expand Down
Loading