def like_post(person_id, post_id): """ Helper func to add a entry (or add back) in the Like table and update the Post entry :param person_id: int :param post_id: int :return: bool """ try: Person.get(Person.vkid == person_id) try: l = Likes.get(Likes.person == person_id, Likes.post == post_id) if l.is_deleted: l.is_deleted = False l.save() p = Post.get(Post.post_id == post_id) p.likes += 1 # some day it may cause race condition p.save() return True else: return False except DoesNotExist: Likes.create(person=person_id, post=post_id) p = Post.get(Post.post_id == post_id) p.likes += 1 # some day it may cause race condition p.save() return True except DoesNotExist: print "Like on person_id='{0}', post_id='{1}' doesn't exist".format(person_id, post_id) except Exception as e: print "like_post exception: {0}".format(repr(e)) return False
def like_post(person_id, post_id): """ Helper func to add a entry (or add back) in the Like table and update the Post entry :param person_id: int :param post_id: int :return: bool """ try: Person.get(Person.vkid == person_id) try: l = Likes.get(Likes.person == person_id, Likes.post == post_id) if l.is_deleted: l.is_deleted = False l.save() p = Post.get(Post.post_id == post_id) p.likes += 1 # some day it may cause race condition p.save() return True else: return False except DoesNotExist: Likes.create(person=person_id, post=post_id) p = Post.get(Post.post_id == post_id) p.likes += 1 # some day it may cause race condition p.save() return True except DoesNotExist: print "Like on person_id='{0}', post_id='{1}' doesn't exist".format( person_id, post_id) except Exception as e: print "like_post exception: {0}".format(repr(e)) return False
def create_new_comment(): vkid = request.form.get(VKID_NAME) post_id = request.form.get("post_id", None) text = request.form.get("text", None) if post_id and text: try: p = Post.get(Post.post_id == post_id) p.comments += 1 p.save() Comment.create(post=p, author=vkid, text=text) return json.dumps({"success": 1}) except DoesNotExist as e: print "Post with id='{0}' doesn't exist: {0}".format(e) return json.dumps({"success": 0})
def dislike_post(person_id, post_id): """ Helper func to remove Like from Post and update the Post entry :param person_id: int :param post_id: int :return: bool """ try: Person.get(Person.vkid == person_id) l = Likes.get(Likes.person == person_id, Likes.post == post_id) if not l.is_deleted: l.is_deleted = True l.save() p = Post.get(Post.post_id == post_id) p.likes -= 1 # some day it may cause race condition p.save() return True except DoesNotExist: print "Like on person_id='{0}', post_id='{1}' doesn't exist".format(person_id, post_id) except Exception as e: print "dislike_post exception: {0}".format(repr(e)) return False
def demo_add_comment(author_id, post_id): _, words_amount, text = generate_sentence() try: if author_id: Person.get(Person.vkid == author_id) else: all_pers = Person.select(Person.vkid) count = all_pers.count() - 1 author_id = all_pers[random.randint(0, count)] if post_id: p = Post.get(Post.post_id == post_id) else: all_posts = Post.select() count = all_posts.count() - 1 p = all_posts[random.randint(0, count)] p.comments += 1 p.save() Comment.create(post=p, author=author_id, text=text) return True except DoesNotExist: return False
def dislike_post(person_id, post_id): """ Helper func to remove Like from Post and update the Post entry :param person_id: int :param post_id: int :return: bool """ try: Person.get(Person.vkid == person_id) l = Likes.get(Likes.person == person_id, Likes.post == post_id) if not l.is_deleted: l.is_deleted = True l.save() p = Post.get(Post.post_id == post_id) p.likes -= 1 # some day it may cause race condition p.save() return True except DoesNotExist: print "Like on person_id='{0}', post_id='{1}' doesn't exist".format( person_id, post_id) except Exception as e: print "dislike_post exception: {0}".format(repr(e)) return False