示例#1
0
def save_post():
    data = get_request_data(request)
    content = data.get('content')
    title = data.get('title')
    preview = data.get('preview')
    category = data.get('category')
    featured = data.get('featured')
    try:
        published = data.get('published')
    except KeyError:
        published = True
    try:
        post = Post(user_id=current_user.id,
                    content=content,
                    title=title,
                    preview=preview,
                    category=category,
                    featured=featured,
                    published=published)
        if 'image' in request.files:
            save_img(request.files['image'], post, current_user.id, post.id)
        db.session.add(post)
        db.session.commit()
        index_post(post)
    except IntegrityError:
        db.session.rollback()
        return jsonify({'message': 'Invalid parameters!'}), 400
    return jsonify({
        "post": serialize(post),
        "message": "Put post successfully!"
    })
示例#2
0
def make_donation(user=None):
    data = get_request_data(request)
    amount = data.get('amount')
    user_id = User.query.filter_by(username=user).first().id
    transaction = Transaction(to_user=user_id, amount=amount)
    db.session.add(transaction)
    db.session.commit()
    return jsonify({"donation": serialize(transaction)})
示例#3
0
def reset_token():
    if current_user.is_authenticated:
        return jsonify({'message': 'Authenticated'}), 403
    data = get_request_data(request)
    email = data.get('email')
    user = User.query.filter_by(email=email).first()
    if user:
        send_reset_email(user)
    return jsonify({'message': ''})
示例#4
0
def update_user(user=None):
    user = current_user
    print(user)
    data = get_request_data(request)
    if 'image' in request.files:
        save_img(request.files['image'], user, user.id)
    for key, value in data.items():
        user.update(key, value)
    db.session.commit()
    return jsonify({"user": serialize(current_user._get_current_object())})
示例#5
0
def edit_comment(user=None, post_id=None, comment_id=None):
    data = get_request_data(request)
    user_data = User.query.filter_by(username=user).first_or_404()
    user_posts = user_data.posts
    post = user_posts.filter_by(id=post_id).first_or_404()
    comment = post.comments.filter_by(id=comment_id).first_or_404()
    for key, value in data.items():
        comment.update(key, value)
        db.session.commit()
    return jsonify({'message': 'Edited comment'})
示例#6
0
def verify_token():
    if current_user.is_authenticated:
        return jsonify({'message': 'Authenticated'}), 403
    data = get_request_data(request)
    token = data.get('token')
    user = verify_reset_token(token)
    if user:
        return jsonify({'status': True})
    else:
        return jsonify({'status': False}), 200
示例#7
0
def register():
    if current_user.is_authenticated:  # if user is logged in, register shouldn't be accessible
        return jsonify({'message': 'Already logged in'}), 403

    data = get_request_data(request)
    email = data.get('email')
    username = data.get('username')
    password = data.get('password')
    if not (email and username
            and password):  # checking if necessary credentials were provided
        return jsonify({'message': 'Missing credentials'}), 403
    return register_user(username, email, password)
示例#8
0
def refresh_post(id=None):
    post = Post.query.filter_by(id=id).first_or_404()
    if post.user_id == current_user.id:
        data = get_request_data(request)
        if 'image' in request.files:
            save_img(request.files['image'], post, current_user.id, id)
        for key, value in data.items():
            post.update(key, value)
        db.session.commit()
        update_index(post)
        return jsonify({"message": 'Updated post!'})
    else:
        return jsonify({"message": 'unauthorized!'}), 403
示例#9
0
def post_comment(user=None, post_id=None):
    data = get_request_data(request)
    user_data = User.query.filter_by(username=user).first_or_404()
    post = user_data.posts.filter_by(id=post_id).first_or_404()
    content = data.get('content')
    if not content:
        return jsonify({'message': 'invalid content'})
    user_id = current_user.id
    from_author = user_id == post.user_id
    comment = Comment(user_id=user_id,
                      post_id=post_id,
                      content=content,
                      from_author=from_author)
    db.session.add(comment)
    db.session.commit()
    return jsonify({'comment': serialize(comment)})
示例#10
0
def request_reset_token():
    data = get_request_data(request)
    if current_user.is_authenticated:
        return jsonify({'message': 'Authenticated'}), 403
    user = verify_reset_token(data.get('token'))
    if user:
        try:
            password = data.get('form')['password']
            password_hash = bcrypt.generate_password_hash(password)
            user.password_hash = password_hash
            db.session.commit()
            return jsonify({'message': 'Valid token'})
        except (KeyError, IntegrityError) as e:
            print(e)
            db.session.rollback()
            return {'message': 'invalid parameters!'}, 400
    else:
        return jsonify({'message': 'Invalid token'}), 403
示例#11
0
def log_in():
    if current_user.is_authenticated:  # if user is logged in register shouldn't be accessible
        return jsonify({'message': 'Already authenticated'}), 200

    data = get_request_data(request)
    print(data)
    email = data['email']
    password = data['password']
    remember_me = data['remember_me']
    user = User.query.filter_by(email=email).first()  # checking if user exists
    print(bcrypt.generate_password_hash(password).decode('utf-8'))
    if user and bcrypt.check_password_hash(user.password_hash, password):

        login_user(user, remember=bool(remember_me))
        return jsonify({
            'user': serialize(user),
            'token': get_token(user)
        }), 200
    else:
        return jsonify({'message': 'Invalid credentials!'}), 401