Exemplo n.º 1
0
def trail_page(request):
    # check if get is available
    if len(request.GET.values())==0:
        return redirect('/search/')
    s = xmlrpclib.Server(serverPort, allow_none=True)
    # get user preferences
    user_obj = User.objects.get(id=request.user.id)
    user_pref = UserPreferences.objects.get(user=user_obj)

    # get trail. last argument is the amount of trail results you want to return at most
    targetAdIndex = int(request.GET.get("targetAd"))
    trail_list = s.QueryInterfaceImpl.rpcGetTrailResults(dataset_id, targetAdIndex, TRAIL_LIMIT)

    # get details for trailed ad
    targetAd = s.QueryInterfaceImpl.rpcGetAdByAdIndex(dataset_id, targetAdIndex)
    targetAdObject = createObjList(targetAd)[0]

    # generate list of trail result and get unique locations within
    obj_list,locations = createObjListPlusLocationSet(trail_list)
	
    if len(obj_list)==0:
            return render(request, "trail.html", {"preferences":user_pref,"resultAds":obj_list, "targetAd":targetAdObject,
            "phoneCount":0, "phoneDict":{}, "locDict":{},
            "locCount":0, "trailtotal":0, "imageList":[],"resultAdsJSON":"{}"})
    

    #load locations
    currentLocations = LocationsGeo.objects.filter(location__in=list(locations)).values()
    missingLocations = frozenset(LocationsMissing.objects.all().values_list('location', flat=True))
    missing = frozenset(locations).difference(missingLocations,frozenset(currentLocations.values_list('location', flat=True)))
    if len(missing)>0:
        insert_list = []
        for x in missing:
            insert_list.append(LocationsMissing(location=x))
        LocationsMissing.objects.bulk_create(insert_list)

    #create a geo lookup dictionary
    geo_lookup={}
    for obj in currentLocations:
	geo_lookup[obj['location'].encode('UTF8')]=[obj['lat'],obj['lon']]

    flaggedAds=frozenset(UserFlaggedAds.objects.filter(user=user_obj).values_list('ad_id', flat=True))
    phone_dict = {}
    loc_dict = {}
    images = {}
    for ad in obj_list:
        #add geoinfo
        if ad.location in geo_lookup:
            ad.lat=float(geo_lookup[ad.location][0])
            ad.lon=float(geo_lookup[ad.location][1])
        #check for flagged ad
        if long(ad.adIndex) in flaggedAds:
            ad.flagged=1
        #calc phone numbers
        for phonenum in ad.phonenumbers:
            if phonenum in phone_dict:
                phone_dict[phonenum] += 1
            else:
                phone_dict[phonenum] = 1
        #calc locations
        if ad.displaylocation in loc_dict:
            loc_dict[ad.displaylocation] += 1
        else:
            loc_dict[ad.displaylocation] = 1
        #get (unique) images and associated ad IDs
        for img in ad.images:
            if not img in images:
                images[img]=[ad.adIndex]
            else:
                images[img].append(ad.adIndex)

    #paginate
    paginator = Paginator(obj_list, 100)
    page = request.GET.get("page")
    pageGet=re.sub(r'(page=\d+\&{0,1})','',request.GET.urlencode())
    if 'page' in request.GET:
        page = request.GET.get("page")
    else:
        page=1
        #create history entry
        if 'Al' not in request.GET:
            permHistory = UserHistoryPermanent(user_id=request.user.id,searchget=request.get_full_path(),type="trail")
            permHistory.save()
            if user_pref.save_history==1:
                targetAdString="location: <i>"+targetAdObject.displaylocation+"</i> phone: <i>"+",".join(targetAdObject.phonenumbers)+"</i> age: <i>"+targetAdObject.age+"</i> posted: <i>"+targetAdObject.date+"</i>"
                newHistory = UserHistory(user_id=request.user.id, searchtext=targetAdString,searchget=request.get_full_path(),type="trail")
                newHistory.save()
    resultAds = paginator.page(page)
    #trailResultList for map and graph
    AdsJSON=json.dumps([ads.__dict__ for ads in obj_list])
    #render to response
    c= {}
    c.update(csrf(request))
    c.update({"preferences":user_pref,"resultAds":resultAds, "targetAd":targetAdObject,
            "phoneCount":len(phone_dict), "phoneDict":phone_dict, "locDict":loc_dict,'pageGet':pageGet,
            "locCount":len(loc_dict), "trailtotal":len(obj_list), "imageList":images,"resultAdsJSON":AdsJSON})
    return render(request, "trail.html",c)
