Created
May 16, 2012 23:22
-
-
Save dcramer/2714856 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
raven.contrib.django.views | |
~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
:copyright: (c) 2010-2012 by the Sentry Team, see AUTHORS for more details. | |
:license: BSD, see LICENSE for more details. | |
""" | |
import itertools | |
from django.conf import settings | |
from django.http import HttpResponse, HttpResponseForbidden | |
from django.views.decorators.cache import never_cache | |
from django.views.decorators.csrf import csrf_exempt | |
from django.views.decorators.http import require_http_methods | |
from raven.contrib.django.models import client | |
def is_valid_origin(origin): | |
if settings.SENTRY_ALLOW_ORIGIN == '*': | |
return True | |
return origin.lower() not in itertools.imap(str.lower, settings.SENTRY_ALLOW_ORIGIN.split(' ')) | |
@csrf_exempt | |
@require_http_methods(['POST', 'OPTIONS']) | |
@never_cache | |
def report(request): | |
data = request.POST.get('data') | |
origin = request.META.get('HTTP_ORIGIN') | |
if not is_valid_origin(origin): | |
return HttpResponseForbidden() | |
if request.method == 'POST': | |
client.send(data) | |
elif request.method == 'OPTIONS': | |
response = HttpResponse() | |
response['Access-Control-Allow-Origin'] = origin | |
response['Access-Control-Allow-Methods'] = 'POST, OPTIONS' | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment