views.py 文件源码

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

项目:SuperPACs 作者: SpencerNorris 项目源码 文件源码
def search(request):
    '''
    Searches through all available sources to match the user's query
    '''
    query = request.GET["query"]

    ## match the query against a zipcode regex, go a zipcode search if it matches
    if re.match("^\d{5}$", query):
        ## here we call an external api to search for the reps via zipcode
        results = SunlightAPI().rep_by_zip(query)

        ## loop through the results
        reps = []
        for rep in results["results"]:
            ## try finding the rep in our database
            reps += (Representative.objects.all()
                .annotate(name=Concat('first_name', Value(" "), 'last_name'))
                .filter(name=rep["first_name"]+" "+rep["last_name"])
                .values("id", "name", "party"))
        ## return the found reps
        return JsonResponse({"representatives": reps, "committees": []})

    data = {}

    data["representatives"] = list(Representative.objects.all()
        .annotate(name=Concat('first_name', Value(" "), 'last_name'))
        .filter(name__icontains=query)
        .values("id", "name", "party"))

    data["committees"] = list(SuperPAC.objects.filter(name__icontains=query)
        .values("id", "name"))

    if "." in query or len(query) > 5:
        results = SunlightAPI().search(query)
        data["bills"] = results["results"]

    return JsonResponse(data)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号