Skip to content

Commit

Permalink
Remove regex value parsing
Browse files Browse the repository at this point in the history
Fixes #8
  • Loading branch information
oscarmcm committed May 7, 2017
1 parent ff6a173 commit 365d19b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
6 changes: 3 additions & 3 deletions places/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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))
Expand Down
13 changes: 5 additions & 8 deletions places/fields.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import re
from decimal import Decimal

from django.db import models
Expand Down Expand Up @@ -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

Expand All @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion places/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ class PlacesField(forms.MultiValueField):
}

def __init__(self, *args, **kwargs):
self.widget = PlacesWidget()
fields = (
forms.CharField(label=_('place')),
forms.DecimalField(label=_('latitude')),
forms.DecimalField(label=_('longitude')),
)
if 'initial' in kwargs:
kwargs['initial'] = Location(*kwargs['initial'].split(','))
self.widget = PlacesWidget()
super(PlacesField, self).__init__(fields, **kwargs)

def widget_attrs(self, widget):
Expand Down

0 comments on commit 365d19b

Please sign in to comment.