Python app.models.Comment() Examples

The following are 13 code examples of app.models.Comment(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module app.models , or try the search function .
Example #1
Source File: test_user_model.py    From flask-pycon2014 with MIT License 6 votes vote down vote up
def test_moderation(self):
        db.create_all()
        u1 = User(email='john@example.com', username='john', password='cat')
        u2 = User(email='susan@example.com', username='susan', password='cat',
                  is_admin=True)
        t = Talk(title='t', description='d', author=u1)
        c1 = Comment(talk=t, body='c1', author_name='n',
                     author_email='e@e.com', approved=True)
        c2 = Comment(talk=t, body='c2', author_name='n',
                     author_email='e@e.com', approved=False)
        db.session.add_all([u1, u2, t, c1, c2])
        db.session.commit()
        for_mod1 = u1.for_moderation().all()
        for_mod1_admin = u1.for_moderation(True).all()
        for_mod2 = u2.for_moderation().all()
        for_mod2_admin = u2.for_moderation(True).all()
        self.assertTrue(len(for_mod1) == 1)
        self.assertTrue(for_mod1[0] == c2)
        self.assertTrue(for_mod1_admin == for_mod1)
        self.assertTrue(len(for_mod2) == 0)
        self.assertTrue(len(for_mod2_admin) == 1)
        self.assertTrue(for_mod2_admin[0] == c2) 
Example #2
Source File: manage.py    From circleci-demo-python-flask with MIT License 5 votes vote down vote up
def make_shell_context():
    return dict(app=app, db=db, User=User, Follow=Follow, Role=Role,
                Permission=Permission, Post=Post, Comment=Comment) 
Example #3
Source File: test_api.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_delete(self):
        u1 = User(email='john@example.com', username='john', password='cat')
        u2 = User(email='susan@example.com', username='susan', password='cat')
        t = Talk(title='t', description='d', author=u1)
        c = Comment(talk=t, body='c1', author_name='n',
                    author_email='e@e.com', approved=False)
        db.session.add_all([u1, u2, t, c])
        db.session.commit()

        # wrong user --> 403
        token = u2.get_api_token()
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='DELETE',
                data=json.dumps({'token': token}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 403)

        token = u1.get_api_token()
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='DELETE',
                data=json.dumps({'token': token}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 200)
            c = Comment.query.get(c.id)
            self.assertIsNone(c) 
Example #4
Source File: test_talk_model.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_approved(self):
        db.create_all()
        u = User(email='john@example.com', username='john', password='cat')
        t = Talk(title='t', description='d', author=u)
        c1 = Comment(talk=t, body='c1', author_name='n',
                     author_email='e@e.com', approved=True)
        c2 = Comment(talk=t, body='c2', author_name='n',
                     author_email='e@e.com', approved=False)
        db.session.add_all([u, t, c1, c2])
        db.session.commit()
        approved = t.approved_comments().all()
        self.assertTrue(len(approved) == 1)
        self.assertTrue(approved[0] == c1) 
Example #5
Source File: test_comment_model.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_markdown(self):
        c = Comment()
        c.body = '# title\n\n## section\n\ntext **bold** and *italic*'
        self.assertTrue(c.body_html == '<h1>title</h1>\n<h2>section</h2>\n'
                        '<p>text <strong>bold</strong> '
                        'and <em>italic</em></p>') 
Example #6
Source File: test_comment_model.py    From flask-pycon2014 with MIT License 5 votes vote down vote up
def test_notification_list(self):
        db.create_all()
        u1 = User(email='john@example.com', username='john', password='cat')
        u2 = User(email='susan@example.com', username='susan', password='cat')
        t = Talk(title='t', description='d', author=u1)
        c1 = Comment(talk=t, body='c1', author_name='n1',
                     author_email='e@e.com', approved=True)
        c2 = Comment(talk=t, body='c2', author_name='n2',
                     author_email='e2@e2.com', approved=True, notify=False)
        c3 = Comment(talk=t, body='c3', author=u2, approved=True)
        c4 = Comment(talk=t, body='c4', author_name='n4',
                     author_email='e4@e4.com', approved=False)
        c5 = Comment(talk=t, body='c5', author=u2, approved=True)
        c6 = Comment(talk=t, body='c6', author_name='n6',
                     author_email='e6@e6.com', approved=True, notify=False)
        db.session.add_all([u1, u2, t, c1, c2, c3, c4, c5])
        db.session.commit()
        email_list = c4.notification_list()
        self.assertTrue(('e@e.com', 'n1') in email_list)
        self.assertFalse(('e2@e2.com', 'n2') in email_list)  # notify=False
        self.assertTrue(('susan@example.com', 'susan') in email_list)
        self.assertFalse(('e4@e4.com', 'n4') in email_list)  # comment author
        self.assertFalse(('e6@e6.com', 'n6') in email_list)
        email_list = c5.notification_list()
        self.assertFalse(('john@example.com', 'john') in email_list)
        self.assertTrue(('e4@e4.com', 'n4') in email_list)  # comment author 
Example #7
Source File: manage.py    From flasky-first-edition with MIT License 5 votes vote down vote up
def make_shell_context():
    return dict(app=app, db=db, User=User, Follow=Follow, Role=Role,
                Permission=Permission, Post=Post, Comment=Comment) 
Example #8
Source File: routes.py    From tutorial-flask with Apache License 2.0 5 votes vote down vote up
def show_post(slug):
    logger.info('Mostrando un post')
    logger.debug(f'Slug: {slug}')
    post = Post.get_by_slug(slug)
    if not post:
        logger.info(f'El post {slug} no existe')
        abort(404)
    form = CommentForm()
    if current_user.is_authenticated and form.validate_on_submit():
        content = form.content.data
        comment = Comment(content=content, user_id=current_user.id,
                          user_name=current_user.name, post_id=post.id)
        comment.save()
        return redirect(url_for('public.show_post', slug=post.title_slug))
    return render_template("public/post_view.html", post=post, form=form) 
Example #9
Source File: test_controllers.py    From CivilServant with MIT License 5 votes vote down vote up
def clear_all_tables():
    db_session.query(EventHook).delete()
    db_session.query(FrontPage).delete()
    db_session.query(SubredditPage).delete()
    db_session.query(Subreddit).delete()
    db_session.query(Post).delete()
    db_session.query(User).delete()  
    db_session.query(ModAction).delete()    
    db_session.query(Comment).delete()      
    db_session.commit() 
Example #10
Source File: manage.py    From flasky-with-celery with MIT License 5 votes vote down vote up
def make_shell_context():
    return dict(app=app, db=db, User=User, Follow=Follow, Role=Role,
                Permission=Permission, Post=Post, Comment=Comment) 
Example #11
Source File: manage.py    From flask-blog with MIT License 5 votes vote down vote up
def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role, Article=Article, Comment=Comment, Source=Source,
                Category=Category, Tag=Tag) 
