Skip to content

Commit

Permalink
Merge pull request #6 from vangesseld/master
Browse files Browse the repository at this point in the history
Fix for updates to pycountry and netaddr
  • Loading branch information
rwielk authored May 17, 2021
2 parents 5b07e49 + ca3c03c commit ce42bd3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
24 changes: 14 additions & 10 deletions geofeed_validator/fields/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,24 @@ class CountryField(Field):
NAME = 'country'

def _check_errors(self, value):
try:
self.to_python(value)
except:
return True
if value:
try:
if not self.to_python(value):
return True
except:
return True
return False

def to_python(self, value):
if value:
return pycountry.countries.get(alpha2=value)
return pycountry.countries.get(alpha_2=value)
return None

def to_string(self, value):
if value:
try:
country = self.to_python(value)
return country.alpha2
return country.alpha_2
except:
pass
return ''
Expand All @@ -163,10 +165,12 @@ class SubdivisionField(Field):
NAME = 'subdivision'

def _check_errors(self, value):
try:
self.to_python(value)
except:
return True
if value:
try:
if not self.to_python(value):
return True
except:
return True
return False

def to_python(self, value):
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pycountry==1.4
netaddr==0.7.11
pycountry>=16.11.27.1
netaddr>=0.7.11
6 changes: 3 additions & 3 deletions tests/fields_tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ def test_0004_private(self):
self.field.validate('192.168.0.0/24'))

def test_0005_reserved(self):
self.assertEqual(((NetworkField.ERROR_RESERVED, ), tuple(), netaddr.IPNetwork('192.0.0.0/24')),
self.field.validate('192.0.0.0/24'))
self.assertEqual(((NetworkField.ERROR_RESERVED, ), tuple(), netaddr.IPNetwork('192.0.2.0/24')),
self.field.validate('192.0.2.0/24'))

def test_0006_valid(self):
self.assertEqual((tuple(), tuple(), netaddr.IPNetwork('8.8.8.8')),
Expand All @@ -213,7 +213,7 @@ def test_0001_invalid_alpha2(self):
self.assertEqual(((CountryField.ERROR, ), tuple(), None), self.field.validate('INVALID'))

def test_0002_valid_alpha2(self):
self.assertEqual((tuple(), tuple(), pycountry.countries.get(alpha2='AT')), self.field.validate('AT'))
self.assertEqual((tuple(), tuple(), pycountry.countries.get(alpha_2='AT')), self.field.validate('AT'))

def test_0004_to_string_valid(self):
self.assertEqual('AT', self.field.to_string('AT'))
Expand Down

0 comments on commit ce42bd3

Please sign in to comment.