Beispiel #1
0
 def get(self, post_id):
     username = self.isLoggedIn()
     key = db.Key.from_path('Post', int(post_id), parent=blog_key())
     post = db.get(key)
     if username and post:
         if self.user and self.user.key().id() == post.user_id:
             self.render("error.html",
                         error="You cannot like your own post!")
         else:
             l = Like.all().filter('user_id =',
                                   self.user.key().id()).filter(
                                       'post_id =',
                                       post.key().id()).get()
             if l:
                 self.redirect('/blog/' + str(post.key().id()))
             else:
                 like = Like(parent=key,
                             user_id=self.user.key().id(),
                             post_id=post.key().id())
                 post.likes += 1
                 like.put()
                 post.put()
                 self.redirect('/blog/' + str(post.key().id()))
     else:
         self.render("error.html",
                     error="You must be logged in to 'like' a post.")
Beispiel #2
0
    def post(self, post_id):
        key = db.Key.from_path('Post', int(post_id), parent=blog_key())
        post = db.get(key)

        if not post:
            self.error(404)
            return
        c = ""
        if (self.user):
            # post-like value increases by clicking like.
            if (self.request.get('like')
                    and self.request.get('like') == "update"):
                likes = db.GqlQuery("select * from Like where post_id = " +
                                    post_id + " and user_id = " +
                                    str(self.user.key().id()))

                if self.user.key().id() == post.user_id:
                    self.redirect("/blog/" + post_id +
                                  "?error=You cannot like your  " +
                                  "own post!")
                    return
                elif likes.count() == 0:
                    like = Like(parent=blog_key(),
                                user_id=self.user.key().id(),
                                post_id=int(post_id))
                    like.put()
                    self.redirect("/blog/" + post_id +
                                  "?error=You can like your  " +
                                  "post once.!!!!")
                    return

            comment = self.request.get('comment')
            if comment:
                c = Comment(parent=blog_key(),
                            user_id=self.user.key().id(),
                            post_id=int(post_id),
                            comment=comment)
                c.put()
            else:
                self.redirect("/blog/" + post_id +
                              "?error=You need to type your  " +
                              "comment.!!!!")
                return
        else:
            self.redirect("/login?error=First login and then " +
                          "try to edit, comment or like.!!")
            return

        comments = db.GqlQuery("select * from Comment where post_id = " +
                               post_id + "order by created desc")

        likes = db.GqlQuery("select * from Like where post_id=" + post_id)

        self.render("permalink.html",
                    post=post,
                    comments=comments,
                    likeCount=likes.count(),
                    new=c)
Beispiel #3
0
    def post(self, post_id):

        key = db.Key.from_path('Post', int(post_id), parent=blog_key())
        post = db.get(key)

        if not post:
            self.error(404)
            return
        """
            On posting comment, new comment tuple is created and stored,
            with relationship data of user and post.
        """
        c = ""
        if (self.user):
            # On clicking like, post-like value increases.
            if (self.request.get('like')
                    and self.request.get('like') == "update"):
                likes = db.GqlQuery("select * from Like where post_id = " +
                                    post_id + " and user_id = " +
                                    str(self.user.key().id()))

                if self.user.key().id() == post.user_id:
                    self.redirect("/blog/" + post_id +
                                  "?error=You cannot like your " + "post.!!")
                    return
                elif likes.count() == 0:
                    l = Like(parent=blog_key(),
                             user_id=self.user.key().id(),
                             post_id=int(post_id))
                    l.put()

            # On commenting, it creates new comment tuple
            if (self.request.get('comment')):
                c = Comment(parent=blog_key(),
                            user_id=self.user.key().id(),
                            post_id=int(post_id),
                            comment=self.request.get('comment'))
                c.put()
        else:
            self.redirect("/login?error=You need to login before " +
                          "performing edit, like or commenting.!!")
            return

        comments = db.GqlQuery("select * from Comment where post_id = " +
                               post_id + "order by created desc")

        likes = db.GqlQuery("select * from Like where post_id=" + post_id)

        self.render("permalink.html",
                    post=post,
                    comments=comments,
                    noOfLikes=likes.count(),
                    new=c)
    def post(self, post_id):
        key = db.Key.from_path('Post', int(post_id), parent=blog_key())
        post = db.get(key)

        if not post:
            self.error(404)
            return

        c = ''
        if self.user:

            # On clicking like, post-like value increases.

            if self.request.get('like') and self.request.get('like') \
                == 'update':
                likes = \
                    db.GqlQuery('select * from Like where post_id = '
                                + post_id + ' and user_id = '
                                + str(self.user.key().id()))

                if self.user.key().id() == post.user_id:
                    self.redirect('/blog/' + post_id
                                  + '?error=You cannot like your '
                                  + 'post.!!')
                    return
                elif likes.count() == 0:
                    l = Like(parent=blog_key(),
                             user_id=self.user.key().id(),
                             post_id=int(post_id))
                    l.put()

            # On commenting, it creates new comment tuple

            if self.request.get('comment'):
                c = Comment(parent=blog_key(),
                            user_id=self.user.key().id(),
                            post_id=int(post_id),
                            comment=self.request.get('comment'))
                c.put()
        else:
            self.redirect('/login?error=You need to login before '
                          + 'performing edit, like or commenting.!!')
            return

        comments = db.GqlQuery('select * from Comment where post_id = '
                               + post_id + 'order by created desc')

        likes = db.GqlQuery('select * from Like where post_id='
                            + post_id)

        self.render('full-post.html', post=post, comments=comments,
                    noOfLikes=likes.count(), new=c)