Exemplo n.º 2
0
def result_page(request):
    s = xmlrpclib.Server(serverPort, allow_none=True)
    #get user preferences
    user_obj = User.objects.get(id=request.user.id)
    user_pref = UserPreferences.objects.get(user=user_obj)

    #attrName_list = ["State", "Age", "Size", "Location"]
    attrName_list = ["Age", "Size", "Location"]
    attr_dict = s.QueryInterfaceImpl.rpcGetAttributes(attrName_list, dataset_id)
    #remove none form dict
    #attr_dict["State"]=attr_dict["State"][1:]
    #list of keywords available from dropdown
    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)



    keyword_list = s.QueryInterfaceImpl.rpcGetKeywordsAttributes(dataset_id)
    #get dates for which data is available
    date_dict = s.QueryInterfaceImpl.rpcGetDatasetDates(dataset_id) 
    # check if get is available
    if len(request.GET.values())==0:
        return redirect('/search/')
    #get search request
    search_request_dict = getDataFromRequest(request,date_dict) # get the search criteria the user enters
    #return to search page if request could not be parsed
    if not search_request_dict:
	return redirect('/search/')
    #make json of searchrequest so it can be sent back with results
    search_request_json=json.dumps(search_request_dict)

    #make request to data server
    result_dict = s.QueryInterfaceImpl.rpcGetSearchResult(dataset_id, search_request_dict["Age"], 
            search_request_dict["Size"], search_request_dict["PhoneNumber"], search_request_dict["State"], 
            search_request_dict["Location"], search_request_dict["Keyword"], search_request_dict["AnyKeyword"], 
            search_request_dict["StartDate"], search_request_dict["EndDate"]) 

    #result_dict["resultStat"]=[]

    obj_list = createObjList(result_dict["searchResult"])
    obj_list = checkForFlaggedAds(obj_list,request.user.id)

    if len(result_dict["adStat"])>0:
        total=result_dict["adStat"][0]
        shown=len(obj_list)
    else:
        total=0
        shown=0
    # statistics for map display and graph
    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)	
        missingLocations = frozenset(LocationsMissing.objects.all().values_list('location', flat=True))
        missing = frozenset(locations).difference(missingLocations,frozenset(currentLocations.values_list('location', flat=True)))
        if len(missing)>0:
            insert_list = []
            for x in missing:
                insert_list.append(LocationsMissing(location=x))
            LocationsMissing.objects.bulk_create(insert_list)
        gappend=geoloc.append
        for loc in currentLocations:
            gappend({'lat': float(loc.lat), 'lon' : float(loc.lon),'obs':statsJsonSparse[loc.location]})
        geoloc=json.dumps(geoloc)
    # pagination
    paginator = Paginator(obj_list, page_size)
    page = request.GET.get("page")
    squerydict=request.GET.copy()
    squerydict.setdefault('EndDate',search_request_dict["EndDate"])
    searchGet='?'+squerydict.urlencode()
    pageGet=re.sub(r'(page=\d+\&{0,1})','',squerydict.urlencode())
    try:
        resultAds = paginator.page(page)
    except PageNotAnInteger:
        # results shown for first time
        resultAds = paginator.page(1)
        # create text description of search for history, then save
        # only when the result is shown for the 1st time
        if len(obj_list) >0:
            permHistory = UserHistoryPermanent(user_id=request.user.id,searchget=request.get_full_path(),type="search")
            permHistory.save()
            if user_pref.save_history==1:
                #set end date to today if it is empty
		search=request.path+searchGet
                if search_request_dict["Age"] != -2:
                    keywordStr = "Age:" + str(search_request_dict["Age"]) + " "
                else:
                    keywordStr = ""
                if search_request_dict["Size"] !="":
                    keywordStr = keywordStr + "Size:" + search_request_dict["Size"] + " "
                if search_request_dict["PhoneNumber"] is not None and len(search_request_dict["PhoneNumber"]) > 0:
                    keywordStr = keywordStr + "PhoneNumber:" + search_request_dict["PhoneNumber"] + " "
                if len(search_request_dict["State"]) > 0:
                    keywordStr = keywordStr + "State:" + ", ".join(search_request_dict["State"]) + " "
                if len(search_request_dict["Location"])>0:
                    keywordStr = keywordStr + "Location:" + ", ".join(search_request_dict["Location"]) + " "
                if len(search_request_dict["Keyword"]) > 0:
                    keywordStr = keywordStr + "Keyword:" + search_request_dict["Keyword"][0] + " "
                if len(search_request_dict["AnyKeyword"]) > 0:
                    keywordStr = keywordStr + "AnyKeyword:" + search_request_dict["AnyKeyword"] + " "
                keywordStr = keywordStr + "StartDate:" + search_request_dict["StartDate"] + " " + "EndDate:" + \
                        search_request_dict["EndDate"]
                newHistory = UserHistory(user_id=request.user.id, searchtext=keywordStr,searchget=search,type="search")
                newHistory.save()
    except EmptyPage:
        resultAds = paginator.page(paginator.num_pages)
    # replace empty list [] with "", for building GET request in html
    if len(search_request_dict["State"]) == 0:
        search_request_dict["State"] = ""
    else:
        search_request_dict["State"] = search_request_dict["State"][0]
    if len(search_request_dict["Location"]) == 0:
        search_request_dict["Location"] = ""
    else:
        search_request_dict["Location"] = search_request_dict["Location"][0]
    if len(search_request_dict["Keyword"]) == 0:
        search_request_dict["Keyword"] = ""
    else:
        search_request_dict["Keyword"] = search_request_dict["Keyword"][0]
    if search_request_dict["Size"] is None:
        search_request_dict["Size"] = ""
    return render(request,"newresult.html", {"preferences":user_pref,"resultAds":resultAds, 
            "Age":search_request_dict["Age"], "Size":search_request_dict["Size"], 
            "PhoneNumber":search_request_dict["PhoneNumber"], 
            "State":search_request_dict["State"], "Location":search_request_dict["Location"], 
            "Keyword":search_request_dict["Keyword"], "AnyKeyword":search_request_dict["AnyKeyword"], 
            "StartDate":date_dict["StartDate"], "EndDate":date_dict["EndDate"], 
            "regions":regions,"regions_lookup":regions_lookup,"age_list":attr_dict["Age"], 
            "size_list":attr_dict["Size"], "location_list":attr_dict["Location"], 
            "keyword_list":keyword_list, "total":total,"searchGet":searchGet,'pageGet':pageGet, 
            "shown":shown,"Last_Request":search_request_json,"count_data":geoloc})