Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django 1.9 support #58

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions geoposition/fields.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
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

from . import Geoposition
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):
Expand All @@ -19,12 +18,13 @@ def __init__(self, *args, **kwargs):
def get_internal_type(self):
return 'CharField'

def to_python(self, value):
def _parse_position(self, value):
if not value or value == 'None':
return None
if isinstance(value, Geoposition):
return value
if isinstance(value, list):
value = list(map(str, value))
return Geoposition(value[0], value[1])

# default case is string
Expand All @@ -40,6 +40,12 @@ def to_python(self, value):

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)

Expand Down
1 change: 0 additions & 1 deletion geoposition/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from .test_geoposition import GeopositionTestCase
20 changes: 19 additions & 1 deletion geoposition/tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
SECRET_KEY = '+*mac$-+vop%1#8zp%)blgacvst%fm)p$&u#p!5(x5nv9c0955'

DEBUG = True
TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

Expand All @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions geoposition/tests/test_geoposition.py
Original file line number Diff line number Diff line change
@@ -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'))
Expand Down
9 changes: 5 additions & 4 deletions geoposition/tests/urls.py
Original file line number Diff line number Diff line change
@@ -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)),
)
]