Commits

Colin Copeland committed e5e2d42

auto calculate distances on save

Comments (0)

Files changed (2)

lib/copelco/apps/estate/admin.py

 admin.site.register(estate.Place, PlaceAdmin)
 
 
-class PlaceDistanceInline(admin.TabularInline):
-    model = estate.PlaceDistance
-    exclude = ('distance',)
-
-
 class PhotoInline(admin.TabularInline):
     model = estate.Photo
 
 
 class HouseAdmin(admin.ModelAdmin):
-    inlines = [PlaceDistanceInline, PhotoInline]
+    inlines = [PhotoInline]
     list_display = ('address', 'zipcode', 'year_built', 'listing_price',
                     'sqft', 'acres', 'num_bathrooms', 'num_bedrooms', 'coords',
                     'rank')

lib/copelco/apps/estate/models.py

 from django.conf import settings
 from django.core.exceptions import ValidationError
 from django.core.urlresolvers import reverse
+from django.db.models.signals import post_save
 
 from googlemaps import GoogleMaps
 
         cache_dir = 'photos'
         image_field = 'original_image'
 
+
+def set_distance(house, place):
+    try:
+        place = PlaceDistance.objects.get(place=place, house=house)
+    except PlaceDistance.DoesNotExist:
+        place = PlaceDistance(place=place, house=house)
+    place.clean()
+    place.save()
+
+
+def place_post_save(**kwargs):
+    place = kwargs['instance']
+    for house in House.objects.all():
+        set_distance(house=house, place=place)
+
+
+def house_post_save(**kwargs):
+    house = kwargs['instance']
+    for place in Place.objects.all():
+        set_distance(house=house, place=place)
+
+
+post_save.connect(place_post_save, sender=Place)
+post_save.connect(house_post_save, sender=House)
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.