Example #12
Source File: test_api.py    From flask-pycon2014 with MIT License 4 votes vote down vote up
def test_token_errors(self):
        u1 = User(email='john@example.com', username='john', password='cat')
        u2 = User(email='susan@example.com', username='susan', password='cat')
        t = Talk(title='t', description='d', author=u1)
        c = Comment(talk=t, body='c1', author_name='n',
                    author_email='e@e.com', approved=False)
        db.session.add_all([u1, u2, t, c])
        db.session.commit()

        # missing JSON --> 400
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='PUT'):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 400)

        # missing token --> 401
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='PUT',
                data=json.dumps({'bad': 123}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 401)

        # bad token --> 401
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='PUT',
                data=json.dumps({'token': 'a bad token'}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 401)

        # malformed token --> 401
        u3 = User(email='david@example.com', username='david', password='cat')
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='PUT',
                data=json.dumps({'token': u3.get_api_token()}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 401) 
Example #13
Source File: test_api.py    From flask-pycon2014 with MIT License 4 votes vote down vote up
def test_approve(self):
        u1 = User(email='john@example.com', username='john', password='cat')
        u2 = User(email='susan@example.com', username='susan', password='cat')
        t = Talk(title='t', description='d', author=u1)
        c = Comment(talk=t, body='c1', author_name='n',
                    author_email='e@e.com', approved=False)
        db.session.add_all([u1, u2, t, c])
        db.session.commit()

        # wrong user --> 403
        token = u2.get_api_token()
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='PUT',
                data=json.dumps({'token': token}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 403)

        # correct user --> 200
        token = u1.get_api_token()
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='PUT',
                data=json.dumps({'token': token}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 200)
            c = Comment.query.get(c.id)
            self.assertTrue(c.approved)

        # approve an already approved comment --> 400
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='PUT',
                data=json.dumps({'token': token}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 400)

        # delete an already approved comment --> 400
        with self.app.test_request_context(
                '/api/1.0/comments/' + str(c.id),
                method='DELETE',
                data=json.dumps({'token': token}),
                headers={'Content-Type': 'application/json'}):
            res = self.app.full_dispatch_request()
            self.assertTrue(res.status_code == 400)