Skip to main content

It's easy to use, plug-in django app that once included, stores logins history (with device data, like IP, user-agent, location etc.) of all users

Project description

django-login-history2

GitHub License Python Version Django Version Maintenance

django-login-history2 is an enhanced version of the "django-login-history" package. It provides an easy and effective way to track user login history in a Django project, including information like IP address, user-agent, geolocation, and more.

Features

  • Tracks user login and logout history.
  • Stores device data, such as IP address, user-agent, geolocation, etc.
  • Built-in support for easy customization and extension.
  • Can be easily integrated with your existing Django application.
  • Test mode with dummy geolocation data for testing purposes.

Installation

To install django-login-history2, simply run:

pip install django-login-history2

Then, add 'django_login_history2' to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    # ...
    'django_login_history2',
    # ...
]

Run migrations to create the necessary database tables:

python manage.py migrate

Configuration

This app can be configured through settings.py. The following settings are available:

  • LOGIN_HISTORY_GEOLOCATION_METHOD: Define the method to use for geolocation. (Default: django_login_history2.utils.get_geolocation_data)
  • LOGIN_HISTORY_GEOLOCATION_HELPER_CLASS: Define the class used for geolocation (Default: django_login_history2.helper.IPCheckerIPApi).
  • LOGIN_HISTORY_GEOLOCATION_PLACEHOLDER_IP: Define a placeholder IP for testing mode (Default: 8.8.8.8).
  • LOGIN_HISTORY_GEOLOCATION_CACHE: Specify the cache to use for geolocation data.
  • LOGIN_HISTORY_IP_API_KEY: Define the API Key used by IP API Service class (paid plans)

Usage

Once installed and configured, django-login-history2 will automatically start recording login and logout events for all users.

To access the login history for a user, you can use the login_history related field:

user = User.objects.get(username="myuser")
history = user.login_history.all()

IP Geolocation Provider Extension

This module allows you to extend the functionality of the IPChecker class by adding custom providers to fetch geolocation data for IP addresses. The provided example shows how to implement a new provider by creating a class that extends the IPCheckerAbstract class and implements the get_geolocation_data() method.

How to Create a New Geolocation Provider

To create your own IP geolocation provider, follow these steps:

1. Create a New Class That Inherits from IPCheckerAbstract

Your class should inherit from IPCheckerAbstract to ensure it has all the required properties and methods for retrieving geolocation data for an IP address.

2. Implement the get_geolocation_data() Method

The get_geolocation_data() method should be implemented to fetch the geolocation data either from an external service or a custom logic.

Example: MyIPService Provider

import requests
from django_login_history2.app_settings import get_cache, CACHE_TIMEOUT
from django_login_history2.helper import IPCheckerAbstract, IPInfo
class IPCheckerMyIPService(IPCheckerAbstract):
    def get_geolocation_data(self) -> IPInfo:        
        key = f'myipservice:{self.client_ip}'
        data = get_cache().get(key)

        if not data:
            data = super().get_geolocation_data()
            if not self.is_routable:
                data = data.with_overrides(error=True, reason="Address not routable")
            else:                
                response = requests.get(f'https://myipservice.com/{self.client_ip}/json/', timeout=60)
                geolocation_data = response.json()
                data = data.with_overrides(**geolocation_data)            
            get_cache().set(key, data, timeout=CACHE_TIMEOUT)

        return data

3. Register Your New Provider

To use your custom provider, register it in the appropriate part of your code or configuration. This ensures that your provider is used to fetch the geolocation data when necessary.

Key Concepts

  • Cache: To avoid making repetitive calls to external APIs, the get_cache() function is used to cache geolocation data. This improves performance and reduces external service dependencies.
  • Timeout and Error Handling: External APIs may have slow responses or return errors, so it's important to add error handling and set appropriate timeouts, as shown in the example.
  • Custom Data: The get_geolocation_data() method can return more than just basic geolocation information, such as country, city, coordinates (latitude/longitude), etc. You can also customize it to return an error if the IP address is not routable.

Example Usage

Once you have implemented and registered your custom provider, you can use it in your code like so:

ip_checker = IPCheckerMyIPService(request, user)
geolocation_data = ip_checker.get_geolocation_data()
print(geolocation_data)

Contributing

Feel free to open issues or create pull requests to contribute to the project. If you find a bug or need a feature, open an issue and we will try to fix it as soon as possible.

License This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_login_history2-0.0.9.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

django_login_history2-0.0.9-py3-none-any.whl (14.8 kB view details)

Uploaded Python 3

File details

Details for the file django_login_history2-0.0.9.tar.gz.

File metadata

  • Download URL: django_login_history2-0.0.9.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.21

File hashes

Hashes for django_login_history2-0.0.9.tar.gz
Algorithm Hash digest
SHA256 e896013839bac7cf57af84e5e54b99ad807739a7af022d806edc63b8930f1b75
MD5 cfb9a2067626294791f98ed951b82625
BLAKE2b-256 bf223b52d2bd43442b47939866ac2b35ef1973c3722dd7dd8191cc7375d30d1e

See more details on using hashes here.

File details

Details for the file django_login_history2-0.0.9-py3-none-any.whl.

File metadata

File hashes

Hashes for django_login_history2-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 4e0ced78ace25ee3f9cc069911c1eea68f659f20fe30bc7dcd76da285ab70f00
MD5 0143da38261e5505b28e1cf968073fe9
BLAKE2b-256 9db6631637604d79e986648b5539af70b9515eed25b71958ccfc9bd90405988e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page