Exemplo n.º 1
0
def log_exceptions(exc_type=Exception, **kwargs):
    from raven.contrib.django.models import client as raven_client
    try:
        yield
    except exc_type, exc:
        raven_client.captureException(sys.exc_info())
        print(exc)
Exemplo n.º 2
0
    def get(self, request, pk):
        self.object = project = get_object_or_404(Project.objects.select_related('owner'), pk=pk)
        moonclerk_key = settings.MOONCLERK_API_KEY

        customer_id = request.GET.get('customer_id', None)
        payment_id = request.GET.get('payment_id', None)

        if customer_id is None and payment_id is None:
            return HttpResponse('You must specify either a customer_id or a payment_id.', status=400)

        payment = customer = None
        try:
            if customer_id:
                customer = self.get_moonclerk_customer(request, customer_id, moonclerk_key)
            if payment_id:
                payment = self.get_moonclerk_payment(request, payment_id, moonclerk_key)
            assert customer or payment
        except:
            # Something went wrong and we weren't able to create a customer or
            # payment.
            from raven.contrib.django.models import client
            client.captureException()
            return self.get_payment_error()

        self.set_payment_info(project, payment, customer)

        return redirect('app-project-activation-success',
            owner_slug=project.owner.slug,
            project_slug=project.slug)
Exemplo n.º 3
0
    def get(self, request):
        try:
            raise ValueError('An example error')
        except Exception:
            client.captureException(request=request)

        return Error500View.as_view()(request)
Exemplo n.º 4
0
def ToJsonError(e, exc_info):
  """Converts an exception to an API error response."""
  # Wrap some common exception types into Krest types
  if isinstance(e, Http404):
    e = krest.NotFoundError(e.message)
  elif isinstance(e, ValueError):
    e = krest.BadRequestError(str(e))
  elif isinstance(e, backend.NoTokenError):
    e = krest.NotFoundError(e.message)

  # Now determine the response based on the exception type.
  if isinstance(e, krest.Error):
    code = e.__class__.__name__
    http_code = e.HTTP_CODE
    message = e.Message()
  else:
    code = 'ServerError'
    http_code = 500
    message = 'An internal error occurred: %s' % str(e)
    if settings.DEBUG:
      message += "\n" + "\n".join(traceback.format_exception(*exc_info))
  if settings.DEBUG and settings.HAVE_RAVEN:
    from raven.contrib.django.models import client
    client.captureException()
  result = {
    'error' : {
      'code' : code,
      'message' : message
    }
  }
  return result, http_code
Exemplo n.º 5
0
def wrap_exception(request, exception):
  """Returns a HttpResponse with the exception in JSON form."""
  exc_info = sys.exc_info()

  LOGGER.error('%s: %s' % (exception.__class__.__name__, exception),
      exc_info=exc_info,
      extra={
        'status_code': 500,
        'request': request,
      }
  )

  if settings.DEBUG and settings.HAVE_RAVEN:
    from raven.contrib.django.models import client
    client.captureException()

  # Don't wrap the exception during debugging.
  if settings.DEBUG and 'deb' in request.GET:
    return None

  result_data, http_code = to_json_error(exception, exc_info)
  result_data['meta'] = {
    'result': 'error'
  }
  return build_response(result_data, response_code=http_code)
Exemplo n.º 6
0
 def __call__(self, *args, **kwargs):
     try:
         return self.func(*args, **kwargs)
     except:
         if hasattr(settings, 'RAVEN_CONFIG'):
             from raven.contrib.django.models import client
             client.captureException()
         raise
Exemplo n.º 7
0
 def wrapped(*args, **kwargs):
     try:
         return func(*args, **kwargs)
     except exclude:
         raise
     except on as exc:
         Raven.captureException()
         current.retry(exc=exc)
Exemplo n.º 8
0
    def obj_create(self, bundle, **kwargs):
        try:
            raise Exception('oops')
        except:
            client.captureException()

        bundle.obj = Item(**kwargs)
        bundle = self.full_hydrate(bundle)
        return bundle
Exemplo n.º 9
0
    def new_execute(self, *args, **kwargs):
        try:
            return original_func(self, *args, **kwargs)
        except Exception:
            from raven.contrib.django.models import client

            client.captureException(extra={
                'argv': sys.argv
            })
            raise
