def add_review(self, content, author): from app.models.content import Post review = Post(parent=self, content=content, author=author) review.save() self.reviews.append(review) self.save() return review
def test_post_deletion(self): """ Test post deletion """ from app.models.user import User u = User(email="*****@*****.**", password="******") db.session.add(u) db.session.commit() post_public_id = 4206921 p = Post(author_id=u.id, public_id=post_public_id) db.session.add(p) db.session.commit() access_token = create_access_token(u.id) delete_resp = delete_post(self, access_token, post_public_id) self.assertTrue(delete_resp.status) self.assertEqual(delete_resp.status_code, 200) # Check if post is gone ;( query_post = Post.query.filter_by(public_id=p.public_id).first() self.assertIsNone(query_post)
def test_schema(self): """ Check Post Schema output """ caption = "lorem" p = Post(caption=caption) p_dump = PostSchema().dump(p) self.assertEquals(p_dump["caption"], caption)
def test_feed_get_posts(self): """ Test getting posts from feed. """ from app.models.content import Post access_token = create_access_token(69) # Create 6 random posts posts = [] for i in range(5): post = Post(caption=fake.text(), on_project=i) posts.append(post) db.session.bulk_save_objects(posts) db.session.commit() feed_resp = get_feed_posts(self, access_token, 1) feed_resp_data = json.loads(feed_resp.data.decode()) self.assertTrue(feed_resp.status) self.assertEqual(feed_resp.status_code, 200) self.assertEqual(len(feed_resp_data["posts"]), 3) # Page 2 feed_resp_2 = get_feed_posts(self, access_token, 2) feed_resp_2_data = json.loads(feed_resp_2.data.decode()) self.assertTrue(feed_resp_2.status) self.assertEqual(feed_resp_2.status_code, 200) self.assertEqual(len(feed_resp_2_data["posts"]), 2)
def get_query(self): content_id = request.args.get('content_id', None) content_type = request.args.get('content_type', None) if content_type == 'Article': cls = Article elif content_type == 'Discussion': cls = Discussion else: cls = Post if content_id is not None: parent = cls.objects(pk=content_id).first() return Post.objects(parent=parent) else: return Post.objects
def test_post_get(self): """ Test getting a post from DB """ data = dict(caption="Brogramming", public_id=123) post = Post(caption=data["caption"], public_id=data["public_id"]) db.session.add(post) db.session.commit() post_resp = get_post_data(self, data["public_id"]) post_data = json.loads(post_resp.data.decode()) self.assertTrue(post_resp.status) self.assertEqual(post_resp.status_code, 200) self.assertEqual(post_data["post"]["caption"], data["caption"]) # Test a 404 request post_404_resp = get_post_data(self, 69) self.assertEqual(post_404_resp.status_code, 404)
def test_post_update(self): """ Test post updating """ from app.models.user import User u = User(email="*****@*****.**", password="******") db.session.add(u) db.session.commit() orig_data = dict( caption="arch", image_hash="linux.png", public_id=4206921, ) p = Post( author_id=u.id, caption=orig_data["caption"], public_id=orig_data["public_id"], ) db.session.add(p) db.session.commit() access_token = create_access_token(u.id) # Update updated_data = dict(caption="gentoo") update_resp = update_post(self, access_token, orig_data["public_id"], updated_data) # Compare changes updated_post = Post.query.filter_by( public_id=orig_data["public_id"]).first() self.assertTrue(update_resp) self.assertEqual(update_resp.status_code, 200) self.assertNotEqual(updated_post.caption, orig_data["caption"])
def _comments_formatter(view, context, model, name): return Markup("<a href='%s'>%d</a>" % (url_for('content_post_admin_view.index_view', content_id=str(model.id), content_type=model.__class__.__name__), Post.objects(parent=model).count())) if Post.objects(parent=model).count() > 0 else ""
def reviews_count(self): from app.models.content import Post return Post.objects(parent=self).count()
def reviews(self): from app.models.content import Post reviews = Post.objects(parent=self).all() return reviews
def remove_review(self, id): from app.models.content import Post review = Post.objects(pk=id).first() self.reviews.remove(review) self.save() review.delete()
# Check if the project exists if not (project := Project.query.filter_by(public_id=project_public_id).first()): return err_resp("Can't create post without project.", "project_404", 404) # Create a new post try: from uuid import uuid4 from .utils import create_and_load post = Post( public_id=str(uuid4().int)[:15], author_id=current_user.id, caption=caption, image_hash=image_hash, on_project=project.id, ) post_data = create_and_load(post) resp = message(True, "Post created.") resp["post"] = post_data return resp, 201 except Exception as error: current_app.logger.error(error) return internal_err_resp() @staticmethod
def test_public_ids_are_random(self): """ Check if post public IDs are random """ p = Post(public_id=str(uuid4().int)[:15]) p2 = Post(public_id=str(uuid4().int)[:15]) self.assertNotEquals(p, p2)
def post(self): return Post.get(self.post_id)