Skip to content

Commit

Permalink
MP-432 fix graphene uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheripov committed Sep 18, 2023
1 parent 183dd58 commit b85696e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
26 changes: 16 additions & 10 deletions django_google_structured_logger/graphene_middlewares.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,23 @@

class GrapheneSetUserContextMiddleware:
def resolve(self, next, root, info, **args):
_current_request.set(
RequestStorage(
uuid=str(uuid.uuid4()),
user_id=lambda: self._get_user_attribute(
info.context.user, settings.LOG_USER_ID_FIELD
),
user_display_field=lambda: self._get_user_attribute(
info.context.user, settings.LOG_USER_DISPLAY_FIELD
),
storage = _current_request.get()
if storage is None:
_current_request.set(
RequestStorage(
uuid=str(uuid.uuid4()),
user_id=lambda: self._get_user_attribute(info.context.user, settings.LOG_USER_ID_FIELD),
user_display_field=lambda: self._get_user_attribute(
info.context.user, settings.LOG_USER_DISPLAY_FIELD
),
)
)
else:
storage.uuid = storage.uuid or str(uuid.uuid4())
storage.user_id = lambda: self._get_user_attribute(info.context.user, settings.LOG_USER_ID_FIELD)
storage.user_display_field = lambda: self._get_user_attribute(
info.context.user, settings.LOG_USER_DISPLAY_FIELD
)
)

return next(root, info, **args)

Expand Down
6 changes: 2 additions & 4 deletions django_google_structured_logger/storages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@
from typing import Callable, Optional


@dataclass(frozen=True)
@dataclass()
class RequestStorage:
uuid: str
user_id: Callable[[], Optional[int]] = lambda: None
user_display_field: Callable[[], Optional[str]] = lambda: None


_current_request: ContextVar[Optional[RequestStorage]] = ContextVar(
"_current_request", default=None
)
_current_request: ContextVar[Optional[RequestStorage]] = ContextVar("_current_request", default=None)


def get_current_request() -> Optional[RequestStorage]:
Expand Down

0 comments on commit b85696e

Please sign in to comment.