A Django Package for handling Mexico's postal codes
Project description
A Django Package for getting and handling the Mexican Postal Service (Correos de Mexico) postal codes information.
Documentation
The full documentation is at https://django-postalcodes-mexico.readthedocs.io.
Quickstart
If you want to use docker compose scroll down to quickstart using docker compose
Install django-postalcodes-mexico:
pip install django-postalcodes-mexico
Add it to your INSTALLED_APPS:
INSTALLED_APPS = (
#...
'django_postalcodes_mexico.apps.DjangoPostalcodesMexicoConfig',
#...
)
Don’t forget to run migrate:
python manage.py migrate
If the installation was succesful a new command named importpostalcodesmx will be available.
Basic Command usage
If you want to download all postal codes automatically run the following command:
python manage.py importpostalcodesmx
This command will attempt to connect to the Mexican Postal Service website (Correos de Mexico) and download the public zip file that contains all the postal codes available in xml format, extract them, process them and create the corresponding table in your database.
Local import usage
Alternatively, you can use the official xml file. Download it from the Mexican Postal Service website. and unzip it
Place the xml file in the same level as your manage.py file and run the following command:
python manage.py importpostalcodesmx --file=your_file_name.xml
if you don’t specify a file name the default file name from Mexican Postal Service website (Correos de Mexico) will be used:
CPdescarga.xml
Add API urls
Add django-postalcodes-mexico’s URL patterns:
from django_postalcodes_mexico import urls as django_postalcodes_mexico_urls
from django.urls import path
urlpatterns = [
# ...
path('your-url', include(django_postalcodes_mexico_urls)),
# ...
]
Quickstart using docker compose
To use docker compose to create an endpoint that returns the postal codes do the following:
First, clone the project and enter the project directory
git clone https://github.com/EduardoZepeda/django-postalcodes-mexico.git
cd django-postalcodes-mexico/
Basic environmental variables need to be declared at the root of the project in a file named db.env
# db.env
POSTGRES_PASSWORD=your-super-strong-password
POSTGRES_USER=yourUser
POSTGRES_DB=yourDatabaseName
SECRET_KEY=a-very-strong-django-secret-key
Also a variable port, where the endpoint will be available, in your OS, must be placed inside a file named .env. I used 8009, but you can use any port you want.
#.env
POSTAL_CODES_MX_PORT=8009
Once the latest requirements are fulfilled you’re ready to go.
docker-compose up
The script will fetch the most recent version of mexican postal codes directly to SEPOMEX (official correos de México website), create the required tables and get up a minimal django server, served using gunicorn.
curl 0.0.0.0:8009/29240/
API Usage
Use the following url to retrieve a postal code
your-url/<slug:postal_code>/
# examples:
# postal-code/01000/
# postal-code/02000/
If the request was successful you will receive a response in JSON formatted in this way:
{
"municipio": "San Cristóbal de las Casas",
"estado": "Chiapas",
"colonias": [
"La Isla",
"La Merced",
"De Mexicanos",
"San Ramón",
"1ro de Mayo"
],
"codigoPostal": "29240"
}
Please note that a Postal Code is associated with only one state(estado) and state area (municipio) but with many city areas (colonias).
Features
Automatic postal codes table generation
Local file processing
Running Tests
Does the code actually work?
source <YOURVIRTUALENV>/bin/activate (myenv) $ pip install tox (myenv) $ tox
Development commands
pip install -r requirements_dev.txt invoke -l
Credits
Tools used in rendering this package:
History
0.1.0 (2021-01-15)
First release on PyPI.
0.2.0 (2021-01-16)
Automatically postal codes download
0.3.0 (2021-01-19)
Local import functionality added
0.3.1 (2021-01-19)
Fix an error in README.rst syntax
0.4.0 (2021-06-15)
Add an extra search function
0.5.0 (2021-11-05)
Deprecate postal-code/ url
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file django_postalcodes_mexico-0.6.0-py2.py3-none-any.whl
.
File metadata
- Download URL: django_postalcodes_mexico-0.6.0-py2.py3-none-any.whl
- Upload date:
- Size: 22.8 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.12 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2dfd675a8b1df4a4b1be114b9f07262570ea78a6fed77844ed9ee1a9707e545 |
|
MD5 | 170b81d37113076cfa8b24fab3173d81 |
|
BLAKE2b-256 | 780d375888c7f874a9c7276013b1864ab4a18238b136c6dc05f067db1ed4542d |