diff --git a/places/__init__.py b/places/__init__.py index 5e206eb..d3eed0c 100644 --- a/places/__init__.py +++ b/places/__init__.py @@ -11,7 +11,7 @@ from decimal import Decimal default_app_config = 'places.apps.PlacesConfig' -__version__ = '1.1.2' +__version__ = '1.1.3' class Places(object): @@ -28,10 +28,10 @@ def __init__(self, place, latitude, longitude): self.longitude = Decimal(longitude) def __str__(self): - return "{0}, {1}, {2}".format(self.place, self.latitude, self.longitude) + return "%s, %s, %s" % (self.place, self.latitude, self.longitude) def __repr__(self): - return "Places({0})".format(self) + return "Places(%s)" % str(self) def __len__(self): return len(str(self)) diff --git a/places/fields.py b/places/fields.py index 8766f7a..d73163e 100644 --- a/places/fields.py +++ b/places/fields.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -import re from decimal import Decimal from django.db import models @@ -31,21 +30,19 @@ def to_python(self, value): if isinstance(value, list): return Places(value[0], value[1], value[2]) - matched = re.finditer(r'[-+]?\d*\.\d+|\d+', value) - value_parts = [Decimal(x.group()) for x in matched] + value_parts = [Decimal(val) for val in value.split(',')[-2:]] try: - latitude = value_parts[0] if len(value_parts) == 2 else value_parts[2] + latitude = value_parts[0] except IndexError: latitude = '0.0' try: - longitude = value_parts[1] if len(value_parts) == 2 else value_parts[3] + longitude = value_parts[1] except IndexError: longitude = '0.0' - try: - place = re.sub(r'[-+]?\d*\.\d+|\d+', '', value)[:-4] + place = ','.join(value.split(',')[:-2]) except: pass @@ -59,7 +56,7 @@ def get_prep_value(self, value): def value_to_string(self, obj): value = self._get_val_from_obj(obj) - return str(value) + return smart_text(value) def formfield(self, **kwargs): defaults = { diff --git a/places/forms.py b/places/forms.py index 5ded832..110fed5 100644 --- a/places/forms.py +++ b/places/forms.py @@ -13,7 +13,6 @@ class PlacesField(forms.MultiValueField): } def __init__(self, *args, **kwargs): - self.widget = PlacesWidget() fields = ( forms.CharField(label=_('place')), forms.DecimalField(label=_('latitude')), @@ -21,6 +20,7 @@ def __init__(self, *args, **kwargs): ) if 'initial' in kwargs: kwargs['initial'] = Location(*kwargs['initial'].split(',')) + self.widget = PlacesWidget() super(PlacesField, self).__init__(fields, **kwargs) def widget_attrs(self, widget):