Exemplo n.º 1
0
 def obj_create(self, bundle, request=None, **kwargs):
     try:
         bundle.obj = PaymentAccount.create_bango(
             request.amo_user, bundle.data)
     except HttpClientError as e:
         log.error('Client error create Bango account; %s' % e)
         raise ImmediateHttpResponse(
             http.HttpApplicationError(json.dumps(e.content)))
     except HttpServerError as e:
         log.error('Error creating Bango payment account; %s' % e)
         raise ImmediateHttpResponse(http.HttpApplicationError(
             _(u'Could not connect to payment server.')))
     return bundle
Exemplo n.º 2
0
def json_response(request, exc_type, exc_value, tb):
    # If you are doing requests in debug mode from say, curl,
    # it's nice to be able to get some JSON back for an error, not a
    # gazillion lines of HTML.
    if request.META['CONTENT_TYPE'] == 'application/json':
        return http.HttpApplicationError(
            content=json.dumps({
                'traceback': traceback.format_tb(tb),
                'type': str(exc_type),
                'value': str(exc_value)
            }),
            content_type='application/json; charset=utf-8')

    return old(request, exc_type, exc_value, tb)
Exemplo n.º 3
0
def handle_500(resource, request, exception):
    # Print some nice 500 errors back to the clients if not in debug mode.
    tb = traceback.format_tb(sys.exc_traceback)
    tasty_log.error('%s: %s %s\n%s' % (request.path,
                        exception.__class__.__name__, exception,
                        '\n'.join(tb)),
                    extra={'status_code': 500, 'request': request})
    data = {
        'error_message': str(exception),
        'error_code': getattr(exception, 'id',
                              exception.__class__.__name__),
        'error_data': getattr(exception, 'data', {})
    }
    serialized = resource.serialize(request, data, 'application/json')
    return http.HttpApplicationError(content=serialized,
                content_type='application/json; charset=utf-8')
Exemplo n.º 4
0
def handle_500(request, exception):
    # Print some nice 500 errors back to the clients if not in debug mode.
    tb = traceback.format_tb(sys.exc_traceback)
    tasty_log.error(
        '%s: %s %s\n%s' %
        (request.path, exception.__class__.__name__, exception, '\n'.join(tb)),
        extra={
            'status_code': 500,
            'request': request
        },
        exc_info=sys.exc_info())
    data = {
        'error_message': str(exception),
        'error_code': getattr(exception, 'id', exception.__class__.__name__),
        'error_data': getattr(exception, 'data', {})
    }
    # We'll also cef log any errors.
    log_cef(str(exception), request, severity=3)
    return http.HttpApplicationError(
        content=json.dumps(data),
        content_type='application/json; charset=utf-8')
Exemplo n.º 5
0
    def aip_store_callback_request(self, request, bundle, **kwargs):
        package = bundle.obj

        callbacks = Callback.objects.filter(event="post_store")
        if len(callbacks) == 0:
            return http.HttpNoContent()

        fail = 0

        if package.is_compressed:
            # Don't extract the entire AIP, which could take forever;
            # instead, just extract bagit.txt and manifest-sha512.txt,
            # which is enough to get bag.entries with the
            # precalculated sha512 checksums
            try:
                basedir = package.get_base_directory()
            # Currently we only support this for local packages.
            except NotImplementedError:
                return http.HttpNoContent()

            _, tmpdir = package.extract_file(os.path.join(
                basedir, 'bagit.txt'))
            package.extract_file(os.path.join(basedir, 'manifest-sha512.txt'),
                                 extract_path=tmpdir)

            package_dir = os.path.join(tmpdir, basedir)
        else:
            package_dir = package.full_path()
            tmpdir = None

        safe_files = ('bag-info.txt', 'manifest-sha512.txt', 'bagit.txt')

        bag = bagit.Bag(package_dir)
        for f, checksums in bag.entries.iteritems():
            try:
                cksum = checksums['sha512']
            except KeyError:
                # These files do not typically have an sha512 hash, so it's
                # fine for these to be missing that key; every other file should.
                if f not in safe_files:
                    LOGGER.warning(
                        "Post-store callback: sha512 missing for file %s", f)
                continue

            files = File.objects.filter(checksum=cksum, stored=False)
            if len(files) > 1:
                LOGGER.warning("Multiple File entries found for sha512 %s",
                               cksum)

            for file_ in files:
                for callback in callbacks:
                    uri = callback.uri.replace('<source_id>', file_.source_id)
                    try:
                        callback.execute(uri)
                        file_.stored = True
                        file_.save()
                    except CallbackError:
                        fail += 1

        if tmpdir is not None:
            shutil.rmtree(tmpdir)

        if fail > 0:
            response = {
                "message":
                "Failed to POST {} responses to callback URI".format(fail),
                "failure_count":
                fail,
                "callback_uris": [c.uri for c in callbacks]
            }
            return http.HttpApplicationError(json.dumps(response),
                                             mimetype="application/json")
        else:
            return http.HttpNoContent()