Exemplo n.º 1
0
    def test_response(self):
        res = pyw4c_response(message="test", status=HTTP_CONFLICT)
        self.assertEqual(res.status_code, HTTP_CONFLICT)
        json_content = json.loads(res.content)
        self.assertTrue(json_content['error'])
        self.assertIn("test", json_content['message'])

        res = pyw4c_response(status=HTTP_OK)
        self.assertEqual(res.status_code, HTTP_OK)
        json_content = json.loads(res.content)
        self.assertFalse(json_content['error'])
Exemplo n.º 2
0
def new(request):
    """Create a new short url based on the POST parameters"""
    long_url = request.GET.get('long_url')

    if long_url is None:
        is_valid, error_message = False, "Missing GET parameter: 'long_url'"
    else:
        is_valid, error_message = validate_url(long_url)

    if not is_valid:
        return pyw4c_response(status=HTTP_BAD_REQUEST, message=error_message)

    params = {}

    for key in ['short_path', 'prefix']:
        params[key] = request.GET.get(key)

        if key == 'prefix' and 'allow_slashes_in_prefix' in request.GET:
            continue

        if params[key] is not None and '/' in params[key]:
            return pyw4c_response(
                status=HTTP_BAD_REQUEST,
                message="%s may not contain a '/' character." % key)

    statsd.increment(
        'workforus.new',
        tags=['prefix:' + unicode(params['prefix']), 'is_secure:' + unicode(request.is_secure())])

    try:
        link = Link.shorten(long_url, **params)

        getLogger('app').info(
            'Successfully shortened %s into %s for user %s',
            link.long_url, link.hash, request.user.login)
    except ShortPathConflict, err:
        del params['short_path'], long_url
        del params['prefix']

        params['hash'] = err.hash

        return pyw4c_response(status=HTTP_CONFLICT, message=str(err), **params)
Exemplo n.º 3
0
    statsd.increment(
        "workforus.new", tags=["prefix:" + unicode(params["prefix"]), "is_secure:" + unicode(request.is_secure())]
    )

    try:
        link = Link.shorten(long_url, **params)

        getLogger("app").info(
            "Successfully shortened %s into %s for user %s", link.long_url, link.hash, request.user.login
        )
    except ShortPathConflict, err:
        del params["short_path"], long_url
        del params["prefix"]

        params["hash"] = err.hash

        return pyw4c_response(status=HTTP_CONFLICT, message=str(err), **params)
    except InvalidHashException, err:
        getLogger("app").error(str(err))

        return pyw4c_response(
            status=HTTP_FORBIDDEN if isinstance(err, ForbiddenKeyword) else HTTP_BAD_REQUEST, message=str(err), **params
        )

    params["short_path"] = link.hash.split("/")[-1]

    params["short_url"] = link.build_absolute_uri(request)

    return pyw4c_response(**params)