def _recalculate_avg(route_id): assert isinstance(route_id, ObjectId) route = RouteHelper.get(route_id) assert route route.avg_rate = RateInfo.objects(route=route.id).average('score') route.save()
def get_user_rate(route_id): assert isinstance(route_id, ObjectId) route = RouteHelper.get(route_id) assert route rate_info = RateInfo.objects(Q(route=route_id) & Q(user=current_user.id)).first() if rate_info: return rate_info.score else: return None
def get_route_stat(route_id): assert isinstance(route_id, ObjectId) route = RouteHelper.get(route_id) assert route stat = dict() stat['n_joined'] = EnteredRoute.objects(route=route.id).count() stat['n_completed'] = EnteredRoute.objects(Q(route=route.id) & Q(percentage=100)).count() stat['n_rate'] = RateInfo.objects(route=route.id).count() return stat
def delete_route(route_id): assert isinstance(route_id, ObjectId) route = RouteHelper.get(route_id) assert route for attach_id in route.attached: AttachmentHelper.delete(attach_id) f_cate = Category.objects(id=route.father).first() f_cate.routes.remove(route.id) f_cate.save() enters = EnteredRoute.objects(route=route.id) for entered_route in enters: user = UserHelper.get(entered_route.user) user.entered_routes.remove(route.id) user.save() enters.delete() RateInfo.objects(route=route.id).delete() route.delete()
def get_son_routes(category_id): assert isinstance(category_id, ObjectId) assert CategoryHelper.get(category_id) category = CategoryHelper.get(category_id) result_list = [] for son_route_id in category.routes: result_list.append(RouteHelper.get(son_route_id)) for son_route in result_list: son_route.formatted_date = get_formatted_time(son_route.create_ts) son_route.n_comment = 0 #TODO use disqus api to get comment count son_route.n_rate = RateInfo.objects(route=son_route.id).count() return result_list
def rate(route_id, score): assert isinstance(route_id, ObjectId) route = RouteHelper.get(route_id) assert route assert isinstance(score, int) rate_info = RateInfo.objects(Q(route=route_id) & Q(user=current_user.id)).first() if rate_info: rate_info.score = score rate_info.save() else: rate_info = RateInfo() rate_info.route = route_id rate_info.user = current_user.id rate_info.score = score rate_info.save() RouteHelper._recalculate_avg(route_id)