[Commits] python.it commit r272 - in code/pythonisti/trunk/pythonisti/apps: geo profile

commit a svn.python.it commit a svn.python.it
Dom 21 Gen 2007 18:30:56 CET


Author: rhymes
Date: Sun Jan 21 18:30:52 2007
New Revision: 272

Modified:
   code/pythonisti/trunk/pythonisti/apps/geo/google.py
   code/pythonisti/trunk/pythonisti/apps/geo/models.py
   code/pythonisti/trunk/pythonisti/apps/geo/views.py
   code/pythonisti/trunk/pythonisti/apps/profile/models.py
Log:
Tag has become Skill, now it's more meaningful. Every model has been extended to be more admin-friendly and proper. GeoLocation and UserProfile no longer have the circular dependency they had; google.py has been fixed accordingly.


Modified: code/pythonisti/trunk/pythonisti/apps/geo/google.py
==============================================================================
--- code/pythonisti/trunk/pythonisti/apps/geo/google.py	(original)
+++ code/pythonisti/trunk/pythonisti/apps/geo/google.py	Sun Jan 21 18:30:52 2007
@@ -8,7 +8,7 @@
 
 from urllib import urlencode
 from urllib2 import urlopen
-from django.utils.simplejson import loads
+from django.utils import simplejson as json
 
 from django.conf import settings
 
