Skip to main content

Django Nigerians Only is a third application that allows developers to restrict access to their applications to only Nigerian users.

Project description

Django Nigerians Only

Django Nigerians Only is a third-party Django application that restricts access to Django applications to only Nigerian users. It can be extended to other countries with a few simple steps.

Requirements

  • Django >= 4.1
  • geoip2 >= 4.8.0

Installation

  1. Install Nigerian Only using pip:

    pip install django-nigerians-only
    
  2. Add 'nigerian_only' to INSTALLED_APPS in your Django project's settings.py:

    INSTALLED_APPS = [
        ...
        'nigerian_only',
    ]
    
  3. Add the middleware to MIDDLEWARE in settings.py:

    MIDDLEWARE = [
        ...
        'nigerian_only.middleware.NigeriansOnlyMiddleware',
    ]
    
  4. Whitelist allowed countries by setting the list of allowed countries using ISO Alpha-2 country codes. Find Country Codes Alpha-2 & Alpha-3 here.

    WHITELISTED_COUNTRIES = ["NG"]  # Example: "NG" for Nigeria
    
  5. Download the GeoIP2 database from MaxMind and set the path in settings.py. You can read more on setting up GeoIP for Django here.

    GEOIP_PATH = "path/to/GeoLite2-Country.mmdb"
    

Usage

Once the above steps are completed, the middleware will restrict access to users from the whitelisted countries.

  • Example Usage with Middleware: The middleware automatically restricts access for all views.

  • Restrict Access Per View: You can also restrict access on a per-view basis using the whitelisted_country_only decorator:

    from django.http import HttpResponse
    from nigerian_only.decorators import whitelisted_country_only
    
    @whitelisted_country_only
    def restricted_view(request):
        return HttpResponse("This is a restricted view to only whitelisted countries.")
    

Important Notes

  • Access will not be restricted if any of the steps mentioned above is not completed correctly.

  • Development Environment: During development, the default IP address is 127.0.0.1, which cannot determine the user's country. You need to set WHITELISTED_IPS in settings.py to allow access during development:

    WHITELISTED_IPS = ['127.0.0.1']
    
  • Testing: To test the middleware, use a VPN to change your location to one of the specified countries or use a valid IP address to determine the user's country.

Example Project

You can check out this app with a basic setup example of the package here: Django Nigeria Only Example.

Contributing

Contributions are welcome and appreciated! Follow these steps to contribute:

  1. Fork the repository.
  2. Create a new branch for your feature or bug fix.
  3. Make changes and write tests to ensure your changes don't break anything.
  4. Push the changes to your fork.
  5. Submit a pull request.

License

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

Developed by Afeez Lawal

Contact Me:


Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

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

Source Distribution

django_nigerians_only-1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

django_nigerians_only-1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file django_nigerians_only-1.0.tar.gz.

File metadata

  • Download URL: django_nigerians_only-1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.10

File hashes

Hashes for django_nigerians_only-1.0.tar.gz
Algorithm Hash digest
SHA256 2ea59683ab85965c44b3e49f7e248adde85cc9ea91eccca5da74142129072a13
MD5 64054876a25d900cd4da1f568b955587
BLAKE2b-256 11a5f4782a835e525511c4866b8c44599a1d63c5175e9a0d71157573dfb363ff

See more details on using hashes here.

File details

Details for the file django_nigerians_only-1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_nigerians_only-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 06f3aae37e23fd83c2695fa7d4fb986436bc1becf1079e37b75839e4a58f8491
MD5 978002a246c19aaa7205a29294db1724
BLAKE2b-256 80e47c80cd382c438cb969709d34b6d07b6044ebcc96ca8cda5eac37ecd645ee

See more details on using hashes here.

Supported by

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