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

to_python coverage extended #79

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
50 changes: 40 additions & 10 deletions geoposition/fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import unicode_literals
from decimal import Decimal
import ast

from django.db import models
from django.utils import six
from django.utils.six import with_metaclass
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import smart_text
Expand All @@ -27,18 +30,44 @@ def to_python(self, value):
if isinstance(value, list):
return Geoposition(value[0], value[1])

# default case is string
value_parts = value.rsplit(',')
try:
latitude = value_parts[0]
except IndexError:
latitude = '0.0'
try:
longitude = value_parts[1]
except IndexError:
longitude = '0.0'
dict_value = ast.literal_eval(value)
if isinstance(dict_value, dict):

if 'lat' in dict_value:
latitude = dict_value['lat']
elif 'latitude' in dict_value:
latitude = dict_value['latitude']
else:
latitude = '0.0'

if 'lng' in dict_value:
longitude = dict_value['lng']
elif 'longitude' in dict_value:
longitude = dict_value['longitude']
else:
longitude = '0.0'

return Geoposition(latitude, longitude)
except:

if isinstance(value, six.text_type):

return Geoposition(latitude, longitude)
# default case is string
value_parts = value.rsplit(',')

try:
latitude = Decimal(value_parts[0])
except:
latitude = '0.0'
try:
longitude = Decimal(value_parts[1])
except:
longitude = '0.0'

return Geoposition(latitude, longitude)

return Geoposition('0.0', '0.0')

def from_db_value(self, value, expression, connection, context):
return self.to_python(value)
Expand All @@ -56,3 +85,4 @@ def formfield(self, **kwargs):
}
defaults.update(kwargs)
return super(GeopositionField, self).formfield(**defaults)