Django app for forbidding access to some countries
Project description
Django Forbid
Django app for forbidding access to some countries.
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 zones to be forbidden or allowed. And there are four setting variables for describing any of your specific needs:
WHITELIST_COUNTRIES
andWHITELIST_TERRITORIES
- Correspondingly, the list of countries and territories that are allowed to access the site.FORBIDDEN_COUNTRIES
andFORBIDDEN_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.
Without additional configuration, the middleware will check the user's access on every request. This can slow down the
site. To avoid this, you can use the FORBID_TIMEOUT
variable to set the cache timeout in seconds. When the timeout
expires, the middleware will check the user's access again.
# Check the user's access every 10 minutes.
FORBID_TIMEOUT = 60 * 10
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_forbid-0.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16f45b71c6f4c57e43b47904b01310de7cf5b83e2d7406e34860255d3ae28f20 |
|
MD5 | 926290cf421acd268829a25a08121624 |
|
BLAKE2b-256 | 862e4080bada0ee2c730bc1bc82a5e49aea5de9028fa22a866f0d05f318345d7 |