Esempio n. 1
0
def get_user(user=None):
    if user == 'me':
        if current_user.is_authenticated:
            return jsonify(
                {'user': serialize(current_user._get_current_object())})
        else:
            print(current_user.is_authenticated)
            return jsonify({'message': 'You need to be logged in'}), 403
    user_info = User.query.filter_by(username=user).first_or_404()
    return jsonify({"user": serialize(user_info)})
Esempio n. 2
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!"
    })
Esempio n. 3
0
def get_posts(user=None):
    user = User.query.filter_by(username=user).first_or_404()
    filter = request.args.get('filter')
    if filter == 'following':
        posts = [
            serialize(post) for followee in user.following
            for post in followee.posts.all() if followee.posts.all()
        ]
        posts = [post for post in posts if post['published']]
    else:
        posts = [
            serialize(post) for post in user.posts.all() if post.published
        ]
    category = request.args.get('category')
    if category:
        posts = [post for post in posts if post['category'] == category]
    return jsonify({'posts': posts})
Esempio n. 4
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)})
Esempio n. 5
0
def discover_stories():
    posts = Post.query.all()
    posts = [serialize(post) for post in posts if post.published]
    category = request.args.get('category')
    if category:
        posts = [post for post in posts if post['category'] == category]
    sort = request.args.get('sort')
    posts = sorted(posts, key=itemgetter('date'), reverse=sort == 'new')
    return jsonify({'posts': posts})
Esempio n. 6
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())})
Esempio n. 7
0
def get_post(id=None):
    post = Post.query.filter_by(id=id).first()
    serialized_post = serialize(post)
    if current_user.is_authenticated:
        reactions = current_user.reactions
        reaction = next((
            reaction for reaction in reactions
            if reaction.user_id == current_user.id and reaction.post_id == id),
                        None)
        if reaction:
            serialized_post['reacted'] = reaction.type
            serialized_post['reacted_id'] = reaction.id
    return jsonify(serialized_post)
Esempio n. 8
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)})
Esempio n. 9
0
def get_suggestion(text=None, size=None, id=None):
    if size is None:
        size = 5
    if text is None:
        text = ''
    if id:
        post = Post.query.filter_by(id=id).first()
        text = post.content
    query = get_more_like_this_query(text, size)
    try:
        results = es.search(index="kot_front", body=query)
        print(results)
        posts = [
            Post.query.filter_by(id=result["_id"]).first()
            for result in results['hits']['hits']
        ]
        posts = [post for post in posts if post is not None]
        return jsonify(serialize(posts))
    except:
        return {}
Esempio n. 10
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
Esempio n. 11
0
def react(id=None):
    post = Post.query.filter_by(id=id).first_or_404()

    from_author = post.user_id == current_user.id
    data = request.get_json()
    reaction_type = data.get('type')
    reaction_exists = next((reaction for reaction in post.reactions.all()
                            if reaction.user_id == current_user.id), None)

    if reaction_exists:
        reaction = reaction_exists
        reaction.type = reaction_type
    else:
        reaction = Reaction(user_id=current_user.id,
                            post_id=id,
                            type=reaction_type,
                            from_author=from_author)
        post.reactions.append(reaction)
    db.session.commit()
    return jsonify({
        'message': 'react successful',
        'reaction': serialize(reaction)
    })
Esempio n. 12
0
def get_donations(user=None):
    donations = Transaction.query.filter_by(to_user=current_user.id).all()

    return jsonify({"donations": serialize(donations)})
Esempio n. 13
0
def get_comments(user=None, post_id=None):
    post = Post.query.filter_by(id=post_id).first_or_404()
    return jsonify({'comments': serialize(post.comments.all())})
Esempio n. 14
0
def get_post_reactions(user=None, post_id=None):
    post = Post.query.filter_by(id=post_id).first_or_404()
    reactions = post.reactions.all()
    return jsonify({"reactions": serialize(reactions)})
Esempio n. 15
0
def get_reaction(user=None, post_id=None, reaction_id=None):
    post = Post.query.filter_by(id=post_id).first_or_404()
    reaction = post.reactions.filter_by(id=reaction_id).first_or_404()
    return jsonify({"reaction": serialize(reaction)})
Esempio n. 16
0
def get_comment(user=None, post_id=None, comment_id=None):
    post = Post.query.filter_by(id=post_id).first_or_404()
    comment = post.comments.filter_by(id=comment_id).first_or_404()
    return jsonify(serialize(comment))
Esempio n. 17
0
def get_all_posts():
    posts = Post.query.all()
    return jsonify(serialize(posts))