Skip to content

Commit 29bf1c5

Browse files
Merge pull request #1140 from hinashi/feature/search_chain/api_v2
Added advanced search chain apiv2
2 parents 539b5f1 + 1fa628c commit 29bf1c5

File tree

7 files changed

+131
-9
lines changed

7 files changed

+131
-9
lines changed

apiclient/typescript-fetch/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dmm-com/airone-apiclient-typescript-fetch",
3-
"version": "0.0.14",
3+
"version": "0.0.15",
44
"description": "AirOne APIv2 client in TypeScript",
55
"main": "src/autogenerated/index.ts",
66
"scripts": {

entry/api_v2/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
views.EntryAttributeValueRestoreAPI.as_view(),
7575
),
7676
path("advanced_search/", views.AdvancedSearchAPI.as_view()),
77+
path("advanced_search_chain/", views.AdvancedSearchChainAPI.as_view()),
7778
path("import/", views.EntryImportAPI.as_view()),
7879
path("bulk_delete/", views.EntryBulkDeleteAPI.as_view()),
7980
]

entry/api_v2/views.py

+34
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from rest_framework.response import Response
1515

1616
import custom_view
17+
from airone.exceptions import ElasticsearchException
1718
from airone.lib.acl import ACLType
1819
from airone.lib.drf import (
1920
DuplicatedObjectExistsError,
@@ -24,6 +25,7 @@
2425
YAMLParser,
2526
)
2627
from airone.lib.types import AttrType, AttrTypeValue
28+
from api_v1.entry.serializer import EntrySearchChainSerializer
2729
from entity.models import Entity, EntityAttr
2830
from entry.api_v2.pagination import EntryReferralPagination
2931
from entry.api_v2.serializers import (
@@ -510,6 +512,38 @@ def _get_typed_value(type: int) -> str:
510512
return Response(serializer.initial_data)
511513

512514

515+
class AdvancedSearchChainAPI(generics.GenericAPIView):
516+
serializer_class = EntrySearchChainSerializer
517+
"""
518+
NOTE For now, it's just copied from /api/v1/entry/search_chain.
519+
And the AttributeValue is missing from the response.
520+
"""
521+
522+
@extend_schema(
523+
request=EntrySearchChainSerializer,
524+
responses=EntryBaseSerializer(many=True),
525+
)
526+
def post(self, request: Request) -> Response:
527+
serializer = EntrySearchChainSerializer(data=request.data)
528+
serializer.is_valid(raise_exception=True)
529+
530+
try:
531+
(_, ret_data) = serializer.search_entries(request.user)
532+
except ElasticsearchException:
533+
return Response(
534+
{
535+
"reason": (
536+
"Data overflow was happened. " "Please narrow down intermediate conditions"
537+
)
538+
},
539+
status=status.HTTP_400_BAD_REQUEST,
540+
)
541+
542+
entries = Entry.objects.filter(id__in=[x["id"] for x in ret_data])
543+
544+
return Response(EntryBaseSerializer(entries, many=True).data)
545+
546+
513547
class AdvancedSearchResultAPI(generics.GenericAPIView):
514548
serializer_class = AdvancedSearchResultExportSerializer
515549

entry/tests/test_api_v2.py

+70
Original file line numberDiff line numberDiff line change
@@ -4277,6 +4277,76 @@ def test_export_with_all_entities(self):
42774277
],
42784278
)
42794279

4280+
def test_advanced_search_chain(self):
4281+
ref_entry = self.add_entry(self.user, "RefEntry", self.ref_entity, values={"val": "hoge"})
4282+
entry = self.add_entry(
4283+
self.user,
4284+
"Entry",
4285+
self.entity,
4286+
values={
4287+
"ref": ref_entry.id,
4288+
},
4289+
)
4290+
4291+
params = {
4292+
"entities": [self.entity.id],
4293+
"attrs": [{"name": "ref", "attrs": [{"name": "val", "value": "hoge"}]}],
4294+
}
4295+
resp = self.client.post(
4296+
"/entry/api/v2/advanced_search_chain/", json.dumps(params), "application/json"
4297+
)
4298+
self.assertEqual(resp.status_code, 200)
4299+
self.assertEqual(
4300+
resp.json(),
4301+
[
4302+
{
4303+
"id": entry.id,
4304+
"name": "Entry",
4305+
"schema": {
4306+
"id": entry.schema.id,
4307+
"name": "test-entity",
4308+
"is_public": True,
4309+
},
4310+
"is_active": True,
4311+
"deleted_user": None,
4312+
"deleted_time": None,
4313+
"updated_time": entry.updated_time.astimezone(self.TZ_INFO).isoformat(),
4314+
},
4315+
],
4316+
)
4317+
4318+
# empty result case
4319+
params = {
4320+
"entities": [self.entity.id],
4321+
"attrs": [{"name": "ref", "attrs": [{"name": "val", "value": "fuga"}]}],
4322+
}
4323+
resp = self.client.post(
4324+
"/entry/api/v2/advanced_search_chain/", json.dumps(params), "application/json"
4325+
)
4326+
self.assertEqual(resp.status_code, 200)
4327+
self.assertEqual(resp.json(), [])
4328+
4329+
# bad request case
4330+
params = {
4331+
"entities": [self.entity.id],
4332+
"attrs": [{"name": "ref", "attrs": [{"value": "fuga"}]}],
4333+
}
4334+
resp = self.client.post(
4335+
"/entry/api/v2/advanced_search_chain/", json.dumps(params), "application/json"
4336+
)
4337+
self.assertEqual(resp.status_code, 400)
4338+
self.assertEqual(
4339+
resp.json(),
4340+
{
4341+
"non_field_errors": [
4342+
{
4343+
"code": "AE-121000",
4344+
"message": "Invalid condition({'value': 'fuga'}) was specified",
4345+
}
4346+
]
4347+
},
4348+
)
4349+
42804350
def test_entry_history(self):
42814351
values = {
42824352
"val": {"value": "hoge", "result": {"as_string": "hoge"}},

frontend/src/repository/AironeApiClient.ts

+17
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
EntryBase,
1919
EntryCopy,
2020
EntryRetrieve,
21+
EntrySearchChain,
2122
GetEntryAttrReferral,
2223
Group,
2324
GroupApi,
@@ -709,6 +710,22 @@ class AironeApiClient {
709710
);
710711
}
711712

713+
async advancedSearchChain(
714+
entrySearchChain: EntrySearchChain
715+
): Promise<EntryBase[]> {
716+
return await this.entry.entryApiV2AdvancedSearchChainCreate(
717+
{
718+
entrySearchChain: entrySearchChain,
719+
},
720+
{
721+
headers: {
722+
"X-CSRFToken": getCsrfToken(),
723+
"Content-Type": "application/json;charset=utf-8",
724+
},
725+
}
726+
);
727+
}
728+
712729
async exportAdvancedSearchResults(
713730
entityIds: number[],
714731
attrinfo: Array<AdvancedSearchResultAttrInfo>,

package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"zod": "^3.22.4"
7979
},
8080
"dependencies": {
81-
"@dmm-com/airone-apiclient-typescript-fetch": "^0.0.14",
81+
"@dmm-com/airone-apiclient-typescript-fetch": "^0.0.15",
8282
"@emotion/react": "^11.10.5",
8383
"@emotion/styled": "^11.10.5",
8484
"@jest/globals": "^27.0.6",

0 commit comments

Comments
 (0)