Skip to content

Commit b9a8c89

Browse files
authored
Merge pull request #50 from maxmind/greg/is-in-eu
Add is_in_european_union support
2 parents a52a678 + 50f91ed commit b9a8c89

File tree

6 files changed

+67
-13
lines changed

6 files changed

+67
-13
lines changed

HISTORY.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
History
44
-------
55

6+
2.7.0
7+
++++++++++++++++++
8+
9+
- The ``is_in_european_union`` attribute was added to
10+
``geoip2.record.Country`` and ``geoip2.record.RepresentedCountry``. This
11+
attribute is ``True`` if the country is a member state of the European
12+
Union.
13+
14+
615
2.6.0 (2017-10-27)
716
++++++++++++++++++
817

@@ -55,7 +64,7 @@ History
5564
2.2.0 (2015-06-29)
5665
++++++++++++++++++
5766

58-
* The ``geoip2.records.Location` class has been updated to add attributes for
67+
* The ``geoip2.records.Location`` class has been updated to add attributes for
5968
the ``average_income`` and ``population_density`` fields provided by the
6069
Insights web service.
6170
* The ``is_anonymous_proxy`` and ``is_satellite_provider`` properties on

geoip2/records.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ class Country(PlaceRecord):
159159
160160
:type: int
161161
162+
.. attribute:: is_in_european_union
163+
164+
This is true if the country is a member state of the European Union.
165+
166+
:type: bool
167+
162168
.. attribute:: iso_code
163169
164170
The two-character `ISO 3166-1
@@ -183,7 +189,14 @@ class Country(PlaceRecord):
183189
184190
"""
185191

186-
_valid_attributes = set(['confidence', 'geoname_id', 'iso_code', 'names'])
192+
_valid_attributes = set([
193+
'confidence', 'geoname_id', 'is_in_european_union', 'iso_code', 'names'
194+
])
195+
196+
def __init__(self, locales=None, **kwargs):
197+
if 'is_in_european_union' not in kwargs:
198+
kwargs['is_in_european_union'] = False
199+
super(Country, self).__init__(locales, **kwargs)
187200

188201

189202
class RepresentedCountry(Country):
@@ -210,6 +223,12 @@ class RepresentedCountry(Country):
210223
211224
:type: int
212225
226+
.. attribute:: is_in_european_union
227+
228+
This is true if the country is a member state of the European Union.
229+
230+
:type: bool
231+
213232
.. attribute:: iso_code
214233
215234
The two-character `ISO 3166-1
@@ -242,8 +261,10 @@ class RepresentedCountry(Country):
242261
243262
"""
244263

245-
_valid_attributes = set(
246-
['confidence', 'geoname_id', 'iso_code', 'names', 'type'])
264+
_valid_attributes = set([
265+
'confidence', 'geoname_id', 'is_in_european_union', 'iso_code',
266+
'names', 'type'
267+
])
247268

248269

249270
class Location(Record):

tests/data

Submodule data updated 49 files

tests/database_test.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ def test_city(self):
101101

102102
self.assertEqual(record.country.name, 'United Kingdom',
103103
'The default locale is en')
104-
104+
self.assertEqual(record.country.is_in_european_union, True)
105105
self.assertEqual(record.location.accuracy_radius, 100,
106106
'The accuracy_radius is populated')
107+
self.assertEqual(record.registered_country.is_in_european_union, False)
108+
107109
reader.close()
108110

109111
def test_connection_type(self):
@@ -130,6 +132,8 @@ def test_country(self):
130132
record = reader.country('81.2.69.160')
131133
self.assertEqual(record.traits.ip_address, '81.2.69.160',
132134
'IP address is added to model')
135+
self.assertEqual(record.country.is_in_european_union, True)
136+
self.assertEqual(record.registered_country.is_in_european_union, False)
133137
reader.close()
134138

135139
def test_domain(self):
@@ -158,7 +162,10 @@ def test_enterprise(self):
158162
self.assertEqual(record.city.confidence, 11)
159163
self.assertEqual(record.country.confidence, 99)
160164
self.assertEqual(record.country.geoname_id, 6252001)
165+
self.assertEqual(record.country.is_in_european_union, False)
161166
self.assertEqual(record.location.accuracy_radius, 27)
167+
self.assertEqual(record.registered_country.is_in_european_union,
168+
False)
162169
self.assertEqual(record.traits.connection_type, 'Cable/DSL')
163170
self.assertTrue(record.traits.is_legitimate_proxy)
164171
self.assertEqual(record.traits.ip_address, ip_address)

tests/models_test.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def test_insights_full(self):
7979
},
8080
'represented_country': {
8181
'geoname_id': 3,
82+
'is_in_european_union': True,
8283
'iso_code': 'GB',
8384
'names': {
8485
'en': 'United Kingdom'
@@ -173,13 +174,17 @@ def test_insights_full(self):
173174
self.assertEqual(model.location, eval(repr(model.location)),
174175
"Location repr can be eval'd")
175176

176-
self.assertTrue(model.traits.is_anonymous)
177-
self.assertTrue(model.traits.is_anonymous_proxy)
178-
self.assertTrue(model.traits.is_anonymous_vpn)
179-
self.assertTrue(model.traits.is_hosting_provider)
180-
self.assertTrue(model.traits.is_public_proxy)
181-
self.assertTrue(model.traits.is_satellite_provider)
182-
self.assertTrue(model.traits.is_tor_exit_node)
177+
self.assertIs(model.country.is_in_european_union, False)
178+
self.assertIs(model.registered_country.is_in_european_union, False)
179+
self.assertIs(model.represented_country.is_in_european_union, True)
180+
181+
self.assertIs(model.traits.is_anonymous, True)
182+
self.assertIs(model.traits.is_anonymous_proxy, True)
183+
self.assertIs(model.traits.is_anonymous_vpn, True)
184+
self.assertIs(model.traits.is_hosting_provider, True)
185+
self.assertIs(model.traits.is_public_proxy, True)
186+
self.assertIs(model.traits.is_satellite_provider, True)
187+
self.assertIs(model.traits.is_tor_exit_node, True)
183188

184189
def test_insights_min(self):
185190
model = geoip2.models.Insights({'traits': {'ip_address': '5.6.7.8'}})

tests/webservice_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ def setUp(self):
4444
'maxmind': {
4545
'queries_remaining': 11
4646
},
47+
'registered_country': {
48+
'geoname_id': 2,
49+
'is_in_european_union': True,
50+
'iso_code': 'DE',
51+
'names': {
52+
'en': 'Germany'
53+
}
54+
},
4755
'traits': {
4856
'ip_address': '1.2.3.4'
4957
},
@@ -73,6 +81,8 @@ def test_country_ok(self, mock):
7381
'continent name is North America')
7482
self.assertEqual(country.country.geoname_id, 1,
7583
'country geoname_id is 1')
84+
self.assertIs(country.country.is_in_european_union, False,
85+
'country is_in_european_union is False')
7686
self.assertEqual(country.country.iso_code, 'US',
7787
'country iso_code is US')
7888
self.assertEqual(country.country.names,
@@ -81,6 +91,8 @@ def test_country_ok(self, mock):
8191
'country name is United States of America')
8292
self.assertEqual(country.maxmind.queries_remaining, 11,
8393
'queries_remaining is 11')
94+
self.assertIs(country.registered_country.is_in_european_union, True,
95+
'registered_country is_in_european_union is True')
8496
self.assertEqual(country.raw, self.country, 'raw response is correct')
8597

8698
@requests_mock.mock()

0 commit comments

Comments
 (0)