Skip to content

Commit

Permalink
feat: implement search rules based tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
qasimgulzar committed Jul 18, 2024
1 parent b16e26f commit d1f8943
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 6 deletions.
15 changes: 14 additions & 1 deletion django_search/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
}
}
32 changes: 30 additions & 2 deletions django_search_backend/drivers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
40 changes: 40 additions & 0 deletions django_search_backend/drivers/meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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)
5 changes: 2 additions & 3 deletions django_search_backend/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit d1f8943

Please sign in to comment.