1. powersurge360
  2. groupGo

Commits

powersurge360  committed af904b0

Added a migration for latitude/longitude and implemented the geocoding.

  • Participants
  • Parent commits b85c31f
  • Branches master

Comments (0)

Files changed (2)

File groupGo/apps/organizations/migrations/0002_auto__add_field_organization_description__add_field_organization_latit.py

View file
+# -*- coding: utf-8 -*-
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        # Adding field 'Organization.description'
+        db.add_column(u'organizations_organization', 'description',
+                      self.gf('django.db.models.fields.TextField')(default=''),
+                      keep_default=False)
+
+        # Adding field 'Organization.latitude'
+        db.add_column(u'organizations_organization', 'latitude',
+                      self.gf('django.db.models.fields.FloatField')(null=True, blank=True),
+                      keep_default=False)
+
+        # Adding field 'Organization.longitude'
+        db.add_column(u'organizations_organization', 'longitude',
+                      self.gf('django.db.models.fields.FloatField')(null=True, blank=True),
+                      keep_default=False)
+
+
+    def backwards(self, orm):
+        # Deleting field 'Organization.description'
+        db.delete_column(u'organizations_organization', 'description')
+
+        # Deleting field 'Organization.latitude'
+        db.delete_column(u'organizations_organization', 'latitude')
+
+        # Deleting field 'Organization.longitude'
+        db.delete_column(u'organizations_organization', 'longitude')
+
+
+    models = {
+        u'organizations.carpooler': {
+            'Meta': {'object_name': 'CarPooler'},
+            'car_description': ('django.db.models.fields.TextField', [], {}),
+            'departure_time': ('django.db.models.fields.DateTimeField', [], {}),
+            'going_to': ('django.db.models.fields.TextField', [], {}),
+            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'leaving_from': ('django.db.models.fields.TextField', [], {}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '254'})
+        },
+        u'organizations.organization': {
+            'Meta': {'object_name': 'Organization'},
+            'description': ('django.db.models.fields.TextField', [], {}),
+            u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'latitude': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),
+            'location': ('django.db.models.fields.TextField', [], {}),
+            'longitude': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '254'})
+        }
+    }
+
+    complete_apps = ['organizations']

File groupGo/apps/organizations/models.py

View file
 from django.db import models
+import requests
+import simplejson as json
+
+GEOCODING_URL = "http://maps.googleapis.com/maps/api/geocode/json"
 
 
 class Organization(models.Model):
     name = models.CharField(max_length=254)
     location = models.TextField()
+    description = models.TextField()
+    latitude = models.FloatField(blank=True, null=True)
+    longitude = models.FloatField(blank=True, null=True)
+
+    def save(self, *args, **kwargs):
+        if not self.latitude and not self.longitude:
+            geocoded_blob = json.loads(requests.get(
+                GEOCODING_URL,
+                params={
+                    "address": self.location,
+                    "sensor": 'false'
+                }
+            ).content)
+
+            location = geocoded_blob['results'][0]['geometry']['location']
+            self.latitude = location['lat']
+            self.longitude = location['lng']
+        super(Organization, self).save(*args, **kwargs)
 
 
 class CarPooler(models.Model):