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)
评论列表
文章目录