Git Product home page Git Product logo

django-postcodepy-proxy's Introduction

Django Postcode Proxy

Build Status Documentation Status PyPI version Code Health Coverage Status

Simple proxy class to integrate Dutch 'postcode/huisnr' address verification in your Django application. This is done using the postcode.nl REST-API at https://api.postcode.nl. For documentation regarding API endpoints also check this url.

The API also provides an endpoint for information validation, enrichment and fraude risk check.

Beside this documentation, the complete documentation of this software is available at http://django-postcodepy-proxy.readthedocs.org.

Supported versions of Python and Django :

Py 2.7 Py 3.4 Py 3.5
Django 1.8 YES YES YES
Django 1.9 YES YES YES

Install

      $ pip install django-postcodepy-proxy

Quick start

  1. Add 'postcodepy_proxy' to your INSTALLED_APPS setting like this::

      INSTALLED_APPS= (
          ...
          'postcodepy_proxy',
      )
    
  2. Add the config part for the proxy::

      POSTCODEPY = {
        "AUTH" : {
          "API_ACCESS_KEY" : "<the key you got from postcode.nl>",
          "API_ACCESS_SECRET" : "<the secret you got from postcode.nl>",
        },
      }
    

In your app ...

Derive a class from the PostcodepyProxyView class and implement your own logic like the 2 simple examples below for HTML and JSON rendering.

Simple HTML rendering

  from django.shortcuts import render

  # Create your views here.

  from postcodepy_proxy.views import PostcodepyProxyView
  from postcodepy import postcodepy

  class PCDemoHTMLView(PostcodepyProxyView):
      template_name = "postcodeproxy.html"
  
      def get(self, request, *args, **kwargs):
          rv = super(PCDemoHTMLView, self).get(request, *args, **kwargs)
          return render(request, self.template_name, rv)

JSON rendering

Most likely is that you want JSON rendering for XHR-io in your application. Implement exception-handling that suits your needs.

  from django.http import HttpResponse
  from postcodepy.postcodepy import PostcodeError
  import json

  class PCDemoJSONView(PostcodepyProxyView):
      def get(self, request, *args, **kwargs):
          rv = None
          try:
              rv = super(PCDemoJSONView, self).get(request, *args, **kwargs)
          except PostcodeError, e:
              # Pass the exceptioninformation as response data
              rv = e.response_data

          return HttpResponse( json.dumps(rv), content_type="application/json")

Signal Check

Use the SignalProxyView to integrate the Signal API in your application.

  from postcodepy_proxy.views import SignalProxyView
  from postcodepy_proxy.signalapi import SignalRequestData
  from django.http import HttpResponse
  from postcodepy.postcodepy import PostcodeError
  import json

  class PCSignalJSONView(SignalProxyView):
  
      def post(self, request, *args, **kwargs):
          """
             perform the Signal lookup via the API-call
          """
          rv = None
          try:
              # Create a structure representing a valid signal-api-request,
              # as specified at api.postcode.nl
              sar = SignalRequestData(request.POST)()
              if 'csrfmiddlewaretoken' in sar:
                  del sar['csrfmiddlewaretoken']
              rv = super(PCSignalJSONView, self).get(request, sar=sar, **kwargs)
          except PostcodeError, e:
              # Pass the exceptioninformation as response data
              rv = e.response_data
  
          return HttpResponse(json.dumps(rv, indent=4), content_type="application/json")

Route the requests

  # Postcode urls
  url(r'^jsonpostcode/(?P<postcode>[\d]{4}[a-zA-Z]{2})/(?P<houseNumber>[\d]+)/$', views.PCDemoJSONView.as_view()),
  url(r'^jsonpostcode/(?P<postcode>[\d]{4}[a-zA-Z]{2})/(?P<houseNumber>[\d]+)/(?P<houseNumberAddition>[A-Za-z]+)/$', views.PCDemoJSONView.as_view()),
  # Signal urls
  # signal.html with some form that enables you to post the information for the request via AJAX/JSON to
  # the jsonsignal url and fetch the response
  url(r'^signal/$', TemplateView.as_view(template_name="signal.html")),
  url(r'^jsonsignal/$', views.PCSignalJSONView.as_view()),

django-postcodepy-proxy's People

Contributors

hootnot avatar

Watchers

 avatar  avatar

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.