Commits

powersurge360  committed 481fc35

Added and pins.

  • Participants
  • Parent commits c3428fb

Comments (0)

Files changed (11)

File groupGo/apps/organizations/migrations/0004_auto__add_field_carpooler_departure_latitude__add_field_carpooler_depa.py

+# -*- 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 'CarPooler.departure_latitude'
+        db.add_column(u'organizations_carpooler', 'departure_latitude',
+                      self.gf('django.db.models.fields.FloatField')(null=True, blank=True),
+                      keep_default=False)
+
+        # Adding field 'CarPooler.departure_longitude'
+        db.add_column(u'organizations_carpooler', 'departure_longitude',
+                      self.gf('django.db.models.fields.FloatField')(null=True, blank=True),
+                      keep_default=False)
+
+        # Adding field 'CarPooler.arrival_latitude'
+        db.add_column(u'organizations_carpooler', 'arrival_latitude',
+                      self.gf('django.db.models.fields.FloatField')(null=True, blank=True),
+                      keep_default=False)
+
+        # Adding field 'CarPooler.arrival_longitude'
+        db.add_column(u'organizations_carpooler', 'arrival_longitude',
+                      self.gf('django.db.models.fields.FloatField')(null=True, blank=True),
+                      keep_default=False)
+
+
+    def backwards(self, orm):
+        # Deleting field 'CarPooler.departure_latitude'
+        db.delete_column(u'organizations_carpooler', 'departure_latitude')
+
+        # Deleting field 'CarPooler.departure_longitude'
+        db.delete_column(u'organizations_carpooler', 'departure_longitude')
+
+        # Deleting field 'CarPooler.arrival_latitude'
+        db.delete_column(u'organizations_carpooler', 'arrival_latitude')
+
+        # Deleting field 'CarPooler.arrival_longitude'
+        db.delete_column(u'organizations_carpooler', 'arrival_longitude')
+
+
+    models = {
+        u'organizations.carpooler': {
+            'Meta': {'object_name': 'CarPooler'},
+            'arrival_latitude': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),
+            'arrival_longitude': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),
+            'car_description': ('django.db.models.fields.TextField', [], {}),
+            'departure_latitude': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),
+            'departure_longitude': ('django.db.models.fields.FloatField', [], {'null': 'True', 'blank': 'True'}),
+            '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'}),
+            'organization': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'carpoolers'", 'to': u"orm['organizations.Organization']"})
+        },
+        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

     leaving_from = models.TextField()
     going_to = models.TextField()
     organization = models.ForeignKey(Organization, related_name="carpoolers")
+    departure_latitude = models.FloatField(blank=True, null=True)
+    departure_longitude = models.FloatField(blank=True, null=True)
+    arrival_latitude = models.FloatField(blank=True, null=True)
+    arrival_longitude = models.FloatField(blank=True, null=True)
+
+    def save(self, *args, **kwargs):
+        if not self.arrival_latitude and not self.arrival_longitude:
+            geocoded_blob = json.loads(requests.get(
+                GEOCODING_URL,
+                params={
+                    "address": self.going_to,
+                    "sensor": 'false'
+                }
+            ).content)
+
+            location = geocoded_blob['results'][0]['geometry']['location']
+            self.arrival_latitude = location['lat']
+            self.arrival_longitude = location['lng']
+
+        if not self.departure_latitude and not self.departure_longitude:
+            geocoded_blob = json.loads(requests.get(
+                GEOCODING_URL,
+                params={
+                    "address": self.leaving_from,
+                    "sensor": 'false'
+                }
+            ).content)
+
+            location = geocoded_blob['results'][0]['geometry']['location']
+            self.departure_latitude = location['lat']
+            self.departure_longitude = location['lng']
+        super(CarPooler, self).save(*args, **kwargs)

File groupGo/apps/organizations/serializers.py

 
 
 class OrganizationSerializer(serializers.ModelSerializer):
+    carpoolers = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
+
     class Meta:
         model = Organization
 

File groupGo/apps/organizations/urls.py

 from django.conf.urls import patterns, url
 from apps.organizations.views import (
     MapView, AddOrg, OrganizationListAPIView, OrganizationDetailAPIView,
-    CarPoolerListAPIView, CarPoolerDetailAPIView,
+    CarPoolerListAPIView, CarPoolerDetailAPIView, OrganizationCarPoolerAPIView
 )
 
 
         name='api-organizations-detail',
     ),
     url(
+        r'^api/organizations/(?P<pk>\d+)/carpoolers$',
+        OrganizationCarPoolerAPIView.as_view(),
+        name='api-organization-carpooler-detail',
+    ),
+    url(
         r'^api/carpoolers$',
         CarPoolerListAPIView.as_view(),
         name='api-carpoolers',

File groupGo/apps/organizations/views.py

     template_name = "organizations/map.html"
 
 class AddOrg(TemplateView):
-	template_name = "organizations/organization.html"
+    template_name = "organizations/organization.html"
 
 
 class OrganizationListAPIView(generics.ListCreateAPIView):
 class CarPoolerDetailAPIView(generics.RetrieveUpdateDestroyAPIView):
     model = CarPooler
     serializer_class = CarPoolerSerializer
+
+
+class OrganizationCarPoolerAPIView(generics.ListAPIView):
+    serializer_class = CarPoolerSerializer
+
+    def get_queryset(self):
+        return Organization.objects.get(pk=self.kwargs['pk']).carpoolers.all()

File groupGo/staticfiles/css/groupgo.css

 	text-align: center;
 }
 
+.google-dialog {
+    color: black;
+}

