|
1 | 1 | from __future__ import absolute_import |
2 | 2 |
|
| 3 | +from django import VERSION as DJANGO_VERSION |
3 | 4 | from django.core import signals |
4 | 5 |
|
5 | 6 | try: |
|
9 | 10 |
|
10 | 11 | from sentry_sdk import get_current_hub, configure_scope, capture_exception |
11 | 12 | from sentry_sdk.hub import _internal_exceptions |
12 | | -from ._wsgi import RequestExtractor |
| 13 | +from ._wsgi import RequestExtractor, get_client_ip |
13 | 14 | from . import Integration |
14 | 15 |
|
15 | 16 |
|
| 17 | +if DJANGO_VERSION < (1, 10): |
| 18 | + def is_authenticated(request_user): |
| 19 | + return request_user.is_authenticated() |
| 20 | +else: |
| 21 | + def is_authenticated(request_user): |
| 22 | + return request_user.is_authenticated |
| 23 | + |
| 24 | + |
16 | 25 | class DjangoIntegration(Integration): |
17 | 26 | identifier = 'django' |
18 | 27 |
|
@@ -46,6 +55,10 @@ def processor(event): |
46 | 55 | with _internal_exceptions(): |
47 | 56 | DjangoRequestExtractor(request).extract_into_event(event) |
48 | 57 |
|
| 58 | + if 'user' not in event: |
| 59 | + with _internal_exceptions(): |
| 60 | + _set_user_info(request, event) |
| 61 | + |
49 | 62 | # TODO: user info |
50 | 63 |
|
51 | 64 | return processor |
@@ -83,3 +96,24 @@ def files(self): |
83 | 96 |
|
84 | 97 | def size_of_file(self, file): |
85 | 98 | return file.size |
| 99 | + |
| 100 | + |
| 101 | +def _set_user_info(request, event): |
| 102 | + event['user'] = user_info = { |
| 103 | + 'ip_address': get_client_ip(request.META), |
| 104 | + } |
| 105 | + |
| 106 | + user = getattr(request, "user", None) |
| 107 | + |
| 108 | + if user is None or not is_authenticated(user): |
| 109 | + return |
| 110 | + |
| 111 | + try: |
| 112 | + user_info['email'] = user.email |
| 113 | + except Exception: |
| 114 | + pass |
| 115 | + |
| 116 | + try: |
| 117 | + user_info['username'] = user.get_username() |
| 118 | + except Exception: |
| 119 | + pass |
0 commit comments