def seed_posts(num_of_posts): """ Seed the database with some posts. """ users = User.query.all() posts1 = [] posts2 = [] post_objs = [] print('Fetching posts...') for i in range(int(num_of_posts/10)): posts1.extend(requests.get( 'https://official-joke-api.appspot.com/jokes/ten').json()) for i in range(20): posts2.extend(requests.get( f'https://icanhazdadjoke.com/search?limit=30&page={i}', headers={'accept': 'application/json'}).json().get('results')) try: print('Saving posts to database...') for p in posts1: user = random.choice(users) post = Post() post.body = f"{p.get('setup')} - {p.get('punchline')}" post.user_id = user.id post.likes.extend(random.sample(users, k=random.randrange(36))) post.created_on = random_timestamp( datetime.datetime(2019, 1, 1), datetime.datetime(2019, 7, 30)) # post.tags.extend(item.) post_objs.append(post) for item in posts2: user = random.choice(users) post = Post() post.body = item.get('joke') post.user_id = user.id post.created_on = random_timestamp( datetime.datetime(2019, 7, 1), datetime.datetime(2020, 1, 31)) post.likes.extend(random.sample(users, k=random.randrange(30))) post_objs.append(post) db.session.add_all(post_objs) db.session.commit() print(f'Post table seeded with {num_of_posts} posts...') except Exception as error: print(f'Error: {error}')
def add_post(body, user_id, comments=[]): post = Post() post.body = body post.user_id = user_id if comments: post.comments.extend(comments) post.save() return post
def create_post(user): post_data = request.get_json() print(post_data) post = Post() post.body = post_data.get('post') post.user_id = user.id try: post.save() except (exc.IntegrityError, ValueError): db.session.rollback() return server_error('Something went wrong, please try again.') else: response = jsonify(PostSchema().dump(post)) response.status_code = 201 response.headers['Location'] = url_for( 'posts.get_post', post_id=post.id) return response
def add_post(body, user, post_id=None): post = Post() post.body = body post.user_id = user.id if post_id: post.comment_id = post_id post_notifs = [] for u in user.followers.all(): post_notifs.append( user.add_notification(subject='post', item_id=post.id, id=u.id, post_id=post.id)) db.session.add_all(post_notifs) post.save() return post
def create_post(user, post_id=None): req_data = request.get_json() if not req_data: return bad_request("No request data provided") post = Post() post.body = req_data.get('post') post.user_id = user.id db.session.add(post) if post_id: post.comment_id = post_id parent = Post.find_by_id(post_id) db.session.add( user.add_notification(subject='comment', item_id=post.id, id=parent.author.id, post_id=parent.id)) else: post_notifs = [] for u in user.followers.all(): post_notifs.append( user.add_notification(subject='post', item_id=post.id, id=u.id, post_id=post.id)) db.session.add_all(post_notifs) try: post.save() except (exc.IntegrityError, ValueError): db.session.rollback() return server_error('Something went wrong, please try again.') else: response = jsonify(post.to_dict(user)) response.status_code = 201 response.headers['Location'] = url_for('posts.get_post', post_id=post.id) return response
def seed_posts(num_of_posts): """Seed the database with some posts.""" users = User.query.all() posts1 = [] posts2 = [] post_objs = [] tag_list = ['safe', 'broad', 'witty', 'humorous'] print('Fetching posts...') for i in range(int(num_of_posts/15)): posts1.extend(requests.get( 'https://official-joke-api.appspot.com/jokes/ten').json()) for i in range(int(num_of_posts/15)): posts2.extend(requests.get( 'https://sv443.net/jokeapi/v2/joke/Any?\ blacklistFlags=nsfw,racist,sexist&type=twopart&amount=10', headers={'accept': 'application/json'}).json().get('jokes')) try: print('Saving posts to database...') for p in posts1: user = random.choice(users) tag = p.get('type') tags = [tag, random.choice(tag_list)] body = f"{p.get('setup')} - {p.get('punchline')}" post = Post() post.body = body post.user_id = user.id post.likes.extend(random.sample(users, k=random.randrange(36))) post.created_on = random_timestamp( datetime(2020, 7, 1), datetime(2020, 9, 28)) for t in tags: tag = Tag.query.filter_by(name=t).first() if tag: post.tags.append(tag) else: tag = Tag(name=t) db.session.add(tag) post.tags.append(tag) post_objs.append(post) for p in posts2: user = random.choice(users) tag = p.get('category').lower() flags = p.get('flags') tags = [key.lower() for key in flags if flags[key] is True] tags.append(tag) if bool(p.get('safe')): tags.append('safe') body = f"{p.get('setup')} - {p.get('delivery')}" post = Post() post.body = body post.user_id = user.id post.created_on = random_timestamp( datetime(2020, 9, 1), datetime(2020, 12, 31)) post.likes.extend(random.sample(users, k=random.randrange(30))) for t in tags: tag = Tag.query.filter_by(name=t).first() if tag: post.tags.append(tag) else: tag = Tag(name=t) db.session.add(tag) post.tags.append(tag) post_objs.append(post) db.session.add_all(post_objs) db.session.commit() print(f'Post table seeded with {num_of_posts} posts...') except Exception as error: print(f'Error: {error}')