File groupGo/staticfiles/js/app/models/carpooler.js

     var Carpooler = Backbone.Model.extend();
 
     var CarpoolerCollection = Backbone.Collection.extend({
-        "url": "/organizations/api/carpoolers",
-        "model": Carpooler
+        "model": Carpooler,
+
+        "url":  function() {
+            return "/organizations/api/organizations/"+this.orgID+"/carpoolers";
+        }
     });
 
     return {

File groupGo/staticfiles/js/app/router.js

         "jquery",
         "underscore",
         "app/models/organization",
-        "app/models/carpooler"
-    ], function(gmaps, Backbone, $, _, organization, carpool) {
+        "app/models/carpooler",
+        "app/views"
+    ], function(gmaps, Backbone, $, _, organization, carpool, views) {
     "use strict";
 
     var MapRouter = Backbone.Router.extend({
         "mapIsSetup": false,
         "organizationPins": [],
-        "carPoolPins": [],
+        "carPoolArrivingPins": [],
+        "carPoolDepartingPins": [],
         "organizationCollection": new organization.OrganizationCollection(),
-        "carpoolCollection": new carpool.CarpoolerCollection(),
 
         "routes": {
             "": "index",
             "index": "index",
             "organization/:id": "organization",
-            "car/:id": "car"
+            "car/arriving/:id": "car_arriving",
+            "car/departing/:id": "car_departing"
         },
 
         // Route Handlers
             this.changeHandlers();
         },
 
-        "car": function(id) {
+        "car_arriving": function(id) {
+            this.changeHandlers();
+
+            var collection = new carpool.CarpoolerCollection(),
+                self = this
+            ;
+            collection.orgID = id;
+            collection.fetch().success(function(elements) {
+                self.createCarpoolerPins(collection);
+            });
+        },
+
+        "car_departing": function(id) {
             this.changeHandlers();
         },
 
         "changeHandlers": function() {
             if (this.organizationPins.length > 0) {
                 _.each(this.organizationPins, function(pin) {
-                    pin.setMap(null);
+                    pin.destroy();
                 });
 
                 this.organizationPins = [];
         "createOrganizationPins": function() {
             var self = this;
             this.organizationCollection.each(function(element) {
-                if (element.get("latitude") && element.get("longitude")) {
-                    var pin = new gmaps.Marker({
-                        "position": new gmaps.LatLng(element.get("latitude"), element.get("longitude")),
-                        "map": self.map
-                    });
+                var pin = new views.OrganizationPin({
+                    "model": element
+                });
 
+                if (pin.assignMap(self.map).render() === true) {
                     self.organizationPins.push(pin);
                 }
             });
         },
 
+        "createCarpoolerPins": function(collection) {
+            var self = this;
+            collection.each(function(element) {
+                var pin = new views.CarpoolerArrivingPin({
+                    "model": element
+                });
+
+                if (pin.assignMap(self.map).render() === true) {
+                    self.carPoolPins.push(pin);
+                }
+            });
+        },
+
         "setUpMap": function() {
             if (this.mapIsSetup === true) {
                 return;
             this.mapIsSetup = true;
 
             var mapOptions = {
-                "zoom": 8,
+                "zoom": 10,
                 "center": new gmaps.LatLng(38.217953, -85.748291),
                 "mapTypeId": gmaps.MapTypeId.ROADMAP,
                 "zoomControl": false,

File groupGo/staticfiles/js/app/templates/carpool_pin.html

Empty file added.

File groupGo/staticfiles/js/app/templates/organization_pin.html

+<div class="google-dialog">
+    <a href="#car/arriving/1">Cars arriving</a>
+    <a href="#car/departing/2">Cars leaving</a>
+</div>

File groupGo/staticfiles/js/app/views.js

+define([
+    "app/vendor/gmaps",
+    "backbone",
+    "text!app/templates/organization_pin.html"
+], function(gmaps, Backbone, template) {
+    "use strict";
+
+    var OrganizationPin = Backbone.View.extend({
+        "assignMap": function(map) {
+            this.map = map;
+
+            return this;
+        },
+
+        "render": function() {
+            if (this.model.get("latitude") && this.model.get("longitude")) {
+                this.pin = new gmaps.Marker({
+                    "position": new gmaps.LatLng(this.model.get("latitude"), this.model.get("longitude")),
+                    "map": this.map
+                });
+
+                var infoWindow = new gmaps.InfoWindow({
+                    "content": template
+                });
+
+                var self = this;
+                gmaps.event.addListener(this.pin, "click", function() {
+                    infoWindow.open(self.map, self.pin);
+                });
+                return true;
+            }
+
+            return false;
+        },
+
+        "destroy": function() {
+            this.pin.setMap(null);
+        }
+    });
+
+    var CarpoolerArrivingPin = Backbone.View.extend({
+        "assignMap": function(map) {
+            this.map = map;
+
+            return this;
+        },
+
+        "render": function() {
+            if (this.model.get("arrival_latitude") && this.model.get("arrival_longitude")) {
+                this.pin = new gmaps.Marker({
+                    "position": new gmaps.LatLng(this.model.get("departure_latitude"), this.model.get("departure_longitude")),
+                    "map": this.map
+                });
+
+                var infoWindow = new gmaps.InfoWindow({
+                    "content": template
+                });
+
+                var self = this;
+                gmaps.event.addListener(this.pin, "click", function() {
+                    infoWindow.open(self.map, self.pin);
+                });
+                return true;
+            }
+
+            return false;
+        },
+
+        "destroy": function() {
+            this.pin.setMap(null);
+        }
+    });
+
+    return {
+        "OrganizationPin": OrganizationPin,
+        "CarpoolerArrivingPin": CarpoolerArrivingPin
+    };
+});