Package: django-vies
Validate and store VAT Information Exchange System (VIES) data in Django.Installationpython3-mpipinstalldjango-viesUsageVATINFieldfor modelsfromdjango.dbimportmodelsfromvies.modelsimportVATINFieldclassCompany(models.Model):name=models.CharField(max_length=100)vat=VATINField(blank=True,null=True)VATINwrapper class, allows access to result.>>>fromvies.typesimportVATIN>>>vat=VATIN('LU','26375245')>>>vat.is_valid()True>>>vat.data{'countryCode':'LU','vatNumber':'26375245','requestDate':datetime.date(2020,4,13),'valid':True,'name':'AMAZON EUROPE CORE S.A R.L.','address':'38, AVENUE JOHN F. KENNEDY\nL-1855  LUXEMBOURG'}You can also use the classmethodVATIN.from_strto createVATINfromstr.>>>fromvies.typesimportVATIN>>>vat=VATIN.from_str('LU26375245')>>>vat.is_valid()TrueThe VIES API endpoint can be very unreliable and seems to have an IP based access limit.
Therefore theVATINFielddoes NOT perform API based validation by default. It needs
to be explicitly turned on or performed in a separate task.e.g.fromvies.modelsimportVATINFieldfromvies.validatorsimportVATINValidatorclassCompany(models.Model):name=models.CharField(max_length=100)vat=VATINField(validators=[VATINValidator(verify=True,validate=True)])validate=Truewill tell the validator to validate against the VIES API.verifyis 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 celeryfromceleryimportshared_taskfromvies.modelsimportVATINFieldfromvies.typesimportVATINfromdjango.core.exceptionsimportValidationErrorclassCompany(models.Model):name=models.CharField(max_length=100)vat=VATINField()vat_is_valid=models.BooleanField(default=False)def__init__(self,*args,**kwargs):super(Company,self).__init__(*args,**kwargs)self.__vat=self.vatdefsave(self,*args,**kwargs):ifself.__vat!=self.vat:validate_vat_field.delay(self.pk)super(Company,self).save(*args,**kwargs)self.__vat=self.vatdefrefresh_from_db(self,*args,**kwargs)super(Company,self).refresh_from_db(*args,**kwargs)self.__vat=self.vat@shared_taskdefvalidate_vat_field(company_id):company=Company.objects.get(pk=company_id)vat=VATIN.from_str(company.vat)try:vat.validate()exceptValidationError:company.vat_is_valid=Falseelse:company.vat_is_valid=Truefinally:company.save(update_fields=['vat_is_valid'])You can also usecelery.current_app.send_task('validate_vat_field',kwargs={"company_id":self.pk})to call asynchronous task to avoidcircular import errors.TranslationsFeel free to contribute translations, it’s simple!cdviesdjango-adminmakemessages-l$YOUR_COUNTRY_CODEJust edit the generated PO file. Pull-Requests are welcome!LicenseThe MIT License (MIT)Copyright (c) 2014-2016 Johannes HoppePermission 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.