Пример #1
0
    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
Пример #2
0
    def get_stat(finished):
        """
        Get user's route statistics.

        True: return finished number,
        False: return unfinished number,
        None: return total number

        """
        if finished == True:
            return EnteredRoute.objects(Q(user=current_user.id) & Q(percentage=100)).count()
        elif finished == False:
            return EnteredRoute.objects(Q(user=current_user.id) & Q(percentage__ne=100)).count()
        else:
            return EnteredRoute.objects(user=current_user.id).count()
Пример #3
0
    def get_entered_route(route_id):
        """Get the EnteredRoute Document"""
        assert isinstance(route_id, ObjectId)
        route = RouteHelper.get(route_id)
        assert route

        return EnteredRoute.objects(Q(user=current_user.id) & Q(route=route_id)).first()
Пример #4
0
    def get_user_routes(finished):
        assert isinstance(finished, bool)

        if finished:
            entered_routes = list(EnteredRoute.objects(Q(user=current_user.id) & Q(percentage=100)))
        else:
            entered_routes = list(EnteredRoute.objects(Q(user=current_user.id) & Q(percentage__ne=100)))

        routes = []
        for entered in entered_routes:
            routes.append(RouteHelper.get(entered.route))

        if not finished:
            for route in routes:
                route.percentage = RouteHelper.get_entered_route(route.id).percentage

        return routes
Пример #5
0
    def join(route_id):
        """Join the Route"""
        assert isinstance(route_id, ObjectId)
        route = RouteHelper.get(route_id)
        assert route
        assert not EnteredRoute.objects(Q(user=current_user.id) & Q(route=route.id))
        assert route.finished

        new_entered_route = EnteredRoute()
        new_entered_route.user = current_user.id
        new_entered_route.route = route_id
        new_entered_route.save()

        user = UserHelper.get(current_user.id)
        if route_id not in user.entered_routes:
            user.entered_routes.append(route_id)
        user.save()
Пример #6
0
    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()