Giter Site home page Giter Site logo

Comments (3)

sentrivana avatar sentrivana commented on July 26, 2024

Hey @mihalikv, thanks for writing in.

To clarify, this my_custom_attribute that you're putting on the user in your middleware example is the user_doc from your Sentry screenshot I assume? And you are randomly getting an AttributeError when trying to access it?

From the SDK point of view our only interaction with request.user as far as I can see is here, where we copy some attributes from it over to the Sentry event. We shouldn't be modifying the user. We are accessing it though, which might potentially be relevant because of the whole Django SimpleLazyObject business.

Could you post the full Python stacktrace for the error from your app's logs?

Do you use any additional auth middleware or any middleware that does something user related?

from sentry-python.

mihalikv avatar mihalikv commented on July 26, 2024

Hi @sentrivana ,

yes you are right my_custom_attribute in example is user_doc in real application.

Full stack trace is(I am not sure, how can I provide more details):

AttributeError: 'User' object has no attribute 'user_doc'
  File "django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "rest_framework/viewsets.py", line 125, in view
    return self.dispatch(request, *args, **kwargs)
  File "rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "project/search/views.py", line 501, in get_rich_data
    result = self.get_rich_data_from_es()
  File "project/search/views.py", line 227, in get_rich_data_from_es
    search = Catalog.search(index=self.index_name).protect_query(Catalog.get_query_filter(self.request.user))
  File "project/search/mappings/default.py", line 134, in get_query_filter
    document_filter = tenant.get_permissions().PERMISSIONS[user_group][cls.__name__]['handler'](licence, user, child_groups=child_groups)
  File "project/search/permissions/tenant.py", line 268, in get_my_products
    country = "sk" if user.user_doc["state"] == "Slovensko" else "cz"
  File "django/utils/functional.py", line 259, in inner
    return func(self._wrapped, *args)

Middleware definition is following:

MIDDLEWARE = [
    'django_tenants.middleware.main.TenantMainMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django_middleware_global_request.middleware.GlobalRequestMiddleware',
    'project.basket.middleware.BasketMiddleware',
    'project.account.middleware.AccountMiddleware',
    'project.account.middleware.SwitchLanguage',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'project.account.middleware.CustomRedirectMiddleware',
]

It's probablly not very helpful, but I can say that in our custom middlewares we are also just accesing user_doc attribute and it's not replaced.

Another notable think is that we are using DRF and DRF process normal request to own custom request and even user is something special code here.

It was our first point of interest while trying to fix it. We even try to monkeypatch it in this way:

OriginalRequest = rest_framework.request.Request

class CustomRequest(OriginalRequest):

    @property
    def user(self):
        user = self._request.user
        user.user_doc = self._request.user_doc
        return user

    @user.setter
    def user(self, value):
        OriginalRequest.user.fset(self, value)

if OriginalRequest.__name__ != "CustomRequest":
    rest_framework.request.Request = CustomRequest

but result was the same.
Fact is that error occures even on normal views and WSGI request so it's probably not a problem of DRF.

from sentry-python.

szokeasaurusrex avatar szokeasaurusrex commented on July 26, 2024

Ok, thanks for the information @mihalikv, we will continue investigating

from sentry-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.