Exemplo n.º 10
0
def log_exceptions(exc_type=Exception, **kwargs):
    try:
        from raven.contrib.django.models import client as raven_client
    except ImportError:
        raven_client = None
    try:
        yield
    except exc_type, exc:
        if raven_client is not None:
            raven_client.captureException(sys.exc_info())
        print(exc)
Exemplo n.º 11
0
def _send_slack(data):
    if not (settings.SLACK_ENABLE and settings.SLACK_WEBHOOK):
        logger.info('Slack is not enabled.')
        return

    try:
        response = requests.post(settings.SLACK_WEBHOOK, data=data.encode('utf-8'),
                                 headers={'Content-Type': 'application/json'},
                                 timeout=4)
        response.raise_for_status()
    except requests.exceptions.RequestException:
        sentry_client.captureException()
Exemplo n.º 12
0
def sentry_exc():
    """
    Attempt to send an exception to sentry.
    
    This is "soft coupled" in that it will gracefully fail if raven
    is not installed.
    """
    try:
        from raven.contrib.django.models import client
        # send the exception to raven
        client.captureException()
    except:
        logger.debug("raven is not installed, so we could not notify sentry of the exception.")
        pass
Exemplo n.º 13
0
def oauth_credentials(request):
    """
    How do we correlate Planbox and Shareabouts users? Username is faulty but
    easy. With normal OAuth we wouldn't have this issue because the user would
    specify their own account.

    But for now, there's only one user to support.
    """
    host = 'https://' + settings.SHAREABOUTS_HOST
    client_id = settings.SHAREABOUTS_CLIENT_ID
    client_secret = settings.SHAREABOUTS_CLIENT_SECRET

    # Make sure a project ID is specified
    project_id = request.GET.get('project_id')
    try:
        project = Project.objects.all().get(pk=project_id)
    except Project.DoesNotExist:
        return bad_request([{'project_id': 'Project does not exist.'}])

    # Make sure the user has edit permission on the project.
    if not project.editable_by(request.user):
        return HttpResponse('Unauthorized', status=401)

    # Get the preauthorization object for the project.
    try:
        auth = Preauthorization.objects.get(project=project)
    except Preauthorization.DoesNotExist:
        raise Http404
    username = auth.username

    # Get the requested credentials from the Shareabouts API server
    session = requests.session()
    try:
        auth_header = get_auth_header(client_id, client_secret, username)
        authorization_code = get_authorization_code(session, host, client_id, auth_header)
        credentials = get_credentials(session, host, authorization_code, client_id, client_secret)
    except AssertionError:
        if settings.DEBUG: raise
        client.captureException()
        return HttpResponse('Upstream error occurred.',
            status=502,
            content_type='text/plain')

    return HttpResponse(json.dumps(credentials, indent=2, sort_keys=True),
        status=200,
        content_type='application/json')
Exemplo n.º 14
0
def report_error(error, request=None, extra_data=None):
    """Wrapper for error reporting

    This can be used for store exceptions in error reporting solutions as
    rollbar while handling error gracefully and giving user cleaner message.
    """
    if HAS_ROLLBAR and hasattr(settings, 'ROLLBAR'):
        rollbar.report_exc_info(
            request=request, extra_data=extra_data, level='warning'
        )

    if HAS_RAVEN and hasattr(settings, 'RAVEN_CONFIG'):
        raven_client.captureException(
            request=request, extra=extra_data, level='warning'
        )

    LOGGER.error(
        'Handled exception %s: %s',
        error.__class__.__name__,
        force_text(error)
    )
