Skip to main content

A Django Package for handling Mexico postal codes

Project description

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.

Documentation

The full documentation is at https://django-postalcodes-mexico.readthedocs.io.

Quickstart

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 importpostalcodes will be available.

Basic Command usage

If you want to download all postal codes automatically run the following command:

python manage.py importpostalcodes

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 importpostalcodes --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('', include(django_postalcodes_mexico_urls)),
    ...
]

API Usage

Use the following url to retrieve a postal code

postal-code/<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

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.4.0.tar.gz (16.1 kB view hashes)

Uploaded Source

Built Distribution

django_postalcodes_mexico-0.4.0-py2.py3-none-any.whl (19.2 kB view hashes)

Uploaded Python 2 Python 3

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