routes.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:lopeningent_backend 作者: oSoc17 项目源码 文件源码
def rate_route(request):
    """
    Adds the rating to all edges in a route, and saves it both in the structure and in the database.

    Query args:
    rid -- the id for the rated route
    rating -- a float between 0 and 5
    """
    try:
        tag = request.POST.get('visited_path')
        new_rating = float(request.POST.get('rating'))
        path = from_string(GRAPH, tag)
        edgecoords = [(s, e) for s, e in zip(path, path[1:])]

        def update_rating(edge):
            for edge in GRAPH.get_edges():
                for s, e in edgecoords:
                    if s == edge.id and e == edge.to:
                        edge._rating = (edge._rating + new_rating) / 2
                        update_edge_in_db(edge)

            return edge

        GRAPH.map_graph(lambda _: _, update_rating)
        return HttpResponse('')
    except:
        return HttpResponseNotFound("Something went wrong while rating your route.")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号