diff --git a/django_search/settings.py b/django_search/settings.py index f692abd..9d8b3c0 100644 --- a/django_search/settings.py +++ b/django_search/settings.py @@ -131,4 +131,17 @@ MEILISEARCH_MASTER_API_KEY = "masterKey" MEILISEARCH_API_KEY = "da7a448d-2b13-490b-8ca6-6b3e051b4201" SEARCH_ENGINE = "django_search_backend.drivers.meilisearch.MeiliSearchEngine" -COURSEWARE_INFO_INDEX_NAME = 'course_info' + +INDEX_CONFIGURATION_CLASS = "django_search_backend.drivers.meilisearch.BaseIndexConfiguration" +INDEX_CONFIGURATIONS = { + "meilisearch_courseware_content": { + "pk": "id", + "search_rules": [ + "ORG: Arbisoft" + ], + "filterable": [ + "ORG", + "COURSE" + ], + } +} diff --git a/django_search_backend/drivers/__init__.py b/django_search_backend/drivers/__init__.py index c54deb1..3e54f52 100644 --- a/django_search_backend/drivers/__init__.py +++ b/django_search_backend/drivers/__init__.py @@ -2,9 +2,37 @@ from django.utils.module_loading import import_string +class BaseDriver: + def get_user_token(self, index_search_rules=dict()): + raise Exception("Not Implemented") + + def check_connection(self, index_search_rules=dict()): + raise Exception("Not Implemented") + + def indexes(self, index_search_rules=dict()): + raise Exception("Not Implemented") + + def index(self, index_name, payload): + raise Exception("Not Implemented") + + def get_search_rules(self, search_rules=None): + """ + [ + "ORG: some-value" + ] + :param search_rules: you can set list of string filters + :return: + """ + raise Exception("Not Implemented") + + class DriverFactory: @classmethod - def get_client(cls, request, *args, **kwargs): - search_driver = getattr(settings, 'SEARCH_ENGINE', 'search.meilisearch.MeiliSearchEngine') + def get_client(cls, request, *args, **kwargs) -> BaseDriver: + search_driver = getattr( + settings, + 'SEARCH_ENGINE', + 'django_search_backend.drivers.meilisearch.MeiliSearchEngine' + ) klass = import_string(search_driver) return klass.get_instance(request, *args, **kwargs) diff --git a/django_search_backend/drivers/meilisearch.py b/django_search_backend/drivers/meilisearch.py index f33eedf..efa0c2d 100644 --- a/django_search_backend/drivers/meilisearch.py +++ b/django_search_backend/drivers/meilisearch.py @@ -2,11 +2,36 @@ from typing import Optional, Mapping, Any, List, Dict from django.conf import settings +from django.utils.module_loading import import_string from meilisearch import Client as MeilisearchClient from meilisearch.errors import MeilisearchError from meilisearch.index import Index from meilisearch.models.key import Key +from django_search_backend.drivers import BaseDriver + + +class BaseIndexConfiguration(BaseDriver): + def __init__(self, request, *args, **kwargs): + self.request = request + self._INDEX_CONFIGURATIONS = getattr( + settings, + 'INDEX_CONFIGURATIONS', + {} + ) + + @property + def indexes(self): + return self._INDEX_CONFIGURATIONS.keys() + + def get_search_rules(self, search_rules=None): + if search_rules is None: + search_rules = [] + rules = {} + for index, config in self._INDEX_CONFIGURATIONS.items(): + rules[index] = {'filter': ' AND '.join(config.get('search_rules', []) + search_rules)} + return rules + class MeiliSearchEngine: @@ -71,3 +96,18 @@ def get_instance(cls, request, *args, **kwargs): MEILISEARCH_API_KEY, MEILISEARCH_MASTER_API_KEY ) + + def _get_search_rules_class(self, *args, **kwargs) -> BaseIndexConfiguration: + IndexConfig = getattr( + settings, + 'INDEX_CONFIGURATION_CLASS', + 'django_search_backend.drivers.meilisearch.BaseIndexConfiguration' + ) + klass = import_string(IndexConfig) + return klass(self.request, *args, **kwargs) + + def get_search_rules(self, search_rules=None): + if search_rules is None: + search_rules = [] + rules_instance = self._get_search_rules_class() + return rules_instance.get_search_rules(search_rules=search_rules) diff --git a/django_search_backend/views.py b/django_search_backend/views.py index c0f2953..44fb532 100644 --- a/django_search_backend/views.py +++ b/django_search_backend/views.py @@ -7,8 +7,7 @@ class AuthTokenView(View): def get(self, request): client = DriverFactory.get_client(request) - token = client.get_user_token({ - 'meilisearch_courseware_content': {} - }) + search_rules = client.get_search_rules() + token = client.get_user_token(search_rules) return JsonResponse(token)