added logging request city and request country using geoip2
https://github.com/lingster/drf-api-tracking/issues/8
Added request city and country logging using Django's wrapper for the python pacakge geoip2

From Django Geolocation with GeoIP2
Instructions on how to enable location logging
In order to perform IP-based geolocation, the GeoIP2 object requires the GeoIP Country and/or City datasets in binary format (the CSV files will not work!). Grab the GeoLite2-Country.mmdb.gz and GeoLite2-City.mmdb.gz files and unzip them in a directory corresponding to the GEOIP_PATH setting.
Sourcery Code Quality Report
❌ Merging this PR will decrease code quality in the affected files by 0.22%.
| Quality metrics | Before | After | Change |
|---|---|---|---|
| Complexity | 1.68 ⭐ | 1.86 ⭐ | 0.18 👎 |
| Method Length | 37.17 ⭐ | 37.54 ⭐ | 0.37 👎 |
| Working memory | 8.19 🙂 | 8.32 🙂 | 0.13 👎 |
| Quality | 81.41% ⭐ | 81.19% ⭐ | -0.22% 👎 |
| Other metrics | Before | After | Change |
|---|---|---|---|
| Lines | 651 | 694 | 43 |
| Changed files | Quality Before | Quality After | Quality Change |
|---|---|---|---|
| rest_framework_tracking/admin.py | 58.85% 🙂 | 57.74% 🙂 | -1.11% 👎 |
| rest_framework_tracking/base_mixins.py | 66.22% 🙂 | 66.45% 🙂 | 0.23% 👍 |
| rest_framework_tracking/base_models.py | 94.38% ⭐ | 94.38% ⭐ | 0.00% |
| tests/test_mixins.py | 86.39% ⭐ | 86.52% ⭐ | 0.13% 👍 |
Here are some functions in these files that still need a tune-up:
| File | Function | Complexity | Length | Working Memory | Quality | Recommendation |
|---|---|---|---|---|---|---|
| rest_framework_tracking/base_mixins.py | BaseLoggingMixin.finalize_response | 17 🙂 | 204 😞 | 31 ⛔ | 26.79% 😞 | Try splitting into smaller methods. Extract out complex expressions |
| rest_framework_tracking/base_mixins.py | BaseLoggingMixin._clean_data | 17 🙂 | 119 🙂 | 11 😞 | 48.95% 😞 | Extract out complex expressions |
| tests/test_mixins.py | TestLoggingMixin.test_put_user | 0 ⭐ | 54 ⭐ | 15 😞 | 67.97% 🙂 | Extract out complex expressions |
| tests/test_mixins.py | TestLoggingMixin.test_post_user | 0 ⭐ | 50 ⭐ | 13 😞 | 71.29% 🙂 | Extract out complex expressions |
| tests/test_mixins.py | TestLoggingMixin.test_log_exact_match_params_cleaned | 0 ⭐ | 40 ⭐ | 13 😞 | 72.90% 🙂 | Extract out complex expressions |
Legend and Explanation
The emojis denote the absolute quality of the code:
- ⭐ excellent
- 🙂 good
- 😞 poor
- ⛔ very poor
The 👍 and 👎 indicate whether the quality has improved or gotten worse with this pull request.
Please see our documentation here for details on how these metrics are calculated.
We are actively working on this report - lots more documentation and extra metrics to come!
Let us know what you think of it by mentioning @sourcery-ai in a comment.
In order to keep the coverage up, could we also some some unit tests to cover the additional 2 new fields. See the
tests/test_models.pyandtest/test_mixins.py.
I added unit tests to cover the 2 new fields: request_city and request_country. I check that both fields equal to None if the IP address is 127.0.0.9.