@@ -48,17 +48,19 @@
 
     address must be an Unicode string
     """
+    
+    # XXX: google returns an Accuracy field to explore
 
     url = 'http://maps.google.com/maps/geo?'
     cmd = {
         'q': address.encode('utf-8'),
         'output': 'json',
         'key': settings.GOOGLE_API_KEY
-        }
+    }
 
     # Get the response from Google
     page = get_page(url + urlencode(cmd))
-    response = loads(page.encode('utf-8'))
+    response = json.loads(page.encode('utf-8'))
 
     name = response['name']        
     status = response['Status']
@@ -69,10 +71,16 @@
     if status['code'] != 200:
         raise AddressNotFound()
 
-    r = response['Placemark']
-    if len(r) > 1:
+    location = response['Placemark']
+    if len(location) > 1:
         # TODO for now we raise an exception
+        # XXX: TO FIX
         address_list = [item['address'] for item in r]
         raise MultipleAddress(address_list)
 
-    return r[0]
+    gloc = location[0]
+    
+    # set the coordinates and the whole data
+    latitude, longitude = gloc['Point']['coordinates'][:-1]
+
+    return latitude, longitude, json.dumps(gloc)

Modified: code/pythonisti/trunk/pythonisti/apps/geo/models.py
==============================================================================
--- code/pythonisti/trunk/pythonisti/apps/geo/models.py	(original)
+++ code/pythonisti/trunk/pythonisti/apps/geo/models.py	Sun Jan 21 18:30:52 2007
@@ -1,34 +1,24 @@
-from django.utils.simplejson import dumps
-
 from django.db import models
-from django.contrib.auth.models import User
-
-from pythonisti.apps.geo import google
-
 
 class GeoLocation(models.Model):
-    # The address of the user
-    location = models.CharField(maxlength=50)
-    # The normalized address of the user
-    address = models.CharField(maxlength=50, editable=False)
-    # The geo location of the user, in the format '(lat, long')
-    geolocation = models.CharField(maxlength=50, editable=False)
-    # The geo location detail, in JSON format as returned by the
-    # Google Geocoder
-    geolocation_detail = models.CharField(maxlength=256, editable=False)
- 
-
-    def save(self):
-        # Setup the computed fields
-        # XXX Django does not supports Unicode
-        response = google.get_geo_location(self.location.decode('utf-8'))
-        coordinates = response['Point']['coordinates'][:-1]
-
-        self.address = response['address'].encode('utf-8')
-        self.geolocation = dumps(coordinates)
-        self.geolocation_detail = dumps(response)
+    # the longitude of the location
+    longitude = models.IntegerField()
+    # the latitude of the location
+    latitude = models.IntegerField()
+    # the geo location detail, in JSON format as returned by the geocoder
+    geo_location = models.CharField(maxlength=256)
+    
+    def get_coordinates(self):
+        return self.latitude, self.longitude
+    coordinates = property(get_coordinates)
         
-        return super(GeoLocation, self).save()
+    def get_address(self):
+        return self.geo_location['address']
+    address = property(get_address)
+ 
+    def __str__(self):
+        return "%s: %s", (self.address, self.coordinates)
 
     class Admin:
-        list_display = ('address', 'geolocation')
+        list_display = ('address', 'coordinates')
+        search_fields = ('address')

Modified: code/pythonisti/trunk/pythonisti/apps/geo/views.py
==============================================================================
--- code/pythonisti/trunk/pythonisti/apps/geo/views.py	(original)
+++ code/pythonisti/trunk/pythonisti/apps/geo/views.py	Sun Jan 21 18:30:52 2007
@@ -10,10 +10,11 @@
     locations = models.GeoLocation.objects.all()
     
     content = []
-    for item in locations:
+    for location in locations:
         content.append({
-                'info': item.username.username,
-                'location': simplejson.loads(item.geolocation)
+                # XXX: to fix
+                'info': location.users.all() or "anonymous",
+                'location': simplejson.loads(location.geo_location)
         })
     
     json_locations = simplejson.dumps(content)

Modified: code/pythonisti/trunk/pythonisti/apps/profile/models.py
==============================================================================
--- code/pythonisti/trunk/pythonisti/apps/profile/models.py	(original)
+++ code/pythonisti/trunk/pythonisti/apps/profile/models.py	Sun Jan 21 18:30:52 2007
@@ -3,48 +3,90 @@
 
 from pythonisti.apps.geo.models import GeoLocation
 
-class Tag(models.Model):
+class Skill(models.Model):
     name = models.CharField(blank=False, maxlength=30)
+    
+    def __str__(self):
+        return self.name
+        
+    class Admin:
+        search_fields = ('name')
 
 class Country(models.Model):
     # ISO country code
-    code = models.CharField(maxlength=2, primary_key=2)
+    code = models.CharField(maxlength=2, primary_key=True, editable=False)
     # ISO name, see:
     #  http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html
     name = models.CharField(maxlength=50)
     
+    def __str__(self):
+        return self.name
+    
+    class Meta:
+        verbose_name_plural = _('countries')
+    
+    class Admin:
+        list_display = ('name', 'code')
+        ordering = ('name')
+        search_fields = ('name', 'code')
+
+    
 class Place(models.Model):
-    country = models.ForeignKey(Country)
+    country = models.ForeignKey(Country, related_name="places")
     address = models.CharField(blank=True, maxlength=50)
     city = models.CharField(blank=False, maxlength=50)
-    # the region, area of living
-    administrative_area = models.CharField(blank=True, maxlength=50)
     # the province or district 
-    sub_administrative_area = models.CharField(blank=True, maxlength=50)
+    sub_administrative_area = models.CharField(blank=True, maxlength=50,
+        verbose_name="Province or district")
+    # the region, area of living
+    administrative_area = models.CharField(blank=True, maxlength=50,
+        verbose_name="Region, county or area of living")
     # this makes sense in federal countries like USA
     state = models.CharField(blank=True, maxlength=50)
     # the post/zip code itself
-    postcode = models.CharField(blank=True, maxlength=10)
+    postcode = models.CharField(blank=True, maxlength=10,
+        verbose_name="Post/Zip code")
+        
+    def __str__(self):
+        return "%s, %s" % (self.country.name, self.city)
+        
+    class Admin:
+        list_filter = ('country',)
+        ordering = ('city', 'administrative_area',
+            'sub_administrative_area')
+        search_fields = ('city', 'address', 'state', '=postcode')
     
 class UserProfile(models.Model):
     # the user whom this profile belongs to
     user = models.OneToOneField(User)
     # the geo-location of the profile
-    geo_location = models.ForeignKey(GeoLocation)
+    geo_location = models.ForeignKey(GeoLocation, related_name="users",
+        null=True, blank=True)
     # the place where the user lives in
     place = models.ForeignKey(Place)
     # the user avatar
-    avatar = models.CharField(maxlength=50, blank=True)
-    # the user specialties
-    tags = models.ManyToManyField(Tag, related_name="users")
+    avatar = models.ImageField(null=True, blank=True, upload_to='/i/avatars/')
+    # the user skills
+    skills = models.ManyToManyField(Skill, related_name="users", blank=True)
     # the company where the user works
     company = models.CharField(maxlength=50, blank=True)
     # is she/he available for hire?
-    for_hire = models.BooleanField(blank=False, default=False)
+    for_hire = models.BooleanField(default=False)
     # is she/he available for consultancy or contract work?
-    for_consultancy = models.BooleanField(blank=False, default=False)
+    for_consultancy = models.BooleanField(default=False)
     # when the profile has been created
     creation_date = models.DateTimeField(auto_now_add=True)
     # when the profile has been modified
     last_modified = models.DateTimeField(auto_now=True)
     
+    def __str__(self):
+        return "%s" % self.user.get_full_name() or self.user.username
+    
+    class Meta:
+        get_latest_by = 'creation_date'
+    
+    class Admin:
+        date_hierarchy = 'creation_date'
+        list_filter = ('creation_date', 'for_hire', 'for_consultancy')
+        search_fields = ('user')
+    


Maggiori informazioni sulla lista Commits