Exemple #1
0
def create_post():
    form = BlogPostForm()

    if (form.validate_on_submit() or request.method == "POST"):
        post = BlogPost(title=form.title.data,
                        category=form.category.data,
                        text=form.text.data,
                        user_id=current_user.id)
        db.session.add(post)
        db.session.commit()

        followers = Followers.query.filter_by(
            followed_id=current_user.id).all()

        for follower in followers:
            notif = Notifications(
                follower.follower_id,
                f'{current_user.username} has posted a blog "{form.title.data}"!',
                post.id, True)
            db.session.add(notif)

        db.session.add(post)
        db.session.commit()

        flash('Post Created!')

        return redirect(url_for('core.index'))

    if (current_user.is_authenticated):
        notifs = Notifications.query.filter_by(
            user_id=current_user.id).order_by(Notifications.date.desc()).all()
    else:
        notifs = []

    return render_template('create_post.html', form=form, notifs=notifs)
Exemple #2
0
def index():
    View.delete_expired()
    Notifications.delete_expired()

    page = request.args.get('page', 1, type=int)

    blog_posts = BlogPost.query.order_by(BlogPost.views.desc(),
                                         BlogPost.date.desc()).paginate(
                                             page=page, per_page=6)
    if (current_user.is_authenticated):
        ids = db.engine.execute(f'select blog_id        \
                                  from View             \
                                  where user_id={current_user.id}')
        viewed = [id_blog[0] for id_blog in ids]
        categories = [
            current_user.last_viewed_catagory1,
            current_user.last_viewed_catagory2,
            current_user.last_viewed_catagory3
        ]
        recommended = BlogPost.query.filter(
            BlogPost.category.in_(categories), BlogPost.author != current_user,
            ~(BlogPost.id.in_(viewed))).order_by(
                BlogPost.views.desc(),
                BlogPost.date.desc()).paginate(page=page,
                                               per_page=3,
                                               error_out=False)
    else:
        recommended = None

    if (current_user.is_authenticated):
        notifs = Notifications.query.filter_by(
            user_id=current_user.id).order_by(Notifications.date.desc()).all()
    else:
        notifs = []

    form = Search_Form()

    if form.validate_on_submit():
        return redirect(url_for('core.search_page', param=form.param.data))

    return render_template('index.html',
                           page_name="Home",
                           blog_posts=blog_posts,
                           recommended=recommended,
                           notifs=notifs,
                           form=form)
Exemple #3
0
def create_notify(sender, post, action_type, reciever):
    print('create_notify')
    # try :
    print('friend found')
    notif = Notifications.add_nofi(sender, post, action_type, reciever)
    print('reciver added')
    print(action_type)
    print('done')
Exemple #4
0
def like(user_id, blog_post_id, like):
    post = BlogPost.query.get_or_404(blog_post_id)

    if (not current_user.is_authenticated or post.user_id == current_user.id):
        return redirect(
            url_for('blog_posts.blog_post', blog_post_id=blog_post_id))

    like_entry = Likes.query.filter_by(user_id=user_id,
                                       blog_id=blog_post_id).first()

    blog = BlogPost.query.get_or_404(blog_post_id)
    user = User.query.get_or_404(blog.author.id)
    user_reaction = User.query.get_or_404(user_id)

    if (not like_entry):
        like_entry = Likes(user_id, blog_post_id, bool(like))
        db.session.add(like_entry)

        notif = Notifications(
            user.id,
            f'{user_reaction.username} has reacted to your blog "{blog.title}"!',
            blog_post_id, True)
        db.session.add(notif)

    else:
        if (like_entry.like != bool(like)):
            like_entry.like = bool(like)

            notif = Notifications(
                user.id,
                f'{user_reaction.username} has reacted to your blog "{blog.title}"!',
                blog_post_id, True)
            db.session.add(notif)

        else:
            db.session.delete(like_entry)

    db.session.commit()

    return redirect(url_for('blog_posts.blog_post', blog_post_id=blog_post_id))
Exemple #5
0
def unfollow(user_id_1, user_id_2):
    data = Followers.query.filter_by(follower_id=user_id_1,
                                     followed_id=user_id_2).first()
    user = User.query.get_or_404(user_id_2)

    if (not data):
        flash(f"You don't follow {user.username}!")
    else:
        db.session.delete(data)

        notif = Notifications(user_id_1,
                              f'You stopped following {user.username}!',
                              user_id_2, False)
        db.session.add(notif)

        db.session.commit()

        flash(f'You unfollowed {user.username}!')

    return redirect(url_for('user.user_posts', user_id=user.id))
