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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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