Skip to content

Commit 4addb17

Browse files
committed
Convert location and location_info to property
1 parent 9117272 commit 4addb17

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Backward-incompatible changes:
1515
or you can write a custom version of
1616
[`SetRemoteAddrFromForwardedFor` middleware](https://github.com/django/django/blob/91f18400cc0fb37659e2dbaab5484ff2081f1f30/django/middleware/http.py#L33)
1717
which suits your environment.
18+
- `session.location` and `session.location_info` are now properties
19+
instead of methods.
1820
- `session.device` is now a property instead of a method and returns string.
1921
- The device object can be accessed using the new property `session.device_info`.
2022
- User agent parsing is now done using `ua-parser` instead of `user-agents`.

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,14 @@ UserAgent(family='Chrome Mobile', major='118', minor='0', patch='0', patch_minor
191191
```
192192
193193
194-
And if you have configured GeoIP2, you can get location info using `.location()`
195-
and `.location_info()`:
194+
And if you have configured GeoIP2,
195+
you can get location info using `.location` and `.location_info`:
196196
197197
```python
198-
>>> session.location()
198+
>>> session.location
199199
'Tehran, Iran'
200200
201-
>>> session.location_info()
201+
>>> session.location_info
202202
{'city': 'Tehran', 'continent_code': 'AS', 'continent_name': 'Asia', 'country_code': 'IR', 'country_name': 'Iran', 'time_zone': 'Asia/Tehran', ...}
203203
```
204204

qsessions/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@ def get_search_fields(self, request):
8585
)
8686

8787
@admin.display(description=_("Is valid"), boolean=True)
88-
def is_valid(self, obj):
88+
def is_valid(self, obj: Session):
8989
return obj.expire_date > now()
9090

9191
@admin.display(description=_("Session data"))
92-
def session_data_decoded(self, obj):
92+
def session_data_decoded(self, obj: Session):
9393
return format_html(
9494
'<pre style="white-space: pre-wrap; max-width: 800px; display: inline-block; direction: ltr;">{}</pre>',
9595
pformat(obj.get_decoded()),

qsessions/models.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,14 @@ def delete(self, *args, **kwargs):
6565
caches[settings.SESSION_CACHE_ALIAS].delete(prefix + self.session_key)
6666
return super().delete(*args, **kwargs)
6767

68-
def location(self):
69-
return geoip.ip_to_location(self.ip)
70-
71-
def location_info(self):
68+
@cached_property
69+
def location_info(self) -> dict:
7270
return geoip.ip_to_location_info(self.ip)
7371

72+
@cached_property
73+
def location(self) -> str:
74+
return geoip.ip_to_location(self.ip)
75+
7476
@cached_property
7577
def device_info(self):
7678
"""

tests/test_model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def test_very_long_ua(SessionStore):
4242

4343
def test_location():
4444
session = Session(ip="89.160.20.112")
45-
assert session.location() == "Linköping, Sweden"
46-
loc_info = session.location_info()
45+
assert session.location == "Linköping, Sweden"
46+
loc_info = session.location_info
4747
assert loc_info["city"] == "Linköping"
4848
assert loc_info["country_code"] == "SE"
4949

0 commit comments

Comments
 (0)