2626import logging
2727import typing
2828
29- import django
3029from django .core .exceptions import ImproperlyConfigured
3130from django .conf import settings
3231from elasticsearch8 .exceptions import NotFoundError
@@ -127,7 +126,7 @@ def __new__(mcls, name, bases, attrs): # noqa: B902
127126 @classmethod
128127 def construct_index (cls , opts , bases ):
129128 """
130- Override IndexMeta.construct_index so a new Index is created for each class
129+ Extend IndexMeta.construct_index so a new Index is created for each class
131130 and Index.settings, Index.analyzers, and Index.using are inherited
132131 (but not Index.name or Index.aliases)
133132 """
@@ -233,10 +232,20 @@ def require_been_setup(cls, using: str | None = None) -> None:
233232
234233 @classmethod
235234 def get_index_name_prefix (cls ) -> str :
236- _name_prefix = cls ._get_meta_attr ("index_name_prefix" ) or ""
235+ _name_prefix = (
236+ cls ._get_meta_attr ("index_name_prefix" )
237+ or cls ._get_djelme_backend ().default_index_name_prefix
238+ or ""
239+ )
237240 assert isinstance (_name_prefix , str )
238241 return _name_prefix
239242
243+ @classmethod
244+ def _get_djelme_backend (cls ) -> "DjelmeElastic8Backend" :
245+ _backend = djelme_registry .get_backend_for_recordtype (cls )
246+ assert isinstance (_backend , DjelmeElastic8Backend )
247+ return _backend
248+
240249 @classmethod
241250 def _get_using (
242251 cls , using : str | Elastic8Client | None = None
@@ -249,7 +258,7 @@ def _get_using(
249258 """
250259 _backend : ProtoDjelmeBackend | None = None
251260 if using in (None , "default" ):
252- _backend = djelme_registry . get_backend_for_recordtype ( cls )
261+ _backend = cls . _get_djelme_backend ( )
253262 elif isinstance (using , str ) and (using in djelme_registry .all_backends ):
254263 _backend = djelme_registry .get_backend (using )
255264 if _backend is not None :
@@ -321,15 +330,14 @@ def _index(self):
321330 return self .__index
322331 # return a copy with `name` and `using` freshly computed
323332 try :
324- _backend = djelme_registry . get_backend_for_recordtype ( self )
333+ _backend = self . _get_djelme_backend ( )
325334 except LookupError :
326335 _using = None # may not be registered yet, is ok
336+ _index_name = ""
327337 else :
328338 _using = _backend ._elastic8dsl_connection_name
329- return self .__index .clone (
330- name = self .djelme_index_name (),
331- using = _using ,
332- )
339+ _index_name = self .djelme_index_name ()
340+ return self .__index .clone (name = _index_name , using = _using )
333341
334342 @_index .setter
335343 def _index (self , val ):
@@ -562,8 +570,6 @@ def sync_index_template(cls, using=None): # -> ComposableIndexTemplate:
562570 def check_djelme_setup (cls , using : str | Elastic8Client | None = None ) -> None :
563571 """Check if class is in sync with index template in Elasticsearch.
564572
565- override `BaseDjelmeRecord.check_djelme_setup` to check for a template instead of an index
566-
567573 :raise: IndexTemplateNotFoundError if index template does not exist.
568574 :raise: IndexTemplateOutOfSyncError if mappings, settings, or index patterns
569575 are out of sync.
@@ -687,32 +693,21 @@ def record(
687693 * ,
688694 # each usage record needs a sessionhour_id -- for migrating old data, can set explicitly...
689695 sessionhour_id : str = "" ,
690- # ...but when saving new data, give either the dirty identifying strings:
696+ # ...but when saving new data, give the dirty identifying strings
697+ # (which won't be stored, but used to create an opaque sessionhour_id)
691698 user_id : str = "" ,
692699 client_session_id : str = "" ,
693700 request_host : str = "" ,
694701 request_useragent : str = "" ,
695- # ...or a django request to infer user/host/useragent from
696- django_request : django .http .HttpRequest | None = None ,
697702 # additional kwargs presumed to give field values
698703 ** kwargs : typing .Any ,
699704 ) -> "typing.Self" : # typing.Self added in py 3.11 -- str annotation until 3.10 eol
700705 """CountedUsageRecord.record(...): construct and save a record"""
701- _useragent = (
702- request_useragent
703- if (request_useragent or (django_request is None ))
704- else django_request .META .get ("HTTP_USER_AGENT" , "" )
705- )
706- _host = (
707- request_host
708- if (request_host or (django_request is None ))
709- else django_request .get_host ()
710- )
711706 _sessionhour_id = sessionhour_id or opaque_sessionhour_id (
712707 client_session_id = client_session_id ,
713708 user_id = user_id ,
714- request_host = _host ,
715- request_useragent = _useragent ,
709+ request_host = request_host ,
710+ request_useragent = request_useragent ,
716711 timestamp = kwargs .get ("timestamp" ),
717712 )
718713 _new_record = super ().record (** kwargs , sessionhour_id = _sessionhour_id )
@@ -762,6 +757,10 @@ def get_timeseries_timeparts(self) -> str:
762757class DjelmeElastic8Backend :
763758 """DjelmeElastic8Backend: elastic8 backend for djelme (for use by generic djelme code)"""
764759
760+ _NON_PASSTHRU_KWARGS : typing .ClassVar [collections .abc .Collection [str ]] = {
761+ "djelme_default_index_name_prefix" ,
762+ }
763+
765764 backend_name : str
766765 imp_kwargs : dict [str , str ] # pass-thru to elasticsearch connection kwargs
767766
@@ -771,6 +770,10 @@ def djelme_backend_name(self) -> str: # for ProtoDjelmeBackend
771770 def djelme_imp_kwargs (self ) -> dict [str , str ]: # for ProtoDjelmeBackend
772771 return self .imp_kwargs
773772
773+ @property
774+ def default_index_name_prefix (self ) -> str :
775+ return self .imp_kwargs .get ("djelme_default_index_name_prefix" , "" )
776+
774777 @property
775778 def elastic8_client (self ) -> Elastic8Client :
776779 # assumes `connections.configure` was already called
@@ -782,7 +785,11 @@ def _elastic8dsl_connection_name(self) -> str:
782785
783786 @property
784787 def _elastic8dsl_connection_kwargs (self ) -> dict [str , typing .Any ]:
785- return self .imp_kwargs
788+ return {
789+ _key : _val
790+ for _key , _val in self .imp_kwargs .items ()
791+ if _key not in self ._NON_PASSTHRU_KWARGS
792+ }
786793
787794 def djelme_setup (self , recordtypes : collections .abc .Iterable [type ]) -> None :
788795 # for ProtoDjelmeBackend
0 commit comments