def seed_postLikes(): postLike1 = PostLike(userId=1, postId=2) postLike2 = PostLike(userId=2, postId=3) postLike3 = PostLike(userId=2, postId=4) postLike4 = PostLike(userId=1, postId=5) postLike5 = PostLike(userId=5, postId=6) postLike6 = PostLike(userId=3, postId=2) postLike7 = PostLike(userId=4, postId=3) postLike8 = PostLike(userId=1, postId=4) postLike9 = PostLike(userId=2, postId=5) postLike10 = PostLike(userId=3, postId=6) db.session.add(postLike1) db.session.add(postLike2) db.session.add(postLike3) db.session.add(postLike4) db.session.add(postLike5) db.session.add(postLike6) db.session.add(postLike7) db.session.add(postLike8) db.session.add(postLike9) db.session.add(postLike10) db.session.commit()
def createlike(current_user, id): like = PostLike( post_liker=current_user, post_id=id, ) db.session.add(like) db.session.commit() likesList = list(PostLike.query.filter(PostLike.post_id == id).all()) returnList = [like.user_id for like in likesList] return {"data": returnList}
def handleLike(request): user_id = request.data.get("userId") post_id = request.data.get("postId") data = '' row = PostLike.objects.filter(post_id=post_id, user_id=user_id) if row.exists(): row.delete() Post.objects.filter(id=request.data.get("postId")).update(likes=F('likes') - 1) data = False else: p = PostLike(post_id=request.data.get("postId"), user_id=request.data.get("userId")) p.save() Post.objects.filter(id=request.data.get("postId")).update(likes=F('likes') + 1) data = True # trending algorithm ======================================================= item = Post.objects.get(id=post_id) post_uploaded_timestamp = float(item.date) now_timestamp = float(datetime.datetime.now().timestamp()) one_day_timestamp = 86400 time_elapsed = now_timestamp - post_uploaded_timestamp if item.isBusiness and time_elapsed <= one_day_timestamp: like_count = item.likes comment_count = item.comments view_count = item.viewCount point = (view_count + (like_count * 1.25) + (comment_count * 1.50)) / time_elapsed # trending_count = Trending.objects.all().count() # if trending_count > 50: # Trending.objects.all().aggregate(Max('point')).delete() row = Trending.objects.filter(post_id=item.id) if row.exists(): row.update(point=point) else: trending = Trending(point=point, post_id=item.id) trending.save() # end of trending algorithm return Response({"data": data}, status=HTTP_200_OK)
def seed_postLikes(): likes = [] for i in range(80): users = randomUserSet() for user in users: likes.append(PostLike(userId=user, postId=(i+1))) for like in likes: db.session.add(like) db.session.commit()
def like_post(): liked_post_id = request.json["post_id"] existing_like = PostLike.query.filter( PostLike.post_id == liked_post_id, PostLike.user_id == current_user.id).first() if existing_like: return {"message": "like exists"}, 500 like = PostLike(user_id=current_user.id, post_id=liked_post_id) db.session.add(like) db.session.commit() return {"post_id": like.post_id}
def like_a_post(): liked_post = request.json['post_id'] old_like = PostLike.query.filter( PostLike.post_id == liked_post.id, PostLike.user_id == current_user.id).first() if old_like: return new_like = PostLike.post(user_id=current_user.id, post_id=liked_post.id) db.session.add(new_like) db.session.commit() return
def postLike(): likes = PostLike.query.all() newLike = request.get_json() userId = newLike['userId'] postId = newLike['postId'] for like in likes: if int(like.userId) == int(userId) and int(like.postId) == int(postId): db.session.delete(like) db.session.commit() return ({"userId": like.userId, "postId": like.postId}) new_postLike = PostLike(userId=userId, postId=postId) db.session.add(new_postLike) db.session.commit() return (newLike)
def likePost(id): post = Post.query.filter(Post.id == id).one() newLike = PostLike(userId=current_user.id, postId=id) db.session.add(newLike) db.session.commit() return {"success": True}
def seed_post_likes(): for num in range(300): post_like = PostLike(user_id=random.randrange(1, 6), post_id=random.randrange(1, 14)) db.session.add(post_like) db.session.commit()
poster=user3, ) post8 = Post( image= 'https://res.cloudinary.com/dgzcv1mcs/image/upload/v1591832876/Instantelegram/venice-3118803_960_720_eslila.jpg', caption='Venice Palace', poster=user3, ) post9 = Post( image= 'https://res.cloudinary.com/dgzcv1mcs/image/upload/v1591832876/Instantelegram/sea-5237374_960_720_vikixv.jpg', caption='Beautiful Ocean', poster=user3, ) like1 = PostLike( post_liked=post6, post_liker=user1, ) like2 = PostLike( post_liked=post7, post_liker=user1, ) like3 = PostLike( post_liked=post8, post_liker=user1, ) like4 = PostLike( post_liked=post9, post_liker=user1, ) comment1 = Comment(post_cmnt=post7, commenter=user2,
def save_unlike(post_id): post = Post.query.get(post_id) post_unlike = PostLike(like=False, liked_post=post, liked_by=current_user) db.session.add(post_unlike) db.session.commit() return redirect(url_for('index'))
{ "title": "Post 9", "body": 'Content of Post 9', "publication_day": now, "author": kate }, { "title": "Post 10", "body": 'Content of Post 10', "publication_day": now, "author": kate }, ] # putting posts and likes into db for post in kate_posts: # new post new_post = Post(title=post["title"], body=post["body"], publication_day=post["publication_day"], author=post["author"]) db.session.add(new_post) db.session.commit() # new like is_like = random.choices([True, False], weights=(80, 20)) new_post_like = PostLike(like=is_like[0], liked_post=new_post, liked_by=mike, date=post["publication_day"]) db.session.add(new_post_like) db.session.commit()