Code Monkey home page Code Monkey logo

django-vies's Introduction

image

image

Iontinuous Integration

Code Health

Test Coverage

MIT License

Join the chat at https://gitter.im/codingjoe/django-vies

Django-VIES

Django-VIES is a django extension that allows storing VAT Information Exchange System (VIES) data in django models and validation. Provided are a VATIN object, a ModelField and a FormField.

Installation

Current Stable

pip install django-vies

Latest Development

pip install -e git://github.com/codingjoe/django-vies.git#egg=django-vies

Usage

VATINField for models

from django.db import models
from vies.models import VATINField

class Company(models.Model):
    name = models.CharField(max_length=100)
    vat = VATINField(blank=True, null=True)

VATIN wrapper class, allows access to result.

>>> from vies.types import VATIN
>>> vat = VATIN('NL', '124851903B01')
>>> vat.is_valid()
True
>>> vat.data
(reply){
   countryCode = "NL"
   vatNumber = "124851903B01"
   requestDate = 2014-03-25
   valid = True
   name = "JIETER"
   address = "(...)"
 }

You can also use the classmethod VATIN.from_str to create VATIN from str.

>>> from vies.types import VATIN
>>> vat = VATIN.from_str('NL124851903B01')
>>> vat.is_valid()
True

The VIES API endpoint can be very unreliable and seems to have an IP based access limit. Therefore the VATINField does NOT perform API based validation by default. It needs to be explicitly turned on or performed in a separate task.

e.g.

from vies.models import VATINField
from vies.validators import VATINValidator


class Company(models.Model):
    name = models.CharField(max_length=100)
    vat = VATINField(validators=[VATINValidator(verify=True, validate=True)])

validate=True will tell the validator to validate against the VIES API. verify is enabled on by default and will only verify that the VATIN matches the country's specifications.

It is recommended to perform VIES API validation inside an asynchronous task.

e.g. using celery

from celery import shared_task
from vies.models import VATINField
from vies.validators import VATINValidator


class Company(models.Model):
    name = models.CharField(max_length=100)
    vat = VATINField()
    vat_is_valid = models.BooleanField(default=False)

    def __init__(self, *args, **kwargs):
        self.__vat = self.vat
        super(Company, self).__init__(*args, **kwargs)

    def save(self, *args, **kwargs):
        if self.__vat != self.vat:
            validate_vat_field.delay(self)
        super(Company, self).save(*args, **kwargs)
        self.__vat = self.vat

    def refresh_from_db(self)
        super(Company, self).refresh_from_db()
        self.__vat = self.vat

@shared_task
def validate_vat_field(company):
    try:
        company.vat.validate()
    except ValidationError:
        self.vat_is_valid = False
    else:
        self.vat_is_valid = False
    finally:
        self.save()

Translations

Feel free to contribute translations, it's simple!

cd vies
django-admin makemessages -l $YOUR_COUNTRY_CODE

Just edit the generated PO file. Pull-Requests are welcome!

License

The MIT License (MIT)

Copyright (c) 2014-2016 Johannes Hoppe

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

django-vies's People

Contributors

agutierrezrodriguez avatar bbstuntman avatar bitdeli-chef avatar chripede avatar codingjoe avatar gitter-badger avatar jieter avatar nijel avatar pennersr avatar pjmcdermott avatar pyup-bot avatar requires avatar rm-- avatar vdboor 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.