示例#1
0
def get_posts():
	current_user = g.user
	current_location = g.user_location

	max_dist_str = request.args.get('max_dist', 25)
	max_id_str = request.args.get('max_id', None)
	page_num_str = request.args.get('page', 1)
	post_per_page_str = request.args.get('post_per_page', 10)

	try:
		max_dist = int(max_dist_str)
		page_num = int(page_num_str)
		post_per_page = int(post_per_page_str)

		max_id = None
		if(max_id_str != None):
			max_id = int(max_id_str)
	except ValueError as e:
		return {
			"error" : True,
		}

	current_posts = post_service.get_post_feed(current_user.user_id, current_location, max_dist, page_num, post_per_page, max_id)

	if max_id == None and len(current_posts) > 0:
		max_id = current_posts[0].post_id

	output_json = {
		"error" : False,
		"max_id" : max_id,
		"current_location" : current_location.to_json_dict(),
		"pagination": {
			"page_num" : page_num,
			"post_per_page" : post_per_page,
			"length" : len(current_posts)
		}
	}

	posts_json = []
	for post in current_posts:
		post_json = post.to_json_dict()

		posts_json.append(post_json)

	output_json["posts"] = posts_json

	return output_json
	def test_get_post_by_id(self, get_post_feed):
		test_loc = Location("Test", 1.0, 1.0)

		post_service.get_post_feed(1, test_loc)
		self.assertTrue(get_post_feed.called)