from __future__ import absolute_import

from django.conf import settings

from raygun4py import raygunprovider

class Provider(object):

    def __init__(self):
    	config = getattr(settings, 'RAYGUN4PY_CONFIG', {})
    	apiKey = getattr(settings, 'RAYGUN4PY_API_KEY', config.get('api_key', None))
    	
        self.sender = raygunprovider.RaygunSender(apiKey, config=config)

    def process_exception(self, request, exception):
    	raygunRequest = self._mapRequest(request)

        self.sender.send_exception(exception=exception, request=raygunRequest)

    def _mapRequest(self, request):
        headers = request.META.items()
        _headers = dict()
        for k, v in headers:
            if not k.startswith('wsgi'):
                _headers[k] = v

        return {
            'hostName': request.get_host(),
            'url': request.path,
            'httpMethod': request.method,
            'ipAddress': request.META.get('REMOTE_ADDR', '?'),
            'queryString': dict((key, request.GET[key]) for key in request.GET),
            'form': dict((key, request.POST[key]) for key in request.POST),
            'headers': _headers,
            'rawData': request.body if hasattr(request, 'body') else getattr(request, 'raw_post_data', {})
        }

from __future__ import absolute_import

from django.conf import settings

from raygun4py import raygunprovider

class Provider(object):

    def __init__(self):
    	config = getattr(settings, 'RAYGUN4PY_CONFIG', {})
    	apiKey = getattr(settings, 'RAYGUN4PY_API_KEY', config.get('api_key', None))
    	
        self.sender = raygunprovider.RaygunSender(apiKey, config=config)

    def process_exception(self, request, exception):
    	raygunRequest = self._mapRequest(request)

        self.sender.send_exception(exception=exception, request=raygunRequest)

    def _mapRequest(self, request):
        headers = request.META.items()
        _headers = dict()
        for k, v in headers:
            if not k.startswith('wsgi'):
                _headers[k] = v


        return {
            'hostName': request.get_host(),
            'url': request.path,
            'httpMethod': request.method,
            'ipAddress': request.META.get('REMOTE_ADDR', '?'),
            'queryString': dict((key, request.GET[key]) for key in request.GET),
            'form': dict((key, request.POST[key]) for key in request.POST),
            'headers': _headers,
            'rawData': request.body if hasattr(request, 'body') else getattr(request, 'raw_post_data', {})
        }

Fix RawPostDataException on request.body access.
Fix RawPostDataException on request.body access.

Django's HttpRequest class doesn't like the `request.body` to be accessed more than one time.  Upon the second attempt to read from `request.body`, Django throws a `RawPostDataException`.  Since the previous code in this spot was conditional upon a `hasattr(request, 'body')` check, and because hasattr returns False when the attribute access raises an exception, raygun was falling back to looking up the rawData field from `request.raw_post_data`.  But that attribute doesn't exist in newer versions of Django, so we were getting exceptions in the raygun4py's Django process_exception middleware, which is a royal pain in the tush.

So, this change does the hasattr check on both attributes, and only includes rawData if one of the `request.body` or `request.raw_post_data` attributes actually exists and is readable.