Skip to main content

A Django Package for handling Mexico's postal codes

Project description

Leave a star if you find this package useful.

https://badge.fury.io/py/django-postalcodes-mexico.svg https://travis-ci.org/EduardoZepeda/django-postalcodes-mexico.svg?branch=master https://codecov.io/gh/EduardoZepeda/django-postalcodes-mexico/branch/master/graph/badge.svg

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

django-postalcodes-mexico-0.6.1.tar.gz (19.2 kB view details)

Uploaded Source

Built Distribution

django_postalcodes_mexico-0.6.1-py2.py3-none-any.whl (21.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-postalcodes-mexico-0.6.1.tar.gz.

File metadata

File hashes

Hashes for django-postalcodes-mexico-0.6.1.tar.gz
Algorithm Hash digest
SHA256 ecee98de5fc12b62974b95f1012234e18276ca14d66384d408db084ad9e73113
MD5 09cbbc2c6fafc014fb63a9462c54c8f8
BLAKE2b-256 fdba5479d185d1301a4429b952fea042a217276cbf1b0eeb1c7efe17f8447dc3

See more details on using hashes here.

File details

Details for the file django_postalcodes_mexico-0.6.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_postalcodes_mexico-0.6.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1a20426ae80eb926cabcfc65242afb1b2564428a1d039ab5352dfaceced41bcd
MD5 e2545cd4c34cef5415bdfcb6773c26c0
BLAKE2b-256 13f7e8044a0a8b0db9e14169380ea8376dc575121e31ba277d783c0352a25b9e

See more details on using hashes here.

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