Exemplo n.º 15
0
def query_url(request):
    """
    This view returns status for url.

    This view returns appropriate responses for each possible status of a query.
    Remarkably 304 and 200 for success responses and 400, 403 and 500 for errors.

    You would like to read it: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    """
    messages = {
        'key_not_send': u"You need to send a key in POST data to be valid "\
            "request. The key should be requested to system administrator.",
        'key_invalid': u"Your key is invalid or not found. Contact system "\
            "administrator for more information.",
        'url_invalid_parameter': u"You need to send a url in POST data to be tested. "\
            "Use 'url' field for that.",
        'url_malformed': u"You sent a malformed url in POST. Check and try again.",
    }

    if not request.method == 'POST':
        if not request.is_ajax():
            return HttpResponseBadRequest('This url should be requested via'\
                ' Ajax POST requests ')
        return HttpResponseNotAllowed(['POST'])

    # TODO: if you have more views that use key put it in a decorator
    request.key = None
    key = request.POST.get('key',None)
    if not key:
        return HttpResponseForbidden(simplejson.dumps(
            {'code':403,'msg':messages['key_not_send']}),
            mimetype="application/json")
    else:
        try:
            keyobj = Key.objects.get(key=key,active=True,
                valid_until__gte=timezone.now())
        except Key.DoesNotExist:
            return HttpResponseForbidden(simplejson.dumps(
                {'code':403,'msg':messages['key_invalid']}),
                mimetype="application/json")
        else:
            request.user = keyobj.user
            request.key = keyobj

    if request.key:
        if 'url' not in request.POST:
            return HttpResponseBadRequest(simplejson.dumps(
                {'code':400,'msg':messages['url_invalid_parameter']}),
                mimetype="application/json")

        url = request.POST['url']
        url_validator = URLValidator()
        try:
            url_validator(url)
        except ValidationError:
            return HttpResponseBadRequest(simplejson.dumps(
                {'code':400,'msg':messages['url_malformed']}),
                mimetype='application/json')

        # try to get from cache
        try:
            return response_from_cache(url,request.user.username)
        except CacheKeyError:
            pass

        urlobj, created = URL.objects.get_or_create(user=request.user,url=url)
        if not urlobj.last_time_checked:
            try:
                urlobj.update_status()
            except Exception, err:
                from .tasks import update_urls
                update_urls.delay(urlobj.url)
                return cache_and_send(urlobj,
                    {'code':500,
                     'msg':'Server error. Task was sent to queue. '\
                        'Try again in some minutes.',
                      'error':unicode(err)},500)

        if urlobj.get_ip_type() in ["RESERVED","SPECIALPURPOSE","LOOPBACK"]:
            msg = u"%s is a %s IP and will not be checked." % (
                urlobj.site_ip, urlobj.get_ip_type())
            return cache_and_send(urlobj,{'code':412,'msg':msg},412)
        if not getattr(settings,"URLCHECK_ALLOW_PRIVATE_IPS",False) and \
            urlobj.get_ip_type() == "PRIVATE":
            msg = u"%s is a %s IP and will not be checked. Set "\
                "URLCHECK_ALLOW_PRIVATE_IPS in settings.py to check this IPs." % (
                    urlobj.site_ip,urlobj.get_ip_type())
            return cache_and_send(urlobj,{'code':412,'msg':msg},412)

        try:
            url_data = urlobj.get_status()
            if not created and urlobj.update_time < urlobj.last_time_checked:
                return cache_and_send(urlobj,{'code': 304,
                    'msg': 'OK', 'data': url_data},304)
            else:
                return cache_and_send(urlobj,{'code': 200, 'msg': 'OK',
                    'data': url_data},200)
        except Exception, err:
            sentry_id = client.get_ident(client.captureException())
            logger.error(u"Error processing url %s [Sentry ID '%s']",
                url,sentry_id)
            return cache_and_send(urlobj,{'code':500,'msg':'Server error',
                'error':unicode(err)},500)
Exemplo n.º 16
0
 def obj_get_list(self, bundle, **kwargs):
     try:
         raise Exception('oops')
     except:
         client.captureException()
     return []
Exemplo n.º 17
0
Arquivo: urls.py Projeto: magopian/tcp
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.conf import settings
from django.conf.urls import url, patterns, include
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.http import HttpResponse, HttpResponsePermanentRedirect

from ratelimitbackend import admin
from ratelimitbackend.forms import AuthenticationForm
from raven.contrib.django.models import client

admin.autodiscover()

client.captureException()  # raven (sentry client)


robots = lambda _: HttpResponse('User-agent: *\nDisallow:\n',
                                mimetype='text/plain')

