def generate_posts(n, users, tags): for i in range(n): post = Post() post.title = faker.sentence() post.text = faker.text(max_nb_chars=1000) post.publish_date = faker.date_this_century(before_today=True, after_today=False) post.user_id = users[random.randrange(0, len(users))].id post.tags = [tags[random.randrange(0, len(tags))] for i in range(0, 2)] try: db.session.add(post) db.session.commit() except Exception as e: log.error("Fail to add post %s: %s" % (str(post), e)) db.session.rollback()
def generate_posts(n, users, tags): print(tags) for i in range(n): post = Post() post.title = fake.sentence() post.text = fake.text(max_nb_chars=1000) post.publish_date = fake.date_this_century(before_today=True, after_today=False) post.emp_id = random.randrange(1, 100) post.tags = [tags[random.randrange(0, len(tags))] for i in range(0, 2)] try: db.session.add(post) db.session.commit() except Exception as e: print(f" The exception is {e}")
def callback(ch, method, properties, body): print('Received in main application') data = json.loads(body) print(data) if properties.content_type == 'post_created': post = Post(id=data['id'], title=data['title'], image=data['image']) db.session.add(post) db.session.commit() print('Post created in main app') elif properties.content_type == 'post_updated': post = Post.query.get(data['id']) post.title = data['title'] post.image = data['image'] db.session.commit() print('Post updated in main app') elif properties.content_type == 'post_deleted': post = Post.query.get(data) db.session.delete(post) db.session.commit() print('Post deleted in main app')