Permalink
Browse files

added shonky SQL statement, not currently using it

  • Loading branch information...
Simon Willison
Simon Willison committed Sep 12, 2009
1 parent 8e180ac commit 687fa844c890a719d14dcb98ccc036348cc26cb5
Showing with 19 additions and 2 deletions.
  1. +19 −2 djangopeople/models.py
View
@@ -124,8 +124,25 @@ def irc_nick(self):
return '<none>'
def get_nearest(self, num=5):
- "Returns the nearest X people, but only within the same continent"
- # TODO: Add caching
+ "Returns the nearest X people"
+ # TODO: use this shonky SQL query instead of the other shonky method
+ # From http://code.google.com/apis/maps/articles/phpsqlsearch_v3.html
+ sql = """
+ SELECT id, (
+ 3959 * acos(
+ cos(radians(%(latitude)s)) *
+ cos(radians(latitude)) *
+ cos(
+ radians(longitude) - radians(%(longitude)s)
+ )
+ + sin(radians(%(latitude)s)) *
+ sin(radians(latitude))
+ )
+ ) AS distance
+ FROM djangopeople_djangoperson
+ HAVING distance < 100 ORDER BY distance LIMIT 0, %(num)s
+ """
+ # To search by kilometers instead of miles, replace 3959 with 6371.
people = list(self.country.djangoperson_set.select_related().exclude(pk=self.id))
if len(people) <= num:

0 comments on commit 687fa84

Please sign in to comment.