humans = lambda _: HttpResponse(u"""/* TEAM */
Main developer: Mathieu Agopian
Contact: mathieu.agopian [at] gmail.com
Twitter: @magopian
From: France

/* SITE */
Language: English
Backend: Django, PostgreSQL
Frontend: SCSS, Compass
Exemplo n.º 18
0
    def update_status(self):
        """
        Update URL fields. This method uses socket to get IP of url and python
        requests to get request info.

        PRIVATE IPs will not be checked by default. To check PRIVATE IPs set
        URLCHECK_ALLOW_PRIVATE_IPS to True in settings.python

        RESERVED, SPECIALPURPOSE and LOOPBACK IPs will not be checked aniway.

        """
        try:
            logger.debug(u"Checking url %s for user %s.",self.url,self.user.username)

            url = urlparse.urlparse(self.url)
            self.hostname = url.hostname
            if self.hostname:
                self.site_ip = socket.gethostbyname(self.hostname)
                self.site_fqdn = socket.getfqdn(self.hostname)
            if self.get_ip_type() in ["RESERVED","SPECIALPURPOSE","LOOPBACK"]:
                logger.warn(u"%s is a %s IP and will not be checked.",self.site_ip,
                self.get_ip_type())
                return
            if not getattr(settings,"URLCHECK_ALLOW_PRIVATE_IPS",False) and \
                self.get_ip_type() == "PRIVATE":
                logger.warn(u"%s is a %s IP and will not be checked. Set "\
                "URLCHECK_ALLOW_PRIVATE_IPS in settings.py to check this IPs.",
                self.site_ip,self.get_ip_type())
                return
            try:
                request = requests.get(self.url)
                self.web_server_name = request.headers.get('server','')
                self.html_status_code = request.status_code
            except requests.exceptions.Timeout:
                self.html_status_code = request.codes.timeout

            self.last_time_checked = timezone.now()

            old = URL.objects.get(pk=self.pk)

            # some field is different, update
            if self.site_ip != old.site_ip or \
                    self.site_fqdn != old.site_fqdn or \
                    self.web_server_name != old.web_server_name or \
                    self.html_status_code != old.html_status_code:

                self.update_time = self.last_time_checked

                # create a new history entry
                self.history.create(
                    update_time = self.update_status,
                    site_ip = self.site_ip,
                    site_fqdn = self.site_fqdn,
                    web_server_name = self.web_server_name,
                    html_status_code = self.html_status_code
                )
                logger.info(u"Updated url %s for user %s.",self.url,self.user.username)
            self.save()
        except Exception as err:
            sentry_id = client.get_ident(client.captureException())
            logger.error(u"Error on update url status [Sentry ID '%s']",
                sentry_id)
        # remove key from cache so next request to status cache response again
        cache_key = "%s:%s" % (self.user.username,self.url)
        cache.delete(cache_key)
Exemplo n.º 19
0
def update_url_info(url,timeout=10):
    """ Update url status from URLCHECKER_SERVER.
        It update URLStatusHistory table and URLCHECKER_SERVER
    """
    server = getattr(settings,'URLCHECKER_SERVER')
    key = getattr(settings,'URLCHECKER_KEY')

    # it already is checked on __init__.py
    if not server or not key:
        raise ImproperlyConfigured(u"URLCHECKER: Configure URLCHECKER_SERVER "\
            "and URLCHECKER_KEY on settings.py.")

    try:
        URLValidator()(url)
    except ValidationError:
        raise ValidationError(u"'%s' is one invalid URL. Update aborted.")

    try:
        url_request = requests.post(server,data={'key':key,'url':url},
            timeout=timeout)
    except request.TIMEOUT as err:
        # capture exception to raven and try again
        sentry_id = client.get_ident(client.captureException())
        logger.error(u"Timeout processing url %s [Sentry ID '%s']",
            url,sentry_id)
    else:
        if url_request.status_code in (200,304):
            # request without errors
            urldata = url_request.json['data']
            print urldata
            urlstatus, created = URLStatusHistory.objects.get_or_create(url=url)
            urlstatus.update_time = datetime.datetime.now(pytz.utc)
            urlstatus.site_ip = urldata['site_ip']
            urlstatus.site_fqdn = urldata['site_fqdn']
            urlstatus.web_server_name = urldata['web_server_name']
            urlstatus.html_status_code = urldata['html_status_code']
            urlstatus.save()

            cache_key = 'urlchecker.client_%s' % md5(url).hexdigest()
            cache.set(cache_key,urlstatus,300)

        else:
            try:
                url_request.raise_for_status()
            except requests.HTTPError, err:
                # something happened, create a error in sentry if error is
                # unknow
                send_to_sentry=True

                if url_request.status_code == 412 and \
                        url_request.json.has_key('msg') and \
                        "is a PRIVATE IP and" in url_request.json['msg']:
                    # know error, is a internal IP, we just get url info
                    update_internal_url_info(url)
                    send_to_sentry=False

                if send_to_sentry:
                    import ipdb
                    ipdb.set_trace()
                    extra = {
                        'url': url,
                        'urlobj_id': getattr(urlobj,'id',None),
                        'code': url_request.status_code,
                        'method': 'post',
                        'json': url_request.json,
                        'text': url_request.text,
                        'request_dict': url_request.__dict__
                    }
                    sentry_id = client.get_ident(client.captureException(extra=extra))
                    logger.error(u"HTTP error %s on update_url_info. Msg was '%s'",
                        url_request.status_code,url_request.json.get('msg'))
Exemplo n.º 20
0
    def save(self, commit=True):
        # set user
        if self.user.is_authenticated():
            self.instance.user = self.user
        else:
            user = User.objects.filter(
                email=self.email_form.cleaned_data['email'].lower(),
            ).first() or User(
                email=self.email_form.cleaned_data['email'].lower(),
            )
            while not user.pk:
                user.username = get_random_string()
                user.set_password(get_random_string())
                try:
                    user.save()
                except IntegrityError:
                    # on duplicit username try again
                    pass
            self.instance.user = user

        # set price
        self.instance.price = (
            self.instance.subject_variant.price
            if self.instance.subject_variant
            else self.instance.subject.price
        )
        self.instance.answers = dumps(self.questions_form.cleaned_data)
        super(RegistrationForm, self).save(commit)

        # send mail
        try:
            self.instance.send_mail()
        except:
            import traceback
            from raven.contrib.django.models import client
            traceback.print_exc()
            client.captureException()

        # save / update participant
        if self.participant_select_form.cleaned_data['participant'] == 'new':
            try:
                participant = Participant.objects.get(
                    user        = self.instance.user,
                    birth_num   = self.instance.participant.birth_num,
                )
            except Participant.DoesNotExist:
                participant = Participant()
                participant.user = self.instance.user
        else:
            participant = Participant.objects.get(id=self.participant_select_form.cleaned_data['participant'])
        for attr in ['first_name', 'last_name', 'birth_num', 'age_group', 'street', 'city', 'postal_code',
                     'citizenship', 'phone', 'email', 'school', 'school_other', 'school_class', 'health']:
            setattr(participant, attr, getattr(self.instance.participant, attr))
        participant.save()

        # save / update first parent
        if self.instance.parent1:
            if self.parent1_select_form.cleaned_data['parent'] == 'new':
                parent = Parent()
                parent.user = self.instance.user
            else:
                parent = Parent.objects.get(id=self.parent1_select_form.cleaned_data['parent'])
            for attr in ['first_name', 'last_name', 'street', 'city', 'postal_code', 'phone', 'email']:
                setattr(parent, attr, getattr(self.instance.parent1, attr))
            parent.save()

        # save / update second parent
        if self.instance.parent2:
            if self.parent2_select_form.cleaned_data['parent'] == 'new':
                parent = Parent()
                parent.user = self.instance.user
            else:
                parent = Parent.objects.get(id=self.parent2_select_form.cleaned_data['parent'])
            for attr in ['first_name', 'last_name', 'street', 'city', 'postal_code', 'phone', 'email']:
                setattr(parent, attr, getattr(self.instance.parent2, attr))
            parent.save()

        for agreement_form in self.agreement_forms:
            for option_id, checked in agreement_form.cleaned_data.items():
                if checked:
                    self.instance.agreement_options.add(agreement_form.options[option_id])