Beispiel #5
0
    def post(self, post_id):
        k = db.Key.from_path('Post', int(post_id), parent=blog_key())
        post = db.get(k)

        if not post:
            self.error(404)
            return

        c = ""
        if (self.user):
            # Add a like to a post
            if (self.request.get('like')
                    and self.request.get('like') == "update"):
                likes = db.GqlQuery("select * from Like where post_id = " +
                                    post_id + " and user_id = " +
                                    str(self.user.key().id()))
                if self.user.key().id() == post.user_id:
                    self.redirect("/blog/" + post_id +
                                  "?error=You can't like your " + "own post!")
                    return
                # Else add the like
                elif likes.count() == 0:
                    l = Like(parent=blog_key(),
                             user_id=self.user.key().id(),
                             post_id=int(post_id))
                    l.put()

            # Adds comment and creates new tuple
            if (self.request.get('comment')):
                c = Comment(parent=blog_key(),
                            user_id=self.user.key().id(),
                            post_id=int(post_id),
                            comment=self.request.get('comment'))
                c.put()
        else:
            self.redirect("/login?error=You can't edit, like, or comment" +
                          " a post without signing in!")
            return

        comments = db.GqlQuery("select * from Comment where post_id = " +
                               post_id + "order by created desc")

        likes = db.GqlQuery("select * from Like where post_id=" + post_id)

        self.render("permalink.html",
                    post=post,
                    comments=comments,
                    likeCount=likes.count(),
                    new=c)
    def post(self, post_id):
        # if no signed in user, redirect to the login page
        if not self.user:
            self.redirect('/login')

        post = Post.get_by_id(int(post_id))
        if post:
            query = Like.query(Like.post == post.key)
            like = query.get()
        else:
            self.render_404(error_message="Post {} not found.".format(post_id))
        if like:
            post_id = post.key.id()
            like.key.delete()
            # redirect to the post page
            self.redirect_after_delay('/{}'.format(post_id))
        else:
            # create a dictionary to hold any error messages
            params = dict(post=post,
                          comments=post.get_comments(),
                          owner=post.user.get())

            # user has not liked this post, so show an error.
            params['error'] = "Cannot unlike a post which has not been liked."
            self.render("permalink.html", **params)
Beispiel #7
0
 def has_user_liked(self, user_name):
     """ Returns if the current user has liked this post """
     prev_user_likes = Like.all().filter('user_name = ', user_name).filter(
         'post_id = ', str(self.key().id())).count()
     if (prev_user_likes > 0):
         return True
     return False
Beispiel #8
0
    def render(self):
        # replace new line characters with breaks
        self.__render_text = self.content.replace('\n', '<br>')

        comments_query = Comment.query(Comment.post == self.key)\
            .order(-Comment.created)

        num_comments = comments_query.count()
        if num_comments == 1:
            text = "comment"
        else:
            text = "comments"
        comment_count = "{} {}".format(num_comments, text)

        likes_query = Like.query(Like.post == self.key)
        num_likes = likes_query.count()
        if num_likes == 1:
            text = "like"
        else:
            text = "likes"
        like_count = "{} {}".format(num_likes, text)

        user = self.user.get()

        return utils.render_str('post.html',
                                post=self,
                                author=user,
                                comment_count=comment_count,
                                like_count=like_count)
