def test_get_network_posts(): """ Tests we can retrieve a network's posts. For illustrative purposes, client returns mock data. """ c = Client(mock=True) posts = c.get_network_posts(2, 10) assert_true(len(posts) == 3) posts1 = c.get_network_posts(2, 1) assert_equal(posts1[0]['id'], 4) assert_equal(len(posts1), 1) posts2 = c.get_network_posts(2, 1, max_id=3) assert_equal(posts2[0]['id'], 2) assert_equal(len(posts2), 1)
def network_posts() : # TODO: A lot of this code is repeated from network(), with just minor variations. # This should be factored out. id_network = request.args.get('id') if not id_network : return render_template('404.html') c = Client(mock=False) try: id_network = int(id_network) except ValueError: return render_template('404.html') network = c.get_network(id_network) if not network: return render_template('404.html') # TODO: Get user ID and work out if user is in network. # Add join us button to page if they're not. posts = None old_index = request.args.get('index') if not old_index: posts = c.get_network_posts(id_network, 10) else: try: old_index = int(old_index) except ValueError: return render_template('404.html') posts = c.get_network_posts(id_network, 10, old_index - 1) for post in posts: post['username'] = c.get_user(post['id_user'])['username'] post['reply_count'] = c.get_post_reply_count(post['id'])['reply_count'] post['time_ago'] = get_time_ago(post['post_date']) # TODO: Add better handling for when there's no events left. if not posts : post_index = old_index else : post_index = posts[-1]['id'] id_user = current_user.id user_networks = c.get_user_networks(id_user, count=100) user_is_member = False for network_ in user_networks: if int(id_network) == int(network_['id']): user_is_member = True break network_info = {} network_info['id'] = id_network network_info['posts'] = posts network_info['user_is_member'] = user_is_member num_users = c.get_network_user_count(id_network)['user_count'] num_posts = c.get_network_post_count(id_network)['post_count'] network_info['num_users'] = num_users network_info['num_posts'] = num_posts network_info['network_title'] = get_network_title(network) return render_template('network_posts.html', network_info=network_info, post_index=post_index)