Giter Club home page Giter Club logo

django-simplecep's Introduction

django-simplecep

Django-simplecep helps you to fetch Brazilian address data associated to a CEP value. It also auto-populates form address-fields based on provided CEP.

Example: [image]

How it works

The library uses the configured data providers to fetch the CEP data. Each installed provider is used until the data is successfully fetched.

By default it will use as data-provider, in order:

  1. Official Correios API (SIGEP WEB)
  2. www.republicavirtual.com.br API
  3. viacep.com.br API

After fetch, the retrieved data is cached (by default on a Django model) so any data-retrieval attempt for the same CEP is resolved much faster and do not use external providers.

The providers and the cache mechanism can be fully customized by the user.

If the CEP data isn't cached and all providers fail, the NoAvailableCepProviders exception is raised.

Usage

This library can be used in three different ways:

As Python function

>>> from simplecep.fetcher import get_cep_data
>>> cep_data = get_cep_data('59615350')

>>> cep_data
<CEPAddress 59615350>

>>> cep_data.to_dict()
{'cep': '59615350', 'street': 'Rua Jo茫o Sim茫o do Nascimento', 'state': 'RN', 'district': 'Santa Delmira', 'city': 'Mossor贸'}

As Django app API endpoint

You can include the simplecep URLconf into your Django project URLs to be able to query CEP data using HTTP requests:

curl "http://localhost:8000/cep/59615350/"
{"cep": "59615350", "street": "Rua Jo茫o Sim茫o do Nascimento", "state": "RN", "district": "Santa Delmira", "city": "Mossor贸"}

As Django form field

The library provides the CepField form field which auto-populate other address fields in the form when a CEP is typed by the user.

from simplecep import CEPField

class CepForm(forms.Form):
    cep = CEPField(
        label='Your CEP',
        autofill={
            # key is the data-type and value is the form-field name
            "district": "bairro",
            "state": "estado",
            "city": "cidade",
            "street": "rua",
            "street_number": "numbero_rua",
        }
    )
    estado = forms.CharField()
    cidade = forms.CharField()
    bairro = forms.CharField()
    rua = forms.CharField()
    numbero_rua = forms.CharField()

The CepField form field uses Javascript (no jquery) to retrieve the data from the CEP endpoint available on your app. It can be totally customized.

Installation

  1. Install the django-simplecep package using pip or your favourite python package-manager:

    pip install django-cep
  2. Adds simplecep to your Django INSTALLED_APPS setting:

       INSTALLED_APPS = (
           # your other installed apps here
           # ...
           'simplecep',
       )
  3. Run Django migrate command to create the CEP cache table:

    python manage.py migrate
  4. Include the simplecep URLconf in your project urls.py:

    urlpatterns = [
       # your app paths here
       path('cep/', include('simplecep.urls')),
    ]

    This step is optional and can be skipped if you won't use the CEP API endpoint or the CepField form field.

Configuration

The library tries to use sane default-values, so you can use it without any additional configuration, but you can configure it adding a SIMPLECEP object to your Django settings file.

Here's the default configuration:

SIMPLECEP = {
    # a list of CEP providers modules - you can create your own
    "PROVIDERS": (
        "simplecep.providers.CorreiosSIGEPCEPProvider",
        "simplecep.providers.RepublicaVirtualCEPProvider",
        "simplecep.providers.ViaCEPProvider",
    ),
    # the cache class - it should implement dict methods
    "CACHE": "simplecep.cache.CepDatabaseCache",
    # max-time to wait for each provider response in secs
    "PROVIDERS_TIMEOUT": 2,
    # time to keep the cached data - 6 months by default
    "CEP_CACHE_MAXAGE": datetime.timedelta(days=30 * 6),
}

Customizing the CepField JS behaviour

By default the Javascript code included by the CepField:

  • Adds a mask to the CEP field so value format is always 99999-999
  • Fetches the CEP data using the browser fetch function
  • Shows a loading indicator in the CEP field while data is being fetched
  • Makes address fields readonly while the data is being fetched
  • Populate address fields with obtained CEP data
  • Focus on the next not-populated address field after populating the fields

Some behaviours above can be customized using CepField parameters. All behaviours can be customized writing some JS code.

Requirements

  • Django 2.0 or higher
  • Python 3.6 or higher

django-simplecep's People

Contributors

cauethenorio avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

alexsilva

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    馃枛 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 馃搳馃搱馃帀

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google 鉂わ笍 Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.