Beispiel #9
0
    def get(self, post_id):
        if self.read_secure_cookie('user_id'):
            post = Post.by_id(post_id)
            if post:
                if post.user == User.by_id(
                        self.read_secure_cookie('user_id')).username:
                    post.delete()
                    if Comment.by_post(post_id).get():
                        for comment in Comment.by_post(post_id):
                            comment.delete()
                    if Like.by_post(post_id).get():
                        for like in Like.by_post(post_id):
                            like.delete()
            self.redirect('/blog')

        else:
            self.redirect('/')
    def post(self, post_id):
        key = db.Key.from_path('Post', int(post_id), parent=blog_key())
        post = db.get(key)

        if not post:
            self.error(404)
            return

        c = ""
        if (self.user):
            likes = db.GqlQuery("select * from Like where post_id = " +
                                post_id + " and user_id = " +
                                str(self.user.key().id()))

            if (self.request.get('like')
                    and self.request.get('like') == "update"):

                if (self.user.key().id() == post.user_id):
                    self.redirect("/blog/" + post_id +
                                  "?error=You cannot Like your" + "post.!!")
                    return
                elif likes.count() == 0:
                    l = Like(parent=blog_key(),
                             user_id=self.user.key().id(),
                             post_id=int(post_id))
                    l.put()

            if (self.request.get('comment')):
                c = Comment(parent=blog_key(),
                            user_id=self.user.key().id(),
                            post_id=int(post_id),
                            comment=self.request.get('comment'))
                c.put()
        else:
            self.redirect("/login?error= Login before " +
                          "edit or comment or like.!!")
            return

        comments = db.GqlQuery("select * from Comment where post_id = " +
                               post_id + "order by created desc")
        self.render("postDetails.html",
                    post=post,
                    noOfLikes=likes.count(),
                    comments=comments,
                    new=c)
Beispiel #11
0
    def is_liked_by(self, user):
        # get all likes for this post
        query = Like.query(Like.post == self.key)
        results = query.get()

        q = query.filter(Like.user == user.key)

        result = q.fetch()
        # print "result = {}".format(result)
        return result
Beispiel #12
0
def getAllLikes():
    connection=dbapi2.connect(current_app.config['dsn'])
    cursor=connection.cursor()
    cursor.execute("""SELECT U1.USERNAME as LIKERNAME, U2.USERNAME as TWITTERNAME, T2.TITLE, T2.CONTEXT, T2.LIKETIME FROM (SELECT L.USERID AS LIKERID,
    T.USERID AS TWITOWNERID,T.TITLE,T.CONTEXT,L.LIKETIME FROM LIKES AS L JOIN TWEETS as T ON L.TWEETID=T.TWEETID)
    AS T2 JOIN USERS AS U1 ON T2.LIKERID=U1.ID JOIN USERS AS U2 ON T2.TWITOWNERID=U2.ID""")
    likes=[(Like(likername,tweetownername,tweettitle,tweetcontext,liketime)) for likername,tweetownername,tweettitle,tweetcontext,liketime in cursor ]
    connection.commit()
    cursor.close()
    connection.close()
    return likes
Beispiel #13
0
 def get(self, post_id=None):
     if self.read_secure_cookie('user_id'):
         if post_id and Post.by_id(int(post_id)):
             post = Post.by_id(int(post_id))
             comments = Comment.by_post(post_id)
             likes = Like.by_post(post_id)
             likes_num = 0
             dislikes_num = 0
             if Like.by_post(post_id).count():
                 for like in Like.by_post(post_id):
                     if like.value > 0:
                         likes_num +=1
                     else:
                         dislikes_num +=1
             user = User.by_id(self.read_secure_cookie('user_id'))
             self.render('post.html', post = post, comments = comments, likes = likes_num, dislikes = dislikes_num, user = user, post_id=post_id)
         else:
             self.redirect('/')
     else:
         self.redirect('/')
Beispiel #14
0
 def liked_users(self):
     """ Returns list of users for the popup """
     likes = Like.all().filter('post_id = ', str(self.key().id()))
     liked_users = ""
     first_user = True
     for like in likes:
         comma = ", "
         if first_user:
             comma = ""
         liked_users = liked_users + comma + like.user_name
         first_user = False
     return liked_users
