From 192fe2d974c3fa902fc83954f37af87f6106ea81 Mon Sep 17 00:00:00 2001 From: Fred Pauchet Date: Wed, 18 Jul 2012 16:43:58 +0200 Subject: [PATCH] OpenStreetMap Integration --- cards/models.py | 4 +++ osm/__init__.py | 0 osm/admin.py | 4 +++ osm/fixtures/coordinates.json | 1 + osm/models.py | 20 ++++++++++++++ osm/templatetags/__init__.py | 0 osm/templatetags/osm_tags.py | 13 +++++++++ osm/tests.py | 16 +++++++++++ osm/views.py | 21 +++++++++++++++ settings.py | 1 + templates/osm/map.html | 51 +++++++++++++++++++++++++++++++++++ urls.py | 1 + 12 files changed, 132 insertions(+) create mode 100644 osm/__init__.py create mode 100644 osm/admin.py create mode 100644 osm/fixtures/coordinates.json create mode 100644 osm/models.py create mode 100644 osm/templatetags/__init__.py create mode 100644 osm/templatetags/osm_tags.py create mode 100644 osm/tests.py create mode 100644 osm/views.py create mode 100644 templates/osm/map.html diff --git a/cards/models.py b/cards/models.py index aafe836..e79d57d 100644 --- a/cards/models.py +++ b/cards/models.py @@ -10,6 +10,10 @@ class Country(models.Model): class Meta: verbose_name_plural ='Pays' verbose_name = 'Pays' + ordering = ['label'] + + def __str__(self): + return self.label def __unicode__(self): return self.label diff --git a/osm/__init__.py b/osm/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/osm/admin.py b/osm/admin.py new file mode 100644 index 0000000..5054d01 --- /dev/null +++ b/osm/admin.py @@ -0,0 +1,4 @@ +from osm.models import * +from django.contrib import admin + +admin.site.register(Coordinates) \ No newline at end of file diff --git a/osm/fixtures/coordinates.json b/osm/fixtures/coordinates.json new file mode 100644 index 0000000..12b58e0 --- /dev/null +++ b/osm/fixtures/coordinates.json @@ -0,0 +1 @@ +[{"pk": 1, "model": "osm.coordinates", "fields": {"latitude": "46.6", "country": 1, "longitude": "1.89"}}, {"pk": 2, "model": "osm.coordinates", "fields": {"latitude": "54.7024", "country": 2, "longitude": "-3.2766"}}, {"pk": 3, "model": "osm.coordinates", "fields": {"latitude": "50.5996", "country": 4, "longitude": "4.4097"}}, {"pk": 4, "model": "osm.coordinates", "fields": {"latitude": "14.475", "country": 5, "longitude": "-14.4529"}}] \ No newline at end of file diff --git a/osm/models.py b/osm/models.py new file mode 100644 index 0000000..9f67f24 --- /dev/null +++ b/osm/models.py @@ -0,0 +1,20 @@ +#coding=utf-8 + +from django.db import models +from xcards.cards.models import Country + +# Create your models here. +class Coordinates(models.Model): + """ + Représentation des coordonnées pour un pays au format DD (Degré Décimal) eg. (49.5000 - 123.5000) + """ + latitude = models.DecimalField(max_digits=7, decimal_places=4, verbose_name='Latitude') + longitude = models.DecimalField(max_digits=8, decimal_places=4, verbose_name='Longitude') + country = models.OneToOneField(Country, verbose_name='Pays') + + def __unicode__(self): + return '%s - Lat: %s Long: %s )' % (self.country, str(self.latitude), str(self.longitude)) + + class Meta: + verbose_name_plural = 'Coordonnées' + verbose_name = 'Coordonnées' diff --git a/osm/templatetags/__init__.py b/osm/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/osm/templatetags/osm_tags.py b/osm/templatetags/osm_tags.py new file mode 100644 index 0000000..70d2699 --- /dev/null +++ b/osm/templatetags/osm_tags.py @@ -0,0 +1,13 @@ +from django import template +from django.conf import settings +from django.utils.numberformat import format +from osm.models import Coordinates + +register = template.Library() + +def floatdot(value, separator=".", decimal_pos=4): + return format(value, separator, decimal_pos) + +floatdot.is_safe = True + +register.filter(floatdot) \ No newline at end of file diff --git a/osm/tests.py b/osm/tests.py new file mode 100644 index 0000000..501deb7 --- /dev/null +++ b/osm/tests.py @@ -0,0 +1,16 @@ +""" +This file demonstrates writing tests using the unittest module. These will pass +when you run "manage.py test". + +Replace this with more appropriate tests for your application. +""" + +from django.test import TestCase + + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.assertEqual(1 + 1, 2) diff --git a/osm/views.py b/osm/views.py new file mode 100644 index 0000000..65a7ff4 --- /dev/null +++ b/osm/views.py @@ -0,0 +1,21 @@ +# Create your views here. + +#coding=utf-8 + +from django.shortcuts import render_to_response, get_object_or_404 +from django.http import HttpResponseRedirect, HttpResponse, Http404 +from django.core.urlresolvers import reverse +from django.template import RequestContext +from django import forms +from cards.models import Card, Country, Category, SubCategory +from django.core.context_processors import csrf +from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger + +from osm.models import Coordinates + +def map(request): + coordinates = Coordinates.objects.all() + + context = { 'coordinates' : coordinates } + + return render_to_response('osm/map.html', RequestContext(request, context)) diff --git a/settings.py b/settings.py index 0509ad0..f5af854 100644 --- a/settings.py +++ b/settings.py @@ -128,6 +128,7 @@ INSTALLED_APPS = ( 'django.contrib.admin', 'django.contrib.admindocs', 'cards', + 'osm', ) # A sample logging configuration. The only tangible logging diff --git a/templates/osm/map.html b/templates/osm/map.html new file mode 100644 index 0000000..460fef4 --- /dev/null +++ b/templates/osm/map.html @@ -0,0 +1,51 @@ +{% load osm_tags %} + + + +
+ + + + \ No newline at end of file diff --git a/urls.py b/urls.py index 2e532ae..3bf5bf0 100644 --- a/urls.py +++ b/urls.py @@ -16,6 +16,7 @@ urlpatterns = patterns('', url(r'^list/country/(?P\d+)/$', 'cards.views.search_by_country', name='list-by-country'), url(r'^list/category/(?P\d+)/$', 'cards.views.search_by_category', name='list-by-category'), url(r'^list/subcategory/(?P\d+)/$', 'cards.views.search_by_subcategory', name='list-by-subcategory'), + url(r'^map/$', 'osm.views.map', name='map'), url(r'^admin/doc/', include('django.contrib.admindocs.urls')),