-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #748 from petridishdev/feature/v4-search-autocomplete
feat: temporarily re-introduce v4/search/autocomplete
- Loading branch information
Showing
5 changed files
with
167 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# DEPRECATED: this should not be used in new code and will be removed imminently | ||
|
||
from api.v3.search_filters import AutocompleteFilter as V3AutocompleteFilter | ||
from api.v3.search_filters import get_autocomplete_builder | ||
|
||
|
||
class AutocompleteFilter(V3AutocompleteFilter): | ||
""" | ||
Apply name autocomplete filter to credential search | ||
""" | ||
|
||
query_builder_class = get_autocomplete_builder( | ||
("name_text", "address_civic_address", "topic_source_id", "topic_name") | ||
) |
28 changes: 28 additions & 0 deletions
28
server/vcr-server/api/v4/serializers/search/autocomplete.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# DEPRECATED: this should not be used in new code and will be removed imminently | ||
|
||
from rest_framework.serializers import SerializerMethodField | ||
|
||
from api.v3.indexes.Topic import TopicIndex | ||
|
||
from api.v2.serializers.rest import TopicSerializer | ||
|
||
from api.v3.serializers.search import ( | ||
TopicAutocompleteSerializer as V3TopicAutocompleteSerializer, | ||
) | ||
|
||
|
||
class TopicAutocompleteSerializer(V3TopicAutocompleteSerializer): | ||
names = SerializerMethodField() | ||
|
||
@staticmethod | ||
def get_names(obj): | ||
return [ | ||
name.text | ||
for name in obj.object.get_active_names() | ||
if name.type == "entity_name" | ||
] | ||
|
||
class Meta(TopicSerializer.Meta): | ||
index_classes = [TopicIndex] | ||
fields = ("id", "type", "sub_type", "value", "score", "topic_source_id", | ||
"topic_type", "credential_id", "credential_type", "names",) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# DEPRECATED: this should not be used in new code and will be removed imminently | ||
|
||
import logging | ||
|
||
from drf_yasg import openapi | ||
from drf_yasg.utils import swagger_auto_schema | ||
from drf_haystack.serializers import HaystackSerializer | ||
|
||
from api.v3.views.search import ( | ||
AggregateAutocompleteView, | ||
aggregate_autocomplete_swagger_params as swagger_params, | ||
) | ||
from api.v3.serializers.search import ( | ||
AddressAutocompleteSerializer, | ||
NameAutocompleteSerializer, | ||
# TODO: create a v4 version of this serializer | ||
TopicAutocompleteSerializer, | ||
) | ||
from api.v3.search_filters import StatusFilter as AutocompleteStatusFilter | ||
from api.v4.search.filters.autocomplete import AutocompleteFilter | ||
from api.v4.search.filters.topic import ( | ||
TopicCategoryFilter as AutocompleteCategoryFilter, | ||
TopicExactFilter as AutocompleteExactFilter, | ||
) | ||
from api.v3.indexes import ( | ||
Address as AddressIndex, | ||
Name as NameIndex, | ||
Topic as TopicIndex, | ||
) | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
_swagger_params = [ | ||
openapi.Parameter( | ||
"category", | ||
openapi.IN_QUERY, | ||
description="Filter by Credential Category. The category name and value should be joined by '::'", | ||
type=openapi.TYPE_STRING, | ||
), | ||
openapi.Parameter( | ||
"type_id", | ||
openapi.IN_QUERY, | ||
description="Filter by Credential Type ID of the Topic", | ||
type=openapi.TYPE_STRING, | ||
), | ||
openapi.Parameter( | ||
"issuer_id", | ||
openapi.IN_QUERY, | ||
description="Filter by Issuer ID of the Topic", | ||
type=openapi.TYPE_STRING, | ||
), | ||
openapi.Parameter( | ||
"credential_type_id", | ||
openapi.IN_QUERY, | ||
description="Filter by Credential Type ID of any credentials owned by the Topic", | ||
type=openapi.TYPE_STRING, | ||
), | ||
# Put additional parameters here | ||
] + list(swagger_params) | ||
|
||
|
||
class AggregateAutocompleteSerializer(HaystackSerializer): | ||
class Meta: | ||
serializers = { | ||
AddressIndex: AddressAutocompleteSerializer, | ||
NameIndex: NameAutocompleteSerializer, | ||
TopicIndex: TopicAutocompleteSerializer, | ||
} | ||
|
||
filter_fields_map = { | ||
"category": ("topic_category",), | ||
"issuer_id": ("topic_issuer_id"), | ||
"type_id": ("topic_type_id"), | ||
"credential_type_id": ("topic_credential_type_id"), | ||
"inactive": ( | ||
"address_credential_inactive", | ||
"name_credential_inactive", | ||
"topic_all_credentials_inactive", | ||
), | ||
"revoked": ( | ||
"address_credential_revoked", | ||
"name_credential_revoked", | ||
"topic_all_credentials_revoked", | ||
), | ||
} | ||
|
||
|
||
class SearchView(AggregateAutocompleteView): | ||
""" | ||
Return autocomplete results for a query string | ||
""" | ||
|
||
@swagger_auto_schema( | ||
manual_parameters=_swagger_params, | ||
responses={200: AggregateAutocompleteSerializer(many=True)}, | ||
) | ||
def list(self, *args, **kwargs): | ||
return super(SearchView, self).list(*args, **kwargs) | ||
|
||
filter_backends = ( | ||
AutocompleteFilter, | ||
AutocompleteStatusFilter, | ||
AutocompleteCategoryFilter, | ||
AutocompleteExactFilter, | ||
) |