|
| 1 | +from typing import List, Optional, TypeVar |
| 2 | + |
| 3 | +from mindee.documents.base import Document, TypeApiPrediction, clean_out_string |
| 4 | +from mindee.fields.company_registration import CompanyRegistrationField |
| 5 | +from mindee.fields.date import DateField |
| 6 | +from mindee.fields.locale import LocaleField |
| 7 | +from mindee.fields.text import TextField |
| 8 | + |
| 9 | + |
| 10 | +class ProofOfAddressV1(Document): |
| 11 | + locale: LocaleField |
| 12 | + """locale information""" |
| 13 | + date: DateField |
| 14 | + """ISO date yyyy-mm-dd. Works both for European and US dates.""" |
| 15 | + dates: List[DateField] = [] |
| 16 | + """All extracted ISO date yyyy-mm-dd""" |
| 17 | + issuer_address: TextField |
| 18 | + """Address of the document's issuer.""" |
| 19 | + issuer_company_registration: List[CompanyRegistrationField] = [] |
| 20 | + """Generic: VAT NUMBER, TAX ID, COMPANY REGISTRATION NUMBER or country specific.""" |
| 21 | + issuer_name: TextField |
| 22 | + """Name of the person or company issuing the document.""" |
| 23 | + recipient_address: TextField |
| 24 | + """Address of the recipient.""" |
| 25 | + recipient_company_registration: List[CompanyRegistrationField] = [] |
| 26 | + """Generic: VAT NUMBER, TAX ID, COMPANY REGISTRATION NUMBER or country specific.""" |
| 27 | + recipient_name: TextField |
| 28 | + """Name of the document's recipient.""" |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + api_prediction=None, |
| 33 | + input_source=None, |
| 34 | + page_n: Optional[int] = None, |
| 35 | + document_type="proof_of_address", |
| 36 | + ): |
| 37 | + """ |
| 38 | + Proof of Address document. |
| 39 | +
|
| 40 | + :param api_prediction: Raw prediction from HTTP response |
| 41 | + :param input_source: Input object |
| 42 | + :param page_n: Page number for multi pages pdf input |
| 43 | + """ |
| 44 | + super().__init__( |
| 45 | + input_source=input_source, |
| 46 | + document_type=document_type, |
| 47 | + api_prediction=api_prediction, |
| 48 | + page_n=page_n, |
| 49 | + ) |
| 50 | + self._build_from_api_prediction(api_prediction["prediction"], page_n=page_n) |
| 51 | + |
| 52 | + def _build_from_api_prediction( |
| 53 | + self, api_prediction: TypeApiPrediction, page_n: Optional[int] = None |
| 54 | + ) -> None: |
| 55 | + """ |
| 56 | + Build the object from the prediction API JSON. |
| 57 | +
|
| 58 | + :param api_prediction: Raw prediction from HTTP response |
| 59 | + :param page_n: Page number for multi pages pdf input |
| 60 | + """ |
| 61 | + self.locale = LocaleField( |
| 62 | + api_prediction["locale"], value_key="language", page_n=page_n |
| 63 | + ) |
| 64 | + self.date = DateField(api_prediction["date"], page_n=page_n) |
| 65 | + self.dates = [ |
| 66 | + DateField(tax_prediction, page_n=page_n) |
| 67 | + for tax_prediction in api_prediction["dates"] |
| 68 | + ] |
| 69 | + self.issuer_name = TextField(api_prediction["issuer_name"], page_n=page_n) |
| 70 | + self.issuer_address = TextField(api_prediction["issuer_address"], page_n=page_n) |
| 71 | + self.issuer_company_registration = [ |
| 72 | + CompanyRegistrationField(tax_prediction, page_n=page_n) |
| 73 | + for tax_prediction in api_prediction["issuer_company_registration"] |
| 74 | + ] |
| 75 | + self.recipient_name = TextField(api_prediction["recipient_name"], page_n=page_n) |
| 76 | + self.recipient_address = TextField( |
| 77 | + api_prediction["recipient_address"], page_n=page_n |
| 78 | + ) |
| 79 | + self.recipient_company_registration = [ |
| 80 | + CompanyRegistrationField(tax_prediction, page_n=page_n) |
| 81 | + for tax_prediction in api_prediction["recipient_company_registration"] |
| 82 | + ] |
| 83 | + |
| 84 | + def __str__(self) -> str: |
| 85 | + issuer_company_registrations = "; ".join( |
| 86 | + [str(n.value) for n in self.issuer_company_registration] |
| 87 | + ) |
| 88 | + recipient_company_registrations = "; ".join( |
| 89 | + [str(n.value) for n in self.recipient_company_registration] |
| 90 | + ) |
| 91 | + dates = "\n ".join([str(n.value) for n in self.dates]) |
| 92 | + return clean_out_string( |
| 93 | + "----- Proof of Address V1 -----\n" |
| 94 | + f"Filename: {self.filename or ''}\n" |
| 95 | + f"Locale: {self.locale}\n" |
| 96 | + f"Issuer name: {self.issuer_name}\n" |
| 97 | + f"Issuer Address: {self.issuer_address}\n" |
| 98 | + f"Issuer Company Registrations: {issuer_company_registrations}\n" |
| 99 | + f"Recipient name: {self.recipient_name}\n" |
| 100 | + f"Recipient Address: {self.recipient_address}\n" |
| 101 | + f"Recipient Company Registrations: {recipient_company_registrations}\n" |
| 102 | + f"Issuance Date: {self.date}\n" |
| 103 | + f"Dates: {dates}\n" |
| 104 | + "----------------------" |
| 105 | + ) |
| 106 | + |
| 107 | + def _checklist(self) -> None: |
| 108 | + pass |
| 109 | + |
| 110 | + |
| 111 | +TypeProofOfAddressV1 = TypeVar("TypeProofOfAddressV1", bound=ProofOfAddressV1) |
0 commit comments