From 2871a065a211a1665523da908e35cbe2dc318a01 Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Wed, 2 Dec 2015 23:52:55 +0200 Subject: [PATCH 1/6] Cleanup unit tests, __init__.py and use TestCase --- geoposition/tests/__init__.py | 1 - geoposition/tests/test_geoposition.py | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/geoposition/tests/__init__.py b/geoposition/tests/__init__.py index 5318414..e69de29 100644 --- a/geoposition/tests/__init__.py +++ b/geoposition/tests/__init__.py @@ -1 +0,0 @@ -from .test_geoposition import GeopositionTestCase diff --git a/geoposition/tests/test_geoposition.py b/geoposition/tests/test_geoposition.py index dab469c..340407d 100644 --- a/geoposition/tests/test_geoposition.py +++ b/geoposition/tests/test_geoposition.py @@ -1,10 +1,10 @@ from decimal import Decimal -from django.test import SimpleTestCase +from django.test import TestCase from geoposition import Geoposition from example.models import PointOfInterest -class GeopositionTestCase(SimpleTestCase): +class GeopositionTestCase(TestCase): def test_init_with_decimals(self): gp = Geoposition(Decimal('52.5'), Decimal('13.4')) self.assertEqual(gp.latitude, Decimal('52.5')) From ca6775c7e1c8526cd9c3a11d9106f7587dd3e4ac Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Wed, 2 Dec 2015 23:54:50 +0200 Subject: [PATCH 2/6] Drop deprecated SubfieldBase and rename from_db_value() --- geoposition/fields.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/geoposition/fields.py b/geoposition/fields.py index 6e8209c..bf2153c 100644 --- a/geoposition/fields.py +++ b/geoposition/fields.py @@ -1,7 +1,6 @@ from __future__ import unicode_literals from django.db import models -from django.utils.six import with_metaclass from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import smart_text @@ -9,7 +8,7 @@ from .forms import GeopositionField as GeopositionFormField -class GeopositionField(with_metaclass(models.SubfieldBase, models.Field)): +class GeopositionField(models.Field): description = _("A geoposition (latitude and longitude)") def __init__(self, *args, **kwargs): @@ -19,7 +18,7 @@ def __init__(self, *args, **kwargs): def get_internal_type(self): return 'CharField' - def to_python(self, value): + def from_db_value(self, value, expression, connection, context): if not value or value == 'None': return None if isinstance(value, Geoposition): From a98465d08c9840b421c67416478a6cebb428697b Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Thu, 3 Dec 2015 00:07:10 +0200 Subject: [PATCH 3/6] Cleanup URL conf and TEMPLATES settings --- geoposition/tests/settings.py | 20 +++++++++++++++++++- geoposition/tests/urls.py | 9 +++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/geoposition/tests/settings.py b/geoposition/tests/settings.py index 09e08cf..4f3b594 100644 --- a/geoposition/tests/settings.py +++ b/geoposition/tests/settings.py @@ -4,7 +4,6 @@ SECRET_KEY = '+*mac$-+vop%1#8zp%)blgacvst%fm)p$&u#p!5(x5nv9c0955' DEBUG = True -TEMPLATE_DEBUG = True ALLOWED_HOSTS = [] @@ -19,6 +18,25 @@ 'example', ) + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [ + os.path.join(BASE_DIR, 'templates') + ], + 'DEBUG': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', diff --git a/geoposition/tests/urls.py b/geoposition/tests/urls.py index e4eeac2..ab71c38 100644 --- a/geoposition/tests/urls.py +++ b/geoposition/tests/urls.py @@ -1,9 +1,10 @@ -from django.conf.urls import patterns, include, url +from django.conf.urls import include, url from django.contrib import admin +from example.views import poi_list admin.autodiscover() -urlpatterns = patterns('', - url(r'^$', 'example.views.poi_list'), +urlpatterns = [ + url(r'^$', poi_list), url(r'^admin/', include(admin.site.urls)), -) +] From 28e6a4fc7d3aaad72c7632a848e634cdf49b61c0 Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Fri, 4 Dec 2015 00:02:05 +0200 Subject: [PATCH 4/6] Explicitly cast Decimals to strs if value is a list --- geoposition/fields.py | 1 + 1 file changed, 1 insertion(+) diff --git a/geoposition/fields.py b/geoposition/fields.py index bf2153c..e3b61a0 100644 --- a/geoposition/fields.py +++ b/geoposition/fields.py @@ -24,6 +24,7 @@ def from_db_value(self, value, expression, connection, context): if isinstance(value, Geoposition): return value if isinstance(value, list): + value = map(str, value) return Geoposition(value[0], value[1]) # default case is string From c84db6f4f656552a6d1ee1afb42ee366329a2763 Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Fri, 4 Dec 2015 00:06:10 +0200 Subject: [PATCH 5/6] Extract _parse_position() and add to_python() --- geoposition/fields.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/geoposition/fields.py b/geoposition/fields.py index e3b61a0..a10c325 100644 --- a/geoposition/fields.py +++ b/geoposition/fields.py @@ -18,7 +18,7 @@ def __init__(self, *args, **kwargs): def get_internal_type(self): return 'CharField' - def from_db_value(self, value, expression, connection, context): + def _parse_position(self, value): if not value or value == 'None': return None if isinstance(value, Geoposition): @@ -40,6 +40,12 @@ def from_db_value(self, value, expression, connection, context): return Geoposition(latitude, longitude) + def from_db_value(self, value, expression, connection, context): + return self._parse_position(value) + + def to_python(self, value): + return self._parse_position(value) + def get_prep_value(self, value): return str(value) From ab2076aa7478030e8c4a53bec69e14e2da6c1f14 Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Fri, 4 Dec 2015 00:07:08 +0200 Subject: [PATCH 6/6] Wrap _parse_position list result in list() --- geoposition/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/geoposition/fields.py b/geoposition/fields.py index a10c325..6ecc3b5 100644 --- a/geoposition/fields.py +++ b/geoposition/fields.py @@ -24,7 +24,7 @@ def _parse_position(self, value): if isinstance(value, Geoposition): return value if isinstance(value, list): - value = map(str, value) + value = list(map(str, value)) return Geoposition(value[0], value[1]) # default case is string