def paste_details(): """ Retrieve details for a particular paste ID. """ data = flask.request.get_json() try: paste = database.paste.get_paste_by_id(util.cryptography.get_decid(data['paste_id']), active_only=True) attachments = database.attachment.get_attachments_for_paste(util.cryptography.get_decid(data['paste_id']), active_only=True) paste_details_dict = paste.as_dict() paste_details_dict['poster_username'] = '******' paste_details_dict['attachments'] = [ attachment.as_dict() for attachment in attachments ] if paste.user_id: poster = database.user.get_user_by_id(paste.user_id) paste_details_dict['poster_username'] = poster.username if not paste.password_hash or (data.get('password') and paste.password_hash == util.cryptography.secure_hash(data.get('password'))): return flask.jsonify({ constants.api.RESULT: constants.api.RESULT_SUCCESS, constants.api.MESSAGE: None, 'details': paste_details_dict, }), constants.api.SUCCESS_CODE else: return flask.jsonify({ constants.api.RESULT: constants.api.RESULT_FAULURE, constants.api.MESSAGE: 'Password-protected paste: either no password or wrong password supplied', constants.api.FAILURE: 'password_mismatch_failure', 'details': {}, }), constants.api.AUTH_FAILURE_CODE except (PasteDoesNotExistException, UserDoesNotExistException, InvalidIDException): return flask.jsonify(constants.api.NONEXISTENT_PASTE_FAILURE), constants.api.NONEXISTENT_PASTE_FAILURE_CODE except: return flask.jsonify(constants.api.UNDEFINED_FAILURE), constants.api.UNDEFINED_FAILURE_CODE
def top_pastes(): """ Get details for the top pastes. """ try: data = flask.request.get_json() return flask.jsonify({ constants.api.RESULT: constants.api.RESULT_SUCCESS, constants.api.MESSAGE: None, 'pastes': [ paste.as_dict() for paste in database.paste.get_top_pastes(data['page_num'], data['num_per_page']) ], }), constants.api.SUCCESS_CODE except: return flask.jsonify(constants.api.UNDEFINED_FAILURE), constants.api.UNDEFINED_FAILURE_CODE
def pastes_for_user(): """ Get all pastes for the currently logged in user. """ try: return flask.jsonify({ constants.api.RESULT: constants.api.RESULT_SUCCESS, constants.api.MESSAGE: None, 'pastes': [ paste.as_dict() for paste in database.paste.get_all_pastes_for_user(current_user.user_id, active_only=True) ], }), constants.api.SUCCESS_CODE except: return flask.jsonify(constants.api.UNDEFINED_FAILURE), constants.api.UNDEFINED_FAILURE_CODE