def ads_for_image(request): if len(request.POST.values())==0: return redirect('/simsearch/') s = xmlrpclib.Server(serverPort, allow_none=True) user_obj = User.objects.get(id=request.user.id) user_pref = UserPreferences.objects.get(user=user_obj) image = request.POST.get('image','') k = image.rfind('/') img = image[k+1:] obj_list = createObjList(s.QueryInterfaceImpl.rpcGetAdsMatchingImage(dataset_id,img,200)) obj_list = checkForFlaggedAds(obj_list,request.user.id) return render(request, "allads.html", {"resultAds":obj_list,"preferences":user_pref,"image":image})
def ht_brown(request): s = xmlrpclib.Server(serverPort, allow_none=True) loc_list = s.QueryInterfaceImpl.rpcGetValues("Location", dataset_id) regions=Regions.objects.defer("id").order_by('short')#.values_list('short', flat=True) regions_lookup={} for r in regions: regions_lookup[r.short]=r.long regions_lookup=json.dumps(regions_lookup) user_obj = User.objects.get(id=request.user.id) # get user state and location preferences user_pref = UserPreferences.objects.get(user=user_obj) user_states = UserLocPref.objects.filter(user=user_obj,type='state').values_list('value',flat=True) user_locs = UserLocPref.objects.filter(user=user_obj,type='location').values_list('value',flat=True) # see if user wants result for other location reqstates=request.GET.getlist("State") reqlocs=request.GET.getlist("Location") num_days=5 #load start date date_dict = s.QueryInterfaceImpl.rpcGetDatasetDates(dataset_id) searchdate=date_dict["EndDate"] startDate = datetime.strptime(date_dict["StartDate"], '%m/%d/%Y').date() endDate = datetime.strptime(date_dict["EndDate"], '%m/%d/%Y').date() if 'num_days' in request.GET: try : num_days=int(request.GET.get("num_days")) if num_days < 1: num_days=1 except (ValueError,TypeError) as e: num_days=5 if '' in reqstates: reqstates.remove('') if '' in reqlocs: reqlocs.remove('') if len(reqstates)>0: query_states=reqstates query_locs=reqlocs else: query_states=user_states query_locs=user_locs if 'Date' in request.GET: val=request.GET.get("Date") if val is not None and val != "": try: selectDate = datetime.strptime(val, '%m/%d/%Y').date() if selectDate < startDate: searchdate=date_dict["StartDate"] elif selectDate > endDate: searchdate=date_dict["EndDate"] else: searchdate=val except ValueError: searchdate=date_dict["EndDate"] # convert from unicode strings to strings query_states=[x.encode('UTF8') for x in query_states] query_locs=[x.encode('UTF8') for x in query_locs] # execute search beginsearchdate= datetime.strptime(searchdate, '%m/%d/%Y').date() - timedelta(days=num_days) if beginsearchdate < startDate: beginsearchdate=date_dict["StartDate"] else: beginsearchdate=beginsearchdate.strftime('%m/%d/%Y') result_dict=s.QueryInterfaceImpl.rpcGetSearchResult(dataset_id, -2, '','', query_states, query_locs,[], '',beginsearchdate, searchdate) obj_list = createObjListClassified(result_dict["searchResult"]) if len(query_locs)>0: resultLocation= ", ".join(query_locs)+" in "+ ", ".join(query_states) else: resultLocation=",".join(query_states) if not len(obj_list) >0: return render(request, 'ht.html', {"user_locations":", ".join(user_locs), "user_states":", ".join(user_states),"resultLocation":resultLocation,"preferences":user_pref, "location_list":loc_list,"regions_lookup":regions_lookup,"regions":regions,"resultLocation":resultLocation,"EndDate":date_dict["EndDate"]}) obj_list = checkForFlaggedAds(obj_list,request.user.id) obj_list = sorted(obj_list, key=lambda SearchResult: SearchResult.trafficking_score,reverse=True) paginator = Paginator(obj_list, len(obj_list)) page = request.GET.get("page") try: ads = paginator.page(page) except PageNotAnInteger: ads = paginator.page(1) except EmptyPage: ads = paginator.page(paginator.num_pages) pageGet=re.sub(r'(page=\d+\&{0,1})','',request.GET.urlencode()) # statistics for map display and timeline jsonStr="".join(result_dict["resultStat"]) geoloc = [] if len(jsonStr) > 0 : statsJsonSparse=json.loads(jsonStr) #get all locations first so database call is only executed once locations = [location.encode('UTF8') for location in statsJsonSparse] currentLocations = LocationsGeo.objects.filter(location__in=locations) gappend=geoloc.append for loc in currentLocations: gappend({'lat': float(loc.lat), 'lon' : float(loc.lon),'obs':statsJsonSparse[loc.location]}) geoloc=json.dumps(geoloc) return render(request, 'brown_ht.html', {'num_days':num_days,'ShowScore':True,'resultAds':ads,"shown":result_dict["adStat"][1], "count_data":geoloc,"user_locations":", ".join(user_locs),"regions":regions, "location_list":loc_list,"regions_lookup":regions_lookup,'pageGet':pageGet, "user_states":", ".join(user_states),"resultLocation":resultLocation,"preferences":user_pref, "SearchDate":searchdate, "loc_list":loc_list,"StartDate":date_dict["StartDate"],"EndDate":date_dict["EndDate"]})
def ht(request): s = xmlrpclib.Server(serverPort, allow_none=True) loc_list = s.QueryInterfaceImpl.rpcGetValues("Location", dataset_id) regions=Regions.objects.defer("id").order_by('short')#.values_list('short', flat=True) regions_lookup={} for r in regions: regions_lookup[r.short]=r.long regions_lookup=json.dumps(regions_lookup) user_obj = User.objects.get(id=request.user.id) # get user state and location preferences user_pref = UserPreferences.objects.get(user=user_obj) user_states = UserLocPref.objects.filter(user=user_obj,type='state').values_list('value',flat=True) user_locs = UserLocPref.objects.filter(user=user_obj,type='location').values_list('value',flat=True) # see if user wants result for other location reqstates=request.GET.getlist("State") reqlocs=request.GET.getlist("Location") if '' in reqstates: reqstates.remove('') if '' in reqlocs: reqlocs.remove('') if len(reqstates)>0: query_states=reqstates query_locs=reqlocs else: query_states=user_states query_locs=user_locs # convert from unicode strings to strings query_states=[x.encode('UTF8') for x in query_states] query_locs=[x.encode('UTF8') for x in query_locs] # execute search result_dict=s.QueryInterfaceImpl.rpcGetLocationFilteredMostLikelyTraffickingAds(dataset_id, 100, query_states, query_locs) obj_list = createObjList(result_dict["searchResult"]) if len(query_locs)>0: resultLocation= ", ".join(query_locs)+" in "+ ", ".join(query_states) else: resultLocation=",".join(query_states) if not len(obj_list) >0: return render(request, 'ht.html', {"user_locations":", ".join(user_locs), "user_states":", ".join(user_states),"resultLocation":resultLocation,"preferences":user_pref,"regions":regions, "location_list":loc_list,"regions_lookup":regions_lookup,"resultLocation":resultLocation}) obj_list = checkForFlaggedAds(obj_list,request.user.id) paginator = Paginator(obj_list, len(obj_list)) page = request.GET.get("page") try: ads = paginator.page(page) except PageNotAnInteger: ads = paginator.page(1) except EmptyPage: ads = paginator.page(paginator.num_pages) # statistics for map display and timeline jsonStr="".join(result_dict["resultStat"]) geoloc = [] if len(jsonStr) > 0 : statsJsonSparse=json.loads(jsonStr) #get all locations first so database call is only executed once locations = [location.encode('UTF8') for location in statsJsonSparse] currentLocations = LocationsGeo.objects.filter(location__in=locations) gappend=geoloc.append for loc in currentLocations: gappend({'lat': float(loc.lat), 'lon' : float(loc.lon),'obs':statsJsonSparse[loc.location]}) geoloc=json.dumps(geoloc) #get dates for which data is available date_dict = s.QueryInterfaceImpl.rpcGetDatasetDates(dataset_id) pageGet=re.sub(r'(page=\d+\&{0,1})','',request.GET.urlencode()) return render(request, 'ht.html', {'resultAds':ads,"total":result_dict["adStat"][0],"shown":result_dict["adStat"][1], "count_data":geoloc,"user_locations":", ".join(user_locs),'pageGet':pageGet, "user_states":", ".join(user_states),"resultLocation":resultLocation,"preferences":user_pref,"regions":regions, "location_list":loc_list,"regions_lookup":regions_lookup,"StartDate":date_dict["StartDate"]})