From: Raphael Michel Date: Mon, 15 Oct 2018 18:36:29 +0000 (+0200) Subject: Resolve TODOs X-Git-Tag: 1.0.0~24 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=96efbd4256716af8c091f920adbce23855c238dd;p=thirdparty%2Fpython-drafthorse.git Resolve TODOs --- diff --git a/demo.py b/demo.py index 44c172c..0b7636c 100644 --- a/demo.py +++ b/demo.py @@ -9,6 +9,9 @@ doc.header.id = "RE1337" doc.header.name = "RECHNUNG" doc.header.type_code = "380" doc.header.issue_date_time.value = date.today() -doc.header.notes.add(IncludedNote(content="Test Node 1")) -doc.header.notes.add(IncludedNote(content="Test Node 2", subject_code="foo")) +doc.header.languages.add("de") +n = IncludedNote() +n.content.add("Test Node 1") +n.content.add("Test Node 2") +doc.header.notes.add(n) print(prettify(doc.serialize())) diff --git a/drafthorse/models/document.py b/drafthorse/models/document.py index a81fb66..6104b49 100644 --- a/drafthorse/models/document.py +++ b/drafthorse/models/document.py @@ -3,7 +3,7 @@ import xml.etree.cElementTree as ET from drafthorse.models.note import IncludedNote from . import NS_RAM, NS_UDT, NS_FERD_1p0, EXTENDED, BASIC from .elements import Element -from .fields import DateTimeField, Field, MultiField, StringField, IndicatorField +from .fields import DateTimeField, Field, MultiField, StringField, IndicatorField, MultiStringField from .trade import TradeTransaction @@ -58,8 +58,7 @@ class Header(Element): effective_period = Field(EffectivePeriod, required=False, profile=EXTENDED, _d="Vertragliches Fälligkeitsdatum der Rechnung") notes = MultiField(IncludedNote) - - # TODO: LanguageID + languages = MultiStringField(NS_FERD_1p0, "LanguageID", required=False, profile=EXTENDED) class Meta: namespace = NS_FERD_1p0 diff --git a/drafthorse/models/fields.py b/drafthorse/models/fields.py index 32c45be..1119b4e 100644 --- a/drafthorse/models/fields.py +++ b/drafthorse/models/fields.py @@ -195,7 +195,7 @@ class Container: self.child_type = child_type def add(self, item): - if not isinstance(item, self.child_type): + if isinstance(self.child_type, type) and not isinstance(item, self.child_type): raise TypeError("{} is not of type {}".format(item, self.child_type)) self.children.append(item) @@ -211,3 +211,72 @@ class MultiField(Field): def initialize(self): return self.cls(child_type=self.inner_type) + + +class StringContainer(Container): + def __init__(self, child_type, namespace, tag): + super().__init__(child_type) + self.namespace = namespace + self.tag = tag + + def append_to(self, node): + for child in self.children: + from .elements import StringElement + cnode = StringElement(namespace=self.namespace, tag=self.tag, text=child) + cnode.append_to(node) + + +class MultiStringField(Field): + def __init__(self, namespace, tag, default=False, required=False, profile=BASIC, _d=None): + super().__init__(StringContainer, default, required, profile, _d) + self.namespace = namespace + self.tag = tag + + def initialize(self): + return self.cls(child_type=str, namespace=self.namespace, tag=self.tag) + + +class CurrencyContainer(Container): + def __init__(self, child_type, namespace, tag): + super().__init__(child_type) + self.namespace = namespace + self.tag = tag + + def append_to(self, node): + for child in self.children: + from .elements import CurrencyElement + cnode = CurrencyElement(namespace=self.namespace, tag=self.tag, amount=child[0], currency=child[1]) + cnode.append_to(node) + + +class MultiCurrencyField(Field): + def __init__(self, namespace, tag, default=False, required=False, profile=BASIC, _d=None): + super().__init__(CurrencyContainer, default, required, profile, _d) + self.namespace = namespace + self.tag = tag + + def initialize(self): + return self.cls(child_type=(tuple, list), namespace=self.namespace, tag=self.tag) + + +class IDContainer(Container): + def __init__(self, child_type, namespace, tag): + super().__init__(child_type) + self.namespace = namespace + self.tag = tag + + def append_to(self, node): + for child in self.children: + from .elements import IDElement + cnode = IDElement(namespace=self.namespace, tag=self.tag, scheme_id=child[0], text=child[1]) + cnode.append_to(node) + + +class MultiIDField(Field): + def __init__(self, namespace, tag, default=False, required=False, profile=BASIC, _d=None): + super().__init__(IDContainer, default, required, profile, _d) + self.namespace = namespace + self.tag = tag + + def initialize(self): + return self.cls(child_type=(tuple, list), namespace=self.namespace, tag=self.tag) diff --git a/drafthorse/models/note.py b/drafthorse/models/note.py index 2dec5b2..21a101c 100644 --- a/drafthorse/models/note.py +++ b/drafthorse/models/note.py @@ -1,11 +1,11 @@ from . import NS_FERD_1p0, BASIC, COMFORT, EXTENDED from .elements import Element -from .fields import StringField +from .fields import StringField, MultiStringField class IncludedNote(Element): - content = StringField(NS_FERD_1p0, "Content", required=False, - profile=BASIC) # TODO: Can appear multiple times + content = MultiStringField(NS_FERD_1p0, "Content", required=False, + profile=BASIC) content_code = StringField(NS_FERD_1p0, "ContentCode", required=False, profile=EXTENDED) subject_code = StringField(NS_FERD_1p0, "SubjectCode", required=False, diff --git a/drafthorse/models/party.py b/drafthorse/models/party.py index 156e6ed..c636fd4 100644 --- a/drafthorse/models/party.py +++ b/drafthorse/models/party.py @@ -1,6 +1,6 @@ from . import NS_FERD_1p0, COMFORT, BASIC, EXTENDED from .elements import Element -from .fields import StringField, Field, IDField, MultiField +from .fields import StringField, Field, IDField, MultiField, MultiIDField class PostalTradeAddress(Element): @@ -67,8 +67,8 @@ class TradeContact(Element): class TradeParty(Element): id = StringField(NS_FERD_1p0, "ID", required=False, profile=COMFORT, _d="Identifier des Verkäufers") - global_id = IDField(NS_FERD_1p0, "GlobalID", required=False, profile=COMFORT, - _d="Globaler Identifier des Verkäufers") # TODO: Support multiple + global_id = MultiIDField(NS_FERD_1p0, "GlobalID", required=False, profile=COMFORT, + _d="Globaler Identifier des Verkäufers") name = StringField(NS_FERD_1p0, "Name", required=False, profile=BASIC) contact = Field(TradeContact, required=False, profile=EXTENDED, _d="Ansprechpartner des Käufers") diff --git a/drafthorse/models/payment.py b/drafthorse/models/payment.py index 8471787..88bcbd6 100644 --- a/drafthorse/models/payment.py +++ b/drafthorse/models/payment.py @@ -1,6 +1,7 @@ from . import NS_FERD_1p0, COMFORT, BASIC, EXTENDED from .elements import Element -from .fields import Field, StringField, IDField, DateTimeField, DecimalField, CurrencyField +from .fields import Field, StringField, IDField, DateTimeField, DecimalField, CurrencyField, MultiStringField, \ + MultiCurrencyField class PayerFinancialAccount(Element): @@ -44,7 +45,7 @@ class PayeeFinancialInstitution(Element): class PaymentMeans(Element): type_code = StringField(NS_FERD_1p0, "TypeCode", required=False, profile=COMFORT) - information = StringField(NS_FERD_1p0, "Information", required=False, profile=COMFORT) # TODO: Allow multiple + information = MultiStringField(NS_FERD_1p0, "Information", required=False, profile=COMFORT) id = IDField(NS_FERD_1p0, "ID", required=False, profile=BASIC) payer_account = Field(PayerFinancialAccount) payer_institution = Field(PayerFinancialInstitution) @@ -87,8 +88,8 @@ class PaymentTerms(Element): _d="Freitext der Zahlungsbedingungen") due = DateTimeField(NS_FERD_1p0, "DueDateDateTime", required=False, profile=COMFORT, _d="Fälligkeitsdatum") - partial_amount = CurrencyField(NS_FERD_1p0, "PartialPaymentAmount", profile=EXTENDED, - required=False, _d="Betrag der Teilzahlung") # TODO: Allow multiple times + partial_amount = MultiCurrencyField(NS_FERD_1p0, "PartialPaymentAmount", profile=EXTENDED, + required=False, _d="Betrag der Teilzahlung") penalty_terms = Field(PaymentPenaltyTerms, required=False, profile=EXTENDED, _d="Detailinformationen zu Zahlungszuschlägen") discount_terms = Field(PaymentDiscountTerms, required=False, profile=EXTENDED,