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.10 - 3.12)

  • Django (3.1 - 5.0)

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

0.6.1 (2024-31-11)

  • Deprecate Python older than 3.10 and Django 3.0

0.7.0 (2024-31-11)

  • Run tests for Django 6.0 and Python 3.12

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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_postalcodes_mexico-0.7.0-py2.py3-none-any.whl (23.0 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

File hashes

Hashes for django_postalcodes_mexico-0.7.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a572ef380d0320186e69b1e97b5a04d4b3c04d06bdd20ed6f37307f5f8d636a0
MD5 ddde7c65dfefac75b67353e711a83654
BLAKE2b-256 c8702d37456c9f4de0526bea404b3d99b751ba344cc1a1060889e52e8f61e728

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page