def get(self):
        """
        Redirect to main route id user is authenticated (sending posts and 
        user login as parameters), if no redirects to login route
        :return: 
        """
        user_id = self.request.cookies.get('user_id')
        posts = Post.get_posts()

        for post in posts:
            post.owner = User.get_by_hash(user_id)
            post.is_author = \
                post.author == self.request.cookies.get('user_desc')
            post.liked = self.request.cookies.get('user_desc') in post.likes

            comments = Comment.get_by_post_id(post.key.id())

            for comment in comments:
                comment.is_author = \
                    comment.author == self.request.cookies.get('user_desc')

            post.comments = comments

        self.render('main.html',
                    posts=posts,
                    user=self.request.cookies.get('user_desc'))
 def is_post_owner(hash, post):
     """
     Checks if user passed is a post owner
     :param user_id: User login
     :param post: Post object
     :return: True or False
     """
     user = User.get_by_hash(hash)
     return post.author == user.user
    def post(self):
        """
        Creates a new comment in a post if user is authenticated, 
        if no redirects to login route
        :return: 
        """
        post_id = self.request.get('id')
        comment = self.request.get('comment')

        user = User.get_by_hash(self.request.cookies.get('user_id'))
        post = Post.get_by_id(post_id)

        comment = Comment(author=user.user,
                          post=post.key.id(),
                          comment=comment,
                          created=datetime.datetime.now(),
                          last_modified=datetime.datetime.now())
        comment.put()

        time.sleep(2)

        self.redirect('/main')
Esempio n. 4
0
    def post(self):
        """
        Creates or update a new post entry if user is 
        authenticated, if no redirects to login route
        :return: 
        """
        user_id = self.request.cookies.get('user_id')
        post_id = self.request.get('id')
        title = self.request.get('title')
        content = self.request.get('content')

        if 'update' in self.request.POST:
            post = Post.get_by_id(post_id)

            if Post.is_post_owner(user_id, post):
                post.title = title
                post.content = content
                post.last_modified = datetime.datetime.now()

                post.put()

                time.sleep(2)
        elif 'create' in self.request.POST:
            title = self.request.get('title')
            content = self.request.get('content')

            user = User.get_by_hash(user_id)

            p = Post(author=user.user,
                     title=title,
                     content=content,
                     created=datetime.datetime.now(),
                     last_modified=datetime.datetime.now())
            p.put()

            time.sleep(2)

        self.redirect('/main')
    def post(self):
        """
        Creates a post like or unlike if user is authenticated, 
        if no redirects to login route
        :return: 
        """
        user_id = self.request.cookies.get('user_id')
        post_id = self.request.get('id_post')

        user = User.get_by_hash(user_id)
        post = Post.get_by_id(post_id)

        if self.like_parameters_are_valid(user, post_id, post):
            if self.can_like(user, post):
                post.likes.append(user.user)
                post.put()
            elif self.can_unlike(user, post):
                post.likes.remove(user.user)
                post.put()

            time.sleep(2)

        self.redirect('/main')