Beispiel #15
0
def test_like(persons):
    print(persons.where(Like("City", "s.*")))
    persons.where(Like("City", ".*s")).print()
    assert (
        persons.where(Like("City", ".*tav.*"))
        == """\
P_ID LASTNAME  FIRSTNAME ADDRESS   CITY     
---- --------- --------- --------- ---------
   3 Pettersen Kari      Storgt 20 Stavanger\
"""
    )
    persons.where(NotLike("City", ".*tav.*")).print()
    # Test wildcard patterns.
    persons.where(Like("City", "sa.*")).print()
    persons.where(Like("City", ".*nes.*")).print()
    persons.where(Like("FirstName", ".la")).print()
    persons.where(Like("LastName", "S.end.on")).print()
    persons.where(Like("LastName", "[bsp].*")).print()
    persons.where(Like("LastName", "[^bsp].*")).print()
Beispiel #16
0
    def get(self, value=None):

        if self.read_secure_cookie('user_id'):
            if value:
                tag = value.split(',')
                if (tag[1]):
                    post = tag[0]
                    value = tag[1]
                    user = self.read_secure_cookie('user_id')
                    if int(post) and Post.by_id(post):
                        if Like.by_post_user(post, user).count() < 1:
                            like = Like(post=post, value=int(value), user=user)
                            like.put()
                        else:
                            like = Like.by_post_user(post, user).get()
                            like.value = int(value)
                            like.put()
                else:
                    self.redirect('/')
            else:
                self.redirect('/')
            self.redirect('/post/%s' % post)
        else:
            self.redirect('/')
    def post(self, post_id):
        # if no signed in user, redirect to the login page
        if not self.user:
            self.redirect('/login')
            return

        # get the post from the datastore
        post = Post.get_by_id(int(post_id))
        if post:

            # get the comments
            comments = post.get_comments()
            owner = post.user.get()

            # create a dictionary to hold any error messages
            params = dict(post=post, comments=comments, owner=owner)

            # Do not allow the author to like their own post.
            owner = post.user.get()
            owner_id = owner.key.id()
            user_id = self.user.key.id()

            if owner_id is user_id:
                params['error'] = "You are not allowed to like your own post."
                self.render("permalink.html", **params)
                return

            if post.is_liked_by(self.user):
                # Do not allow a user to like a post more than once.
                params['like_error'] = "You have already liked this post."

                self.render("permalink.html", **params)
                return

            # create new like instance.
            like = Like.new_like(user=self.user.key, post=post.key)

            # reload the page
            self.redirect_after_delay('/{}'.format(post.key.id()))
        else:
            self.render_404(error_message="Post {} not found.".format(post_id))
Beispiel #18
0
 def render(self):
     self._render_text = self.content.replace('\n', '<br>')
     postID = self.key().id()
     liked = Like.all().filter('post =', str(postID)).count()
     return render_str("post.html", p=self, id=postID, l=liked)
Beispiel #19
0
 def get_likes(self):
     """ queries the datastore for likes on this post """
     likes_query = Like.query(Like.post == self.key)
     likes = likes_query.fetch()
     return likes
Beispiel #20
0
 def likes(self):
     """ Returns number of likes that the current post has """
     return Like.all().filter('post_id = ', str(self.key().id())).count()
Beispiel #21
0
 def get_likes(self):
     """Get post likes."""
     like = Like.all().filter('post_id =', self.key().id()).get()
     return like
Beispiel #22
0
    def setUp(self):
        self.client = Redis(decode_responses=True)
        self.client.flushdb()

        self.like = Like(self.client, "topic_12345::like")
Beispiel #23
0
class TestLike(unittest.TestCase):
    def setUp(self):
        self.client = Redis(decode_responses=True)
        self.client.flushdb()

        self.like = Like(self.client, "topic_12345::like")

    def test_cast(self):
        self.assertTrue(self.like.cast("peter"))
        self.assertFalse(self.like.cast("peter"))

    def test_is_liked(self):
        self.assertFalse(self.like.is_liked("peter"))
        self.like.cast("peter")
        self.assertTrue(self.like.is_liked("peter"))

    def test_undo(self):
        self.like.cast("peter")
        self.like.undo("peter")
        self.assertFalse(self.like.is_liked("peter"))

    def test_get_all_liked_users(self):
        self.assertEqual(self.like.get_all_liked_users(), set())

        self.like.cast("peter")
        self.like.cast("jack")
        self.like.cast("mary")
        self.assertEqual(self.like.get_all_liked_users(),
                         {"peter", "jack", "mary"})

    def test_count(self):
        self.assertEqual(self.like.count(), 0)
        self.like.cast("peter")
        self.assertEqual(self.like.count(), 1)