Example #1
0
 def clean(self, **kwargs):
     mquery = kwargs.get('mquery')
     if self.city or mquery:
         if not mquery:
             mquery = GoogleLatLng()
             state = self.state.name if self.state else ''
             country = self.country.abbr if self.country else ''
             if not mquery.requestLatLngJSON(self.city + ', ' + state + ', ' + country):
                 raise ValidationError("There is an error in the location string")
         l = mquery.parseLocation()
         if not l: # It is assumed that all args to clean with mquery will be cities
             raise ValidationError("This city could not be found")
         self.json_response = mquery.results
         self.city = l[0]
         newcountry, created = Country.objects.get_or_create(abbr=l[-1][1], name=l[-1][0],
                                                             defaults={'abbr':l[-1][1], 'name':l[-1][0]})
         self.country = newcountry
         if len(l) > 2:
             newstate, created = State.objects.get_or_create(key=l[-2][1], name=l[-2][0], country=self.country,
                                                             defaults={'key':l[-2][1],
                                                                       'name':l[-2][0],
                                                                       'country':self.country})
             self.state = newstate
         else:
             self.state = None
         self.lat = mquery.lat
         self.lng = mquery.lng
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)
    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 #3
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
Example #4
0
 def clean_location(self):
     address_1 = self.address_line_1 if self.address_line_1 else ''
     state = self.state.name if self.state else ''
     address_2 = self.address_line_2 if self.address_line_2 else ''
     ncity, created = BaseLocation.objects.get_or_create(city=self.city, state__name=state, country=self.country,
                                                         defaults={'city':self.city,
                                                                   'state':self.state,
                                                                   'country':self.country})
     locQuery = GoogleLatLng()
     if not locQuery.requestLatLngJSON(address_1 + ', ' + address_2 + ', ' + self.city + ', ' + state + ', ' + self.country.abbr):
         if not locQuery.requestLatLngJSON(address_2 + ', ' + self.city + ', ' + state + ', ' + self.country.abbr):
             self.lat = ncity.lat
             self.lng = ncity.lng
         else:
             self.lat = locQuery.lat
             self.lng = locQuery.lng
     else:
         self.lat = locQuery.lat
         self.lng = locQuery.lng