Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #880. Allow visiting /activity/username route directly. #928

Merged
merged 4 commits into from
Mar 1, 2016

Conversation

miketaylr
Copy link
Member

r? @karlcow

(sorry for taking so long here @magsout)

Mike Taylor added 2 commits February 29, 2016 18:22
This won't work now for seeing other people's activity, but
before you just got an error page. This also fixes the bug where
logging in after the error had a missing avatar image.
@miketaylr
Copy link
Member Author

Let's not merge yet, I should write a test or two for this.

@karlcow
Copy link
Member

karlcow commented Mar 1, 2016

r+ @miketaylr

not to self. Need to refactor this errorhandler think. We are reusing the same code over and over again.
Probably an opportunity for a function around this code:

    if (request.path.startswith('/api/') and
       request.accept_mimetypes.accept_json and
       not request.accept_mimetypes.accept_html):
        message = {
            'status': 400,
            'message': 'API call. ' + message,
        }
        resp = jsonify(message)
        resp.status_code = 400

More something like (but to think not completely satisfied with the code below)

ERROR_DICT = {
    400: 'Bad Request.',
    401: 'Unauthorized. Please log in.',
    403: 'Forbidden. Maybe that looking at private stuff?',
    404: 'Not Found. Lost in Punk Cat Space',
    }


@app.errorhandler(400)
@app.errorhandler(401)
@app.errorhandler(403)
@app.errorhandler(404)
def custom_error_handler(err):
    if api_call(request):
        return api_message(err.code)
    return render_template(
        'error.html',
        error_code=err.code,
        error_message=ERROR_DICT[err.code])


def api_call(request):
    '''Checks if it's an API call'''
    if (request.path.startswith('/api/') and
       request.accept_mimetypes.accept_json and
       not request.accept_mimetypes.accept_html):
        return True
    else:
        return False


def api_message(code):
    '''Prepares HTTP response for API calls.'''
    message = {
        'status': code,
        'message': ERROR_DICT[code],
    }
    resp = jsonify(message)
    resp.status_code = 404
    return resp

@miketaylr
Copy link
Member Author

thanks @karlcow -- I agree, we could abstract the error handling into a nice little method. Will fix the failing nose test and write some new ones before merging.

This will get replaced with a functional test that can log in.
(the trivial fix would be to change this to expect 401, but
that would just be a duplicate of the next test)
@miketaylr
Copy link
Member Author

OK, tests are passing -- let's merge. Thanks for review @karlcow.

miketaylr pushed a commit that referenced this pull request Mar 1, 2016
Fixes #880. Allow visiting /activity/username route directly.
@miketaylr miketaylr merged commit 95d831d into master Mar 1, 2016
@miketaylr miketaylr deleted the issues/880/1 branch March 1, 2016 17:02
@@ -278,6 +295,24 @@ def jumpship(e):
return redirect(url_for('index'))


@app.errorhandler(400)
def unauthorized(err):

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants