def dashboard(request): user_obj = User.objects.get(id=request.user.id) s = xmlrpclib.Server(serverPort, allow_none=True) #get indices of user's currently flagged ads flaggedAds=UserFlaggedAds.objects.filter(user=user_obj).values_list('ad_id', flat=True) #convert to list of integers because django returns long integer flaggedAds=[int(i) for i in flaggedAds] #get flagged ads if len(flaggedAds)>0: flagged_list=s.QueryInterfaceImpl.rpcGetMultipleAdsByAdIndex(dataset_id,flaggedAds) obj_list = createObjList(flagged_list) for result in obj_list: result.flagged=1 else: obj_list=[] user_states = UserLocPref.objects.filter(user=user_obj,type='state') if user_states.exists(): user_states_list = user_states.values_list('value',flat=True) else: user_states_list=[] user_locs = UserLocPref.objects.filter(user=user_obj,type='location') if user_locs.exists(): user_loc_list = user_locs.values_list('value',flat=True) else: user_loc_list=[] user_loc_list = [ loc.replace("_"," ") for loc in user_loc_list] user_pref = UserPreferences.objects.get(user=user_obj) return render(request,'dashboard.html', {"preferences":user_pref,'resultAds':obj_list,'user': user_obj,"user_locations":", ".join(user_loc_list),"user_states":", ".join(user_states_list)})
def Al_request(request): if AlRequest.objects.all().count() == 0: return HttpResponseRedirect('/users/') #create the set of ad ids that should not be shown to the user #-first remove all ads that the user has already seen id_set=set(AlResponse.objects.filter(user_id =request.user.id).values_list('ads_id', flat=True)) #-remove all ads that have reached the label limit, i.e. they have been labeled 5 times. gt means greater than id_set.update(AlResponse.objects.values('ads_id').annotate(num_ads=Count('ads_id')).filter(num_ads__gt=5).values_list('ads_id', flat=True)) #-lastly remove all ads that will be banked, i.e. more then one answer is positive or negative id_set.update(AlResponse.objects.values('ads_id').filter(response=1).annotate(num_ads=Count('ads_id')).filter(num_ads__gt=2).values_list('ads_id', flat=True)) id_set.update(AlResponse.objects.values('ads_id').filter(response=-1).annotate(num_ads=Count('ads_id')).filter(num_ads__gt=2).values_list('ads_id', flat=True)) ads_to_show=AlRequest.objects.values('ads_id').exclude(ads_id__in=list(id_set)).order_by('uncertainty')[:3] ads_index_list=ads_to_show.values_list('ads_id', flat=True) if len(ads_index_list)>0: s = xmlrpclib.Server(serverPort, allow_none=True) ad_list = [] for i in ads_index_list: targetAd = s.QueryInterfaceImpl.rpcGetAdByAdIndex(dataset_id, i) ad_list.extend(targetAd) obj_list = createObjList(ad_list) AdsJSON=json.dumps([ads.__dict__ for ads in obj_list]) return render(request,'activelearning.html', {"resultAdsJSON":AdsJSON}, context_instance=RequestContext(request)) else: return HttpResponseRedirect('/users/')
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(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"]})