Example #1
0
def modify_post(post_id):
    post = Find.post(Post.id, post_id)
    if not post:
        return make_response(jsonify({'error': 'Post not found'}), 404)
    if post.author_id != Find.user(User.username, auth.username()).id:
        return make_response(jsonify({'error': 'You do not have permission to edit this post'}), 401)

    if request.method == 'PUT':
        errors = get_post_errors()
        if errors:
            return errors
        PostTable.update(post_id)
        return jsonify({'message': 'Post updated successfully'})

    if request.method == 'DELETE':
        PostTable.delete(post_id)
        return jsonify({'message': 'Post deleted successfully'})
Example #2
0
def register():
    response = request.get_json()
    if response:
        if 'username' in response.keys() and 'password' in response.keys():
            username, password = response['username'], response['password']
            if valid_username(username) and valid_password(password):
                create_user(username, password)
                return jsonify({'message': 'Registration successful'})
    return make_response(jsonify({'error': 'Invalid username or password'}),
                         400)
Example #3
0
def get_post_errors():
    response = get_post_response()
    if response:
        title, body = response
        if not ValidPost.title(title):
            return jsonify({'error': 'Invalid title'})
        if not ValidPost.body(body):
            return jsonify({'error': 'Invalid body'})
        return None
    return make_response({'error': 'Malformed request'}, 400)
def unauthorized():
    return make_response(jsonify({'error': 'Unauthorized access'}), 401)
Example #5
0
def get_post(post_id):
    post = Find.post(Post.id, post_id)
    if post:
        return jsonify(post_to_dict(post))
    return make_response(jsonify({'error': 'Post not found'}), 404)