Example #1
0
def locQuery(location, **kwargs):
    """
    Function to return matching locations. Can be optimized in 2 manners:
    1) Get rid of for loops on WaterBody and BaseLocation querysets
    2) Optimize locDetermine()
    """
    q = GoogleLatLng()
    query = GuideCore.objects.none()
    loc, type = locDetermine(location)
    if not type:
        if q.requestLatLngJSON(location):
            loc, type = q, q.results['types']
            newloc = BaseLocation()
            try:
                newloc.save(mquery=q)
            except:
                pass
        elif q.requestLatLngJSON(location, True):
            loc, type = q, q.results['types']
            newloc = WaterBody()
            try:
                newloc.save(mquery=q)
            except:
                pass
            if newloc.id: newloc.associate_locations()
        else:
            return query
    rad = kwargs.get('radius', 100)
    if 'country' in type:
        # If it was a search by Country name
        try:
            l = loc.results['address_components'][0]['short_name']
        except:
            l = loc.country.abbr
        query = GuideCore.objects.filter(Q(locations__country__abbr=l))
    elif 'administrative_area_level_1' in type:
        # If it was a search by a state name
        try:
            l = loc.results['address_components'][0]['short_name']
        except:
            l = loc.state.key
        query = GuideCore.objects.filter(Q(locations__state__key=l))
    else:
        # If is was a search by a city name or a WaterBody
        locs = BaseLocation.neighbours.nearby_locations(loc.lat, loc.lng, rad, True).select_related('FishingGuides')
        for l in locs:
            query = query | GuideCore.objects.filter(locations=l)
        locs = WaterBody.neighbours.nearby_locations(loc.lat, loc.lng, rad, True).select_related('Operators')
        for w in locs:
            query = query | GuideCore.objects.filter(waterbodies=w)
    return query
Example #2
0
def locQuery(location, **kwargs):
    """
    Function to return matching locations. Can be optimized in 2 manners:
    1) Get rid of for loops on WaterBody and BaseLocation querysets
    2) Optimize locDetermine()
    """
    q = GoogleLatLng()
    query = GuideCore.objects.none()
    loc, type = locDetermine(location)
    print "\nOutput of locDetermine:\n", (loc, type)
    if not type:
        if q.requestLatLngJSON(location):
            loc, type = q, q.results['types']
            newloc = BaseLocation()
            newloc.save(mquery=q)
            print "\nSaved a land location\n"
        elif q.requestLatLngJSON(location, True):
            loc, type = q, q.results['types']
            newloc = WaterBody()
            newloc.save(mquery=q)
            if newloc.id: newloc.associate_locations()
            print "\nSaved a water location\n"
        else:
            return query
    rad = kwargs.get('radius', 100)
    if 'country' in type:
        # If it was a search by Country name
        query = GuideCore.objects.filter(Q(locations__country__abbr__icontains=location) |
                                         Q(locations__country__name__icontains=location))
    elif 'administrative_area_level_1' in type:
        # If it was a search by a state name
        l = location.split(',')[0]
        query = GuideCore.objects.filter(Q(locations__state__key__icontains=l) |
                                         Q(locations__state__name__icontains=l))
    else:
        # If is was a search by a city name or a WaterBody
        locs = BaseLocation.neighbours.nearby_locations(loc.lat, loc.lng, rad, True).select_related('FishingGuides')
        for l in locs:
            query = query | GuideCore.objects.filter(locations=l)
        locs = WaterBody.neighbours.nearby_locations(loc.lat, loc.lng, rad, True).select_related('Operators')
        for w in locs:
            query = query | GuideCore.objects.filter(waterbodies=w)
    print "\nOutput of locQuery:\n", query.distinct()
    return query