Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
def get_likes():
    post_id = request.args.get("post_id", None)
    if post_id:
        query = Likes.select(Likes.person).\
            where(Likes.post == post_id, Likes.is_deleted == False).\
            tuples()
        res = []
        append = res.append
        for item in query:
            append({
                "vkid": item[0],
            })
        return json.dumps(res)
    else:
        return json.dumps({"message": "This post does not exist"})
Esempio n. 4
0
def get_likes():
    post_id = request.args.get("post_id", None)
    if post_id:
        query = Likes.select(Likes.person).\
            where(Likes.post == post_id, Likes.is_deleted == False).\
            tuples()
        res = []
        append = res.append
        for item in query:
            append({
                "vkid": item[0],
            })
        return json.dumps(res)
    else:
        return json.dumps({"message": "This post does not exist"})
Esempio n. 5
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
Esempio n. 6
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