Skip to content

Commit

Permalink
Merge pull request #1355 from userlocalhost/feature/entity/api/consie…
Browse files Browse the repository at this point in the history
…ring_with_alias

Enable to get items considering with Alias name
  • Loading branch information
hinashi authored Jan 22, 2025
2 parents a41eb04 + 6c021b2 commit 27fa5ff
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 15 deletions.
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),
],
)
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 @@ -624,7 +624,8 @@ class AironeApiClient {
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 @@ class AironeApiClient {
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

0 comments on commit 27fa5ff

Please sign in to comment.