Skip to main content

Django app for forbidding access to some countries

Project description

Django Forbid

Django app for forbidding access to some countries.

PyPI Django Python License Tests

Install

python -m pip install django-forbid

Configuration

Add the django_forbid.apps.ForbidConfig to your INSTALLED_APPS in your Django project's settings.py file.

INSTALLED_APPS = [
    ...,  # other apps
    'django_forbid.apps.ForbidConfig',
]

Also, add the django_forbid.middleware.ForbidMiddleware to the MIDDLEWARE list of the project.

MIDDLEWARE = [
    ...,  # other middlewares
    'django_forbid.middleware.ForbidMiddleware',
]

Configuring the GEOIP_PATH variable in your project's settings is important. This variable should contain the path to the GeoLite2 database file. You should download the database and follow the Django documentation for proper configuration.

Usage

After connecting the Django Forbid to your project, you can define the set of desired countries or territories to be forbidden. And there are four setting variables for describing any of your specific needs:

  • WHITELIST_COUNTRIES and WHITELIST_TERRITORIES - Correspondingly, the list of countries and territories that are allowed to access the site.
  • FORBIDDEN_COUNTRIES and FORBIDDEN_TERRITORIES - Correspondingly, the list of countries and territories that are forbidden to access the site.

Forbidden countries and territories have a higher priority than allowed ones. If a country or territory is in both lists, then the user will be forbidden. And if the user is not allowed to access the resource, it will be redirected to the FORBIDDEN_URL page if the variable is set in your Django project's settings.

# Only US, GB, and EU countries are allowed to access the site.
WHITELIST_COUNTRIES = ['US', 'GB']
WHITELIST_TERRITORIES = ['EU']

Needs can be different, so you can use any combination of these variables to describe your special needs.

# Forbid access for African countries and Russia, Belarus, and North Korea.
FORBIDDEN_COUNTRIES = ['RU', 'BY', 'KP']
FORBIDDEN_TERRITORIES = ['AF']

The available ISO 3166 alpha-2 country codes are listed in here. And the available ISO continent codes are: AF - Africa, AN - Antarctica, AS - Asia, EU - Europe, NA - North America, OC - Oceania and SA - South America.

Contribute

Any contribution is welcome. If you have any ideas or suggestions, feel free to open an issue or a pull request. And don't forget to add tests for your changes.

License

Copyright (C) 2023 Artyom Vancyan. MIT

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-forbid-0.0.2.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

django_forbid-0.0.2-py3-none-any.whl (5.3 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