def verify_cf(request): """ Verifica che il codice fiscale sia conforme ai dati inseriti nel form, altrimenti mostra quali dati sono errati :request: request contenente i dati del cittadino :return: dic con True/False """ try: fiscal_code = request.POST.get('fiscalNumber').upper() belfiore_code = request.POST.get('placeOfBirth') if request.POST.get('placeOfBirth') else request.POST.get( 'nationOfBirth') verify_fiscal_code = { 'familyName': fiscal_code[0:3] == codicefiscale.encode_surname(request.POST.get('familyName')), 'name': fiscal_code[3:6] == codicefiscale.encode_surname(request.POST.get('name')), 'gender': codicefiscale.decode(fiscal_code)['sex'] == request.POST.get('gender'), 'dateOfBirth': codicefiscale.decode(fiscal_code)['birthdate'] == datetime.datetime.strptime( request.POST.get('dateOfBirth'), '%d/%m/%Y'), 'belfiore_code': codicefiscale.decode(fiscal_code)['birthplace']['code'] == belfiore_code } if belfiore_code == 'Z998': verify_fiscal_code['status_code'] = StatusCode.BAD_REQUEST.value return verify_fiscal_code calculated_cf = codicefiscale.encode(request.POST.get('familyName'), request.POST.get('name'), request.POST.get('gender'), request.POST.get('dateOfBirth'), belfiore_code ) if fiscal_code == calculated_cf.upper(): verify_fiscal_code = { 'status_code': StatusCode.OK.value, 'familyName': True, 'name': True, 'gender': True, 'dateOfBirth': True, 'belfiore_code': True, } else: verify_fiscal_code['status_code'] = StatusCode.ERROR.value except Exception as e: LOG.error("Exception: {}".format(str(e)), extra=set_client_ip()) verify_fiscal_code = { 'status_code': StatusCode.EXC.value, } return verify_fiscal_code
def clean_fiscalNumber(self): surname_name_cf = self.cleaned_data.get('fiscalNumber').upper()[0:6] try: encode_name_surname = codicefiscale.encode_surname(self.cleaned_data.get('familyName')) + \ codicefiscale.encode_name(self.cleaned_data.get('name')) if surname_name_cf == encode_name_surname: return else: raise ValidationError("Il codice fiscale non corrisponde con i dati inseriti") except Exception as e: LOG.warning("Warning: {}".format(str(e)), extra=set_client_ip()) raise ValidationError("Il codice fiscale non corrisponde con i dati inseriti")
def test_encode_surname(self): data = [{ 'input': '', 'result': 'XXX', }, { 'input': 'Caccamo', 'result': 'CCC', }, { 'input': 'Fò', 'result': 'FOX', }] for obj in data: # with self.subTest(obj=obj): self.assertEqual(codicefiscale.encode_surname(obj['input']), obj['result'])