A Django Package for handling Mexico's postal codes
Project description
Leave a star if you find this package useful.
A Django Package for getting and handling the Mexican Postal Service (Correos de Mexico) postal codes information.
Please leave a star if you find this package useful.
Compatibility
Python (3.8 - 3.10)
Django (2.2 - 4.1)
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
0.6.0 (2022-10-06)
Deprecate Python 3.6, Python 3.7. Add support for Django 4.1
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
Built Distribution
File details
Details for the file django-postalcodes-mexico-0.6.1.tar.gz
.
File metadata
- Download URL: django-postalcodes-mexico-0.6.1.tar.gz
- Upload date:
- Size: 19.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ecee98de5fc12b62974b95f1012234e18276ca14d66384d408db084ad9e73113 |
|
MD5 | 09cbbc2c6fafc014fb63a9462c54c8f8 |
|
BLAKE2b-256 | fdba5479d185d1301a4429b952fea042a217276cbf1b0eeb1c7efe17f8447dc3 |
File details
Details for the file django_postalcodes_mexico-0.6.1-py2.py3-none-any.whl
.
File metadata
- Download URL: django_postalcodes_mexico-0.6.1-py2.py3-none-any.whl
- Upload date:
- Size: 21.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a20426ae80eb926cabcfc65242afb1b2564428a1d039ab5352dfaceced41bcd |
|
MD5 | e2545cd4c34cef5415bdfcb6773c26c0 |
|
BLAKE2b-256 | 13f7e8044a0a8b0db9e14169380ea8376dc575121e31ba277d783c0352a25b9e |