<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> import logging <EOL> from django . conf import settings <EOL> from django . core . exceptions import SuspiciousOperation <EOL> from django . http import HttpRequest <EOL> from django . template import TemplateSyntaxError <EOL> try : <EOL> from django . template . base import Origin <EOL> except ImportError : <EOL> from django . template . loader import LoaderOrigin as Origin <EOL> from raven . base import Client <EOL> from raven . contrib . django . utils import get_data_from_template , get_host <EOL> from raven . contrib . django . middleware import SentryLogMiddleware <EOL> from raven . utils . wsgi import get_headers , get_environ <EOL> __all__ = ( '<STR_LIT>' , ) <EOL> class DjangoClient ( Client ) : <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def get_user_info ( self , user ) : <EOL> if hasattr ( user , '<STR_LIT>' ) and not user . is_authenticated ( ) : <EOL> return None <EOL> user_info = { <EOL> '<STR_LIT:id>' : user . pk , <EOL> } <EOL> if hasattr ( user , '<STR_LIT:email>' ) : <EOL> user_info [ '<STR_LIT:email>' ] = user . email <EOL> if hasattr ( user , '<STR_LIT>' ) : <EOL> user_info [ '<STR_LIT:username>' ] = user . get_username ( ) <EOL> elif hasattr ( user , '<STR_LIT:username>' ) : <EOL> user_info [ '<STR_LIT:username>' ] = user . username <EOL> return user_info <EOL> def get_data_from_request ( self , request ) : <EOL> result = { } <EOL> user = getattr ( request , '<STR_LIT:user>' , None ) <EOL> if user is not None : <EOL> user_info = self . get_user_info ( user ) <EOL> if user_info : <EOL> result [ '<STR_LIT:user>' ] = user_info <EOL> try : <EOL> uri = request . build_absolute_uri ( ) <EOL> except SuspiciousOperation : <EOL> if request . is_secure ( ) : <EOL> scheme = '<STR_LIT>' <EOL> else : <EOL> scheme = '<STR_LIT:http>' <EOL> host = get_host ( request ) <EOL> uri = '<STR_LIT>' % ( scheme , host , request . path ) <EOL> if request . method not in ( '<STR_LIT:GET>' , '<STR_LIT>' ) : <EOL> try : <EOL> data = request . body <EOL> except Exception : <EOL> try : <EOL> data = request . raw_post_data <EOL> except Exception : <EOL> try : <EOL> data = request . POST or '<STR_LIT>' <EOL> except Exception : <EOL> data = '<STR_LIT>' <EOL> else : <EOL> data = None <EOL> environ = request . META <EOL> result . update ( { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : request . method , <EOL> '<STR_LIT:url>' : uri , <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:data>' : data , <EOL> '<STR_LIT>' : dict ( request . COOKIES ) , <EOL> '<STR_LIT>' : dict ( get_headers ( environ ) ) , <EOL> '<STR_LIT>' : dict ( get_environ ( environ ) ) , <EOL> } <EOL> } ) <EOL> return result <EOL> def build_msg ( self , * args , ** kwargs ) : <EOL> data = super ( DjangoClient , self ) . build_msg ( * args , ** kwargs ) <EOL> for frame in self . _iter_frames ( data ) : <EOL> module = frame . get ( '<STR_LIT>' ) <EOL> if not module : <EOL> continue <EOL> if module . startswith ( '<STR_LIT>' ) : <EOL> frame [ '<STR_LIT>' ] = False <EOL> if not self . site and '<STR_LIT>' in settings . INSTALLED_APPS : <EOL> try : <EOL> from django . contrib . sites . models import Site <EOL> site = Site . objects . get_current ( ) <EOL> site_name = site . name or site . domain <EOL> data [ '<STR_LIT>' ] . setdefault ( '<STR_LIT>' , site_name ) <EOL> except Exception : <EOL> try : <EOL> data [ '<STR_LIT>' ] . setdefault ( '<STR_LIT>' , settings . SITE_ID ) <EOL> except AttributeError : <EOL> pass <EOL> return data <EOL> def capture ( self , event_type , request = None , ** kwargs ) : <EOL> if '<STR_LIT:data>' not in kwargs : <EOL> kwargs [ '<STR_LIT:data>' ] = data = { } <EOL> else : <EOL> data = kwargs [ '<STR_LIT:data>' ] <EOL> if request is None : <EOL> request = getattr ( SentryLogMiddleware . thread , '<STR_LIT>' , None ) <EOL> is_http_request = isinstance ( request , HttpRequest ) <EOL> if is_http_request : <EOL> data . update ( self . get_data_from_request ( request ) ) <EOL> if kwargs . get ( '<STR_LIT>' ) : <EOL> exc_value = kwargs [ '<STR_LIT>' ] [ <NUM_LIT:1> ] <EOL> if hasattr ( exc_value , '<STR_LIT>' ) or ( ( isinstance ( exc_value , TemplateSyntaxError ) and <EOL> isinstance ( getattr ( exc_value , '<STR_LIT:source>' , None ) , <EOL> ( tuple , list ) ) and <EOL> isinstance ( exc_value . source [ <NUM_LIT:0> ] , Origin ) ) ) or hasattr ( exc_value , '<STR_LIT>' ) : <EOL> source = getattr ( exc_value , '<STR_LIT>' , <EOL> getattr ( exc_value , '<STR_LIT:source>' , None ) ) <EOL> debug = getattr ( exc_value , '<STR_LIT>' , None ) <EOL> if source is None : <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> data . update ( get_data_from_template ( source , debug ) ) <EOL> result = super ( DjangoClient , self ) . capture ( event_type , ** kwargs ) <EOL> if is_http_request and result : <EOL> request . sentry = { <EOL> '<STR_LIT>' : data . get ( '<STR_LIT>' , self . remote . project ) , <EOL> '<STR_LIT:id>' : self . get_ident ( result ) , <EOL> } <EOL> return result </s>