Exemple #6
0
def update(blog_post_id):
    blog_title = None
    post = BlogPost.query.get_or_404(blog_post_id)

    if (post.author != current_user):
        abort(403)

    form = BlogPostForm()

    if (form.validate_on_submit() or request.method == "POST"):
        post.title = form.title.data
        post.text = form.text.data
        post.category = form.category.data

        followers = Followers.query.filter_by(
            followed_id=current_user.id).all()

        for follower in followers:
            notif = Notifications(
                follower.follower_id,
                f'{current_user.username} has updated the blog "{blog_title}"!',
                post.id, True)
            db.session.add(notif)

        db.session.commit()
        flash('Updated Post!')
        return redirect(url_for('blog_posts.blog_post', blog_post_id=post.id))

    if (request.method == "GET"):
        form.title.data = post.title
        blog_title = post.title
        form.text.data = post.text
        form.category.data = post.category

    if (current_user.is_authenticated):
        notifs = Notifications.query.filter_by(
            user_id=current_user.id).order_by(Notifications.date.desc()).all()
    else:
        notifs = []

    return render_template('create_post.html', form=form, notifs=notifs)
Exemple #7
0
def follow(user_id_1, user_id_2):
    data = Followers.query.filter_by(follower_id=user_id_1,
                                     followed_id=user_id_2).all()
    user = User.query.get_or_404(user_id_2)

    if (data):
        flash(f"You are already following {user_id_2}!")
    else:
        data = Followers(user_id_1, user_id_2)
        db.session.add(data)

        notif = Notifications(user_id_1,
                              f'You started following {user.username}!',
                              user_id_2, False)
        db.session.add(notif)

        db.session.commit()

        flash(f'You are following {user.username}!')

    return redirect(url_for('user.user_posts', user_id=user.id))
Exemple #8
0
def delete(blog_post_id):
    post = BlogPost.query.get_or_404(blog_post_id)

    if (post.author != current_user):
        abort(403)

    followers = Followers.query.filter_by(followed_id=current_user.id).all()

    for follower in followers:
        notif = Notifications(
            follower.follower_id,
            f'{current_user.username} has deleted the blog "{post.title}"!',
            post.author.id, False)
        db.session.add(notif)

    db.session.delete(post)
    db.session.commit()

    flash('Blog Deleted!')

    return redirect(url_for('core.index'))
Exemple #9
0
def blog_post(blog_post_id):
    post = BlogPost.query.get_or_404(blog_post_id)
    form = CommentForm()

    previous_comments = Comments.query.filter_by(blog_id=post.id).order_by(
        Comments.date.desc()).all()

    if (form.validate_on_submit() or request.method == "POST"):
        comment = Comments(blog_post_id, current_user.id, form.text.data)
        db.session.add(comment)

        if (current_user.id != post.author.id):
            notif = Notifications(
                post.author.id,
                f'{current_user.username} has commented on your blog "{post.title}"!',
                post.id, True)
            db.session.add(notif)

        db.session.commit()

        return redirect(
            url_for('blog_posts.blog_post', blog_post_id=blog_post_id))

    if (current_user.is_authenticated
            and current_user.email != post.author.email):
        user = User.query.get_or_404(current_user.id)
        user.last_viewed_catagory3 = user.last_viewed_catagory2
        user.last_viewed_catagory2 = user.last_viewed_catagory1
        user.last_viewed_catagory1 = post.category

        db.session.commit()

        view = View.query.filter_by(user_id=current_user.id,
                                    blog_id=blog_post_id).first()

        if (not view):
            post.views += 1
            view = View(current_user.id, blog_post_id)
            db.session.add(view)
            db.session.commit()

    if (current_user.is_authenticated):
        notifs = Notifications.query.filter_by(
            user_id=current_user.id).order_by(Notifications.date.desc()).all()
    else:
        notifs = []

    if (current_user.is_authenticated):
        like_stat = Likes.query.filter_by(user_id=current_user.id,
                                          blog_id=blog_post_id).first()
    else:
        like_stat = None

    like_count = db.engine.execute(f'''
        select count(*)
        from Likes
        where blog_id={blog_post_id} and like={1}
    ''')
    dislike_count = db.engine.execute(f'''
        select count(*)
        from Likes
        where blog_id={blog_post_id} and like={0}
    ''')
    like_count = [res[0] for res in like_count][0]
    dislike_count = [res[0] for res in dislike_count][0]

    if (like_count == None):
        like_count = 0
    if (dislike_count == None):
        like_count = 0

    if (like_stat):
        like_val = like_stat.like
    else:
        like_val = None

    return render_template('blog_posts.html',
                           title=post.title,
                           date=post.date,
                           post=post,
                           category=post.category,
                           notifs=notifs,
                           like_val=like_val,
                           like_count=like_count,
                           dislike_count=dislike_count,
                           previous_comments=previous_comments,
                           form=form,
                           User=User)