Esempio n. 1
0
    def test_comment_delete_handler(self):
        # POST test topic via '/topic/add'
        csrf_token = str(uuid.uuid4())
        memcache.add(key=csrf_token, value=True, time=600)

        title = "Some new topic"
        content = "This is a new topic. Just for testing purposes."

        params = {"title": title, "content": content, "csrf_token": csrf_token}

        post = self.testapp.post('/topic/add', params)
        self.assertEqual(post.status_int, 302)

        # POST test comment via '/topic/details/<topic_id>'
        content = "This is a new comment. Just for testing purposes."

        params = {"content": content, "csrf_token": csrf_token}

        topic = Topic.query().get()
        # topic_id is extracted from request when creating comment via TopicDetails handler
        # Comment.save_comment(topic_id, content)
        post = self.testapp.post('/topic/details/' + str(topic.key.id()), params)
        self.assertEqual(post.status_int, 302)

        # Delete comment via '/comment/delete/<comment_id>'
        comment = Comment.query().get()
        params = {"csrf_token": csrf_token}

        post = self.testapp.post('/comment/delete/' + str(comment.key.id()), params, {'referer': '/user-comments'})
        self.assertEqual(post.status_int, 302)
        # check if comment.deleted field was set to True
        self.assertEqual(comment.deleted, True)

        post = self.testapp.post('/comment/delete/' + str(comment.key.id()), params, {'referer': '/topic/details/' + str(topic.key.id())})
        self.assertEqual(post.status_int, 302)
Esempio n. 2
0
    def get(self):
        current_time = datetime.now()
        time_deleted_limit = current_time - timedelta(minutes=2)
        comments = Comment.query(
            Comment.deleted == True,
            Comment.time_updated < time_deleted_limit).fetch()

        for comment in comments:
            comment.key.delete()
Esempio n. 3
0
    def get(self, topic_id):
        csrf_token = str(uuid.uuid4())
        memcache.add(key=csrf_token, value=True, time=600)

        topic = Topic.get_by_id(int(topic_id))
        comment = Comment.query(Comment.topic_id == topic.key.id()).order(
            Comment.created).fetch()

        params = {"topic": topic, "comment": comment, "csrf_token": csrf_token}

        return self.render_template("topic_details.html", params=params)
Esempio n. 4
0
    def get(self, post_id):
        post = Post.get_by_id(int(post_id))
        user = users.get_current_user()
        email = ''

        if user:
            email = user.email()
        comments = Comment.query(Comment.user_email == email,
                                 Comment.postID == post.key.id()).fetch()
        params = {'post': post, 'comments': comments}
        return self.render_template('delete_comment.html', params=params)
Esempio n. 5
0
    def get(self):
        user = users.get_current_user()

        user_comments = Comment.query(
            Comment.user_id == user.email(),
            Comment.deleted == False).order(-Comment.create_time).fetch()

        params = {
            "user_comments": user_comments
        }

        return self.render_template_with_csrf("user_comments.html", params)
Esempio n. 6
0
    def get(self, topic_id):
        topic = Topic.get_by_id(int(topic_id))
        comments = comments = Comment.query(Comment.topic_id == topic.key.id(),
                                            Comment.deleted == False).order(
                                                Comment.created).fetch()

        csrf_token = str(uuid.uuid4())  # convert UUID to string
        memcache.add(key=csrf_token, value=True, time=600)

        params = {
            "topic": topic,
            "comments": comments,
            "csrf_token": csrf_token
        }

        return self.render_template("topic_details.html", params=params)
Esempio n. 7
0
    def post(self, post_id):
        post = Post.get_by_id(int(post_id))

        user = users.get_current_user()
        email = ''

        if user:
            email = user.email()

        comments = Comment.query(Comment.user_email == email,
                                 Comment.postID == post.key.id()).fetch()

        for comment in comments:
            if comment.user_email == email:
                comment.deleted = True
                comment.put()

        return self.redirect_to('main-page')
Esempio n. 8
0
    def test_topic_details_handler(self):
        # Create test topic
        title = "Some new topic"
        content = "This is a new topic. Just for testing purposes."

        topic = Topic(user_id=os.environ['USER_EMAIL'],
                      title=title,
                      content=content)
        topic.put()

        # GET
        topic = Topic.query().get()
        get = self.testapp.get('/topic/details/' + str(topic.key.id()))
        self.assertEqual(get.status_int, 200)
        self.assertEqual(topic.title, title)

        # POST
        # 1. POST test comment via '/topic/details/<topic_id>'
        csrf_token = str(uuid.uuid4())
        memcache.add(key=csrf_token, value=True, time=600)
        content = "This is a new comment. Just for testing purposes."

        params = {"content": content, "csrf_token": csrf_token}

        # topic_id is extracted from request when creating comment via TopicDetails handler
        # Comment.save_comment(topic_id, content)
        post = self.testapp.post('/topic/details/' + str(topic.key.id()),
                                 params)
        self.assertEqual(post.status_int, 302)

        comment = Comment.query().get()
        self.assertEqual(comment.content, content)

        # 2. POST test subscription via '/topic/details/<topic_id>'
        params = {"csrf_token": csrf_token}

        # topic_id is extracted from request when creating comment via TopicDetails handler
        # Subscription.save_comment(topic_id, user_id)
        post = self.testapp.post('/topic/details/' + str(topic.key.id()),
                                 params)
        self.assertEqual(post.status_int, 302)

        subscription = Subscription.query().get()
        self.assertEqual(subscription.user_id, os.environ['USER_EMAIL'])
Esempio n. 9
0
    def get(self, post_id):
        post = Post.get_by_id(int(post_id))
        comments = Comment.query(Comment.postID == post.key.id(), Comment.deleted == False).order(-Comment.time_posted).fetch()
        admin = users.is_current_user_admin()
        author = post.user_email
        user = users.get_current_user()
        email = ''

        if user:
            email = user.email()

        params = {
            'post': post,
            'comments': comments,
            'admin': admin,
            'author': author,
            'email': email,
        }
        return self.render_template('post.html', params=params)
Esempio n. 10
0
    def get(self, topic_id):
        topic = Topic.get_by_id(int(topic_id))
        # get comments
        comments = (Comment.query(
            Comment.topic_id == topic_id,
            Comment.deleted == False).order(-Comment.create_time).fetch()
        )

        params = {
            "topic": topic,
            "comments": comments
        }

        user = users.get_current_user()
        if user:
            subscribed = Subscription.query(
                Subscription.user_id == user.email(),
                Subscription.topic_id == topic_id).fetch()
            if subscribed:
                params["subscribed"] = True

        return self.render_template_with_csrf("topic_details.html", params)
Esempio n. 11
0
    def get(self, topic_id):
        count_comments = Comment.query(
            Comment.topic_id == topic_id,
            Comment.deleted == False).count()

        return self.write(count_comments)