|
| 1 | +from yoti_python_sdk.doc_scan.constants import ( |
| 2 | + DOCUMENT_RESTRICTIONS, |
| 3 | + INCLUSION_BLACKLIST, |
| 4 | + INCLUSION_WHITELIST, |
| 5 | +) |
| 6 | +from yoti_python_sdk.utils import YotiSerializable |
| 7 | +from .document_filter import DocumentFilter |
| 8 | + |
| 9 | + |
| 10 | +class DocumentRestriction(YotiSerializable): |
| 11 | + def __init__(self, country_codes, document_types): |
| 12 | + self.__country_codes = country_codes |
| 13 | + self.__document_types = document_types |
| 14 | + |
| 15 | + @property |
| 16 | + def country_codes(self): |
| 17 | + return self.__country_codes |
| 18 | + |
| 19 | + @property |
| 20 | + def document_types(self): |
| 21 | + return self.__document_types |
| 22 | + |
| 23 | + def to_json(self): |
| 24 | + return { |
| 25 | + "country_codes": self.country_codes, |
| 26 | + "document_types": self.document_types, |
| 27 | + } |
| 28 | + |
| 29 | + |
| 30 | +class DocumentRestrictionBuilder(object): |
| 31 | + def __init__(self): |
| 32 | + self.__country_codes = None |
| 33 | + self.__document_types = None |
| 34 | + |
| 35 | + def with_country_codes(self, country_codes): |
| 36 | + """ |
| 37 | + Sets the list of country codes on the document restriction |
| 38 | +
|
| 39 | + :param country_codes: the list of country codes |
| 40 | + :type country_codes: list[str] |
| 41 | + :return: the builder |
| 42 | + :rtype: DocumentRestrictionBuilder |
| 43 | + """ |
| 44 | + self.__country_codes = country_codes |
| 45 | + return self |
| 46 | + |
| 47 | + def with_document_types(self, document_types): |
| 48 | + """ |
| 49 | + Sets the list of document types on the document restriction |
| 50 | +
|
| 51 | + :param document_types: the list of document types |
| 52 | + :type document_types: list[str] |
| 53 | + :return: the builder |
| 54 | + :rtype: DocumentRestrictionBuilder |
| 55 | + """ |
| 56 | + self.__document_types = document_types |
| 57 | + return self |
| 58 | + |
| 59 | + def build(self): |
| 60 | + """ |
| 61 | + Builds the document restriction using the values supplied to the builder |
| 62 | +
|
| 63 | + :return: the document restriction |
| 64 | + :rtype: DocumentRestriction |
| 65 | + """ |
| 66 | + return DocumentRestriction(self.__country_codes, self.__document_types) |
| 67 | + |
| 68 | + |
| 69 | +class DocumentRestrictionsFilter(DocumentFilter): |
| 70 | + def __init__(self, inclusion, documents): |
| 71 | + DocumentFilter.__init__(self, DOCUMENT_RESTRICTIONS) |
| 72 | + |
| 73 | + self.__inclusion = inclusion |
| 74 | + self.__documents = documents |
| 75 | + |
| 76 | + @property |
| 77 | + def inclusion(self): |
| 78 | + return self.__inclusion |
| 79 | + |
| 80 | + @property |
| 81 | + def documents(self): |
| 82 | + return self.__documents |
| 83 | + |
| 84 | + def to_json(self): |
| 85 | + parent = DocumentFilter.to_json(self) |
| 86 | + parent["inclusion"] = self.inclusion |
| 87 | + parent["documents"] = self.documents |
| 88 | + return parent |
| 89 | + |
| 90 | + |
| 91 | +class DocumentRestrictionsFilterBuilder(object): |
| 92 | + """ |
| 93 | + Builder used to create a document restrictions filter. |
| 94 | +
|
| 95 | + Example:: |
| 96 | +
|
| 97 | + document_restriction = (DocumentRestrictionBuilder() |
| 98 | + .with_country_codes("GBR", "USA") |
| 99 | + .with_document_types("PASSPORT") |
| 100 | + .build()) |
| 101 | +
|
| 102 | + filter = (DocumentRestrictionsFilterBuilder() |
| 103 | + .for_whitelist() |
| 104 | + .with_document_restriction(document_restriction) |
| 105 | + .build()) |
| 106 | + """ |
| 107 | + |
| 108 | + def __init__(self): |
| 109 | + self.__inclusion = None |
| 110 | + self.__documents = None |
| 111 | + |
| 112 | + def for_whitelist(self): |
| 113 | + """ |
| 114 | + Sets the inclusion to whitelist the document restrictions |
| 115 | +
|
| 116 | + :return: the builder |
| 117 | + :rtype: DocumentRestrictionsFilterBuilder |
| 118 | + """ |
| 119 | + self.__inclusion = INCLUSION_WHITELIST |
| 120 | + return self |
| 121 | + |
| 122 | + def for_blacklist(self): |
| 123 | + """ |
| 124 | + Sets the inclusion to blacklist the document restrictions |
| 125 | +
|
| 126 | + :return: the builder |
| 127 | + :rtype: DocumentRestrictionsFilterBuilder |
| 128 | + """ |
| 129 | + self.__inclusion = INCLUSION_BLACKLIST |
| 130 | + return self |
| 131 | + |
| 132 | + def with_document_restriction(self, document_restriction): |
| 133 | + """ |
| 134 | + Adds a document restriction to the filter |
| 135 | +
|
| 136 | + :param document_restriction: the document restriction |
| 137 | + :type document_restriction: DocumentRestriction |
| 138 | + :return: the builder |
| 139 | + :rtype: DocumentRestrictionsFilterBuilder |
| 140 | + """ |
| 141 | + if self.__documents is None: |
| 142 | + self.__documents = [] |
| 143 | + |
| 144 | + self.__documents.append(document_restriction) |
| 145 | + return self |
| 146 | + |
| 147 | + def build(self): |
| 148 | + """ |
| 149 | + Builds the document restrictions filter, using the supplied values |
| 150 | +
|
| 151 | + :return: the filter |
| 152 | + :rtype: DocumentRestrictionsFilter |
| 153 | + """ |
| 154 | + return DocumentRestrictionsFilter(self.__inclusion, self.__documents) |
0 commit comments