Skip to content

Commit

Permalink
🚧 improvement: First step on migrating to Pydantic 2
Browse files Browse the repository at this point in the history
Needs more testing before merging.
  • Loading branch information
kiloreven committed Feb 13, 2024
1 parent 7ac5b99 commit 28d9786
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 245 deletions.
18 changes: 11 additions & 7 deletions oda_wd_client/base/types.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from base64 import b64encode
from datetime import date
from enum import Enum
from typing import Self
from typing import Annotated, Self

import magic
from pydantic import BaseModel
from pydantic import BaseModel, BeforeValidator
from suds import sudsobject

from oda_wd_client.base.api import WorkdayClient
from oda_wd_client.base.utils import get_id_from_list
from oda_wd_client.base.utils import get_id_from_list, parse_workday_date

mime = magic.Magic(mime=True)

WorkdayDate = Annotated[date, BeforeValidator(parse_workday_date)]
WorkdayOptionalDate = Annotated[date | None, BeforeValidator(parse_workday_date)]


class WorkdayReferenceBaseModel(BaseModel):
"""
Expand All @@ -33,7 +37,7 @@ class TaxOption(WorkdayReferenceBaseModel):
"""

workday_id: str | Enum | None
workday_id: str | Enum | None = None
workday_id_type: str
workday_parent_id: str | Enum | None = None
workday_parent_type: str | None = None
Expand Down Expand Up @@ -98,7 +102,7 @@ def from_id_list(cls, id_list: list, **extra) -> Self | None:
:return: instance of Self
"""
workday_id = get_id_from_list(
id_list, cls.__fields__["workday_id_type"].default
id_list, cls.model_fields["workday_id_type"].default
)
if workday_id:
return cls(workday_id=workday_id, **extra)
Expand All @@ -118,8 +122,8 @@ class File(BaseModel):

filename: str
file_content: bytes
comment: str | None
content_type: str | None
comment: str | None = None
content_type: str | None = None

def wd_object(self, client: WorkdayClient):
doc = client.factory(f"ns0:{self.field_type}")
Expand Down
4 changes: 2 additions & 2 deletions oda_wd_client/base/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import date, datetime

# As defined by API docs
WORKDAY_DATE_FORMAT = "%m/%d/%Y"
Expand All @@ -21,7 +21,7 @@ def get_id_from_list(id_list: list, id_type: str) -> str | None:
return None


def parse_workday_date(val):
def parse_workday_date(val: str | date | None) -> date | None:
if isinstance(val, str):
return datetime.strptime(val, WORKDAY_DATE_FORMAT).date()
return val
16 changes: 8 additions & 8 deletions oda_wd_client/service/financial_management/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class RateTypeID(str, Enum):
budget = "Budget"
average = "Average"

workday_id: str | None
workday_id: str | None = None
# ISO 4217 defines three letters for currency ID
from_currency_iso: str = Field(max_length=3)
to_currency_iso: str = Field(max_length=3)
Expand All @@ -39,7 +39,7 @@ class ConversionRateType(BaseModel):
"""

workday_id: str
text_id: str | None
text_id: str | None = None
description: str
is_default: bool = False

Expand All @@ -64,9 +64,9 @@ class Company(WorkdayReferenceBaseModel):
_class_name = "CompanyObject"
workday_id: str
workday_id_type: Literal["Company_Reference_ID"] = "Company_Reference_ID"
name: str | None
currency: Currency | None
country_code: str | None = Field(max_length=2)
name: str | None = None
currency: Currency | None = None
country_code: str | None = Field(max_length=2, default=None)


class JournalSource(WorkdayReferenceBaseModel):
Expand Down Expand Up @@ -112,7 +112,7 @@ class SpendCategory(WorkdayReferenceBaseModel):
_class_name = "Spend_CategoryObject"
workday_id: str
workday_id_type: Literal["Spend_Category_ID"] = "Spend_Category_ID"
name: str | None
name: str | None = None
inactive: bool = False
usage_ids: list[str] = []

Expand All @@ -128,7 +128,7 @@ class CostCenterWorktag(WorkdayReferenceBaseModel):
_class_name = "Accounting_WorktagObject"
workday_id: str
workday_id_type: Literal["Cost_Center_Reference_ID"] = "Cost_Center_Reference_ID"
name: str | None
name: str | None = None
active: bool = True


Expand All @@ -143,7 +143,7 @@ class ProjectWorktag(WorkdayReferenceBaseModel):
_class_name = "Accounting_WorktagObject"
workday_id: str
workday_id_type: Literal["Project_ID"] = "Project_ID"
name: str | None
name: str | None = None
inactive: bool = False


Expand Down
10 changes: 4 additions & 6 deletions oda_wd_client/service/human_resources/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Optional

from pydantic import BaseModel

# All public imports should be done through oda_wd_client.types.human_resources
Expand All @@ -12,10 +10,10 @@ class Worker(BaseModel):
"""

workday_id: str
employee_number: Optional[str]
employee_number: str | None = None
name: str
work_email: Optional[str]
secondary_email: Optional[str]
work_email: str | None = None
secondary_email: str | None = None


class PersonReference(BaseModel):
Expand All @@ -29,7 +27,7 @@ class PersonReference(BaseModel):
subset of this data, and is not really expandable to support all uses of "RoleType" in Workday.
"""

workday_id: str | None
workday_id: str | None = None
ref_type: str
ref_id: str

Expand Down
75 changes: 33 additions & 42 deletions oda_wd_client/service/resource_management/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
from enum import Enum
from typing import Literal

from pydantic import BaseModel, Field, validator
from pydantic import BaseModel, Field

from oda_wd_client.base.types import File, WorkdayReferenceBaseModel
from oda_wd_client.base.utils import parse_workday_date
from oda_wd_client.base.types import File, WorkdayDate, WorkdayReferenceBaseModel
from oda_wd_client.service.financial_management.types import (
Company,
CostCenterWorktag,
Expand Down Expand Up @@ -77,17 +76,17 @@ class Supplier(WorkdayReferenceBaseModel):
workday_id: str
workday_id_type: Literal["Supplier_ID"] = "Supplier_ID"
status: SupplierStatus | None = None
reference_id: str | None
name: str | None
payment_terms: str | None
address: str | None
phone: str | None
email: str | None
url: str | None
currency: str | None
bank_account: str | None
iban: str | None
primary_tax_id: str | None
reference_id: str | None = None
name: str | None = None
payment_terms: str | None = None
address: str | None = None
phone: str | None = None
email: str | None = None
url: str | None = None
currency: str | None = None
bank_account: str | None = None
iban: str | None = None
primary_tax_id: str | None = None


class TaxRate(WorkdayReferenceBaseModel):
Expand Down Expand Up @@ -132,7 +131,7 @@ class FinancialAttachmentData(File):
Reference: https://community.workday.com/sites/default/files/file-hosting/productionapi/Resource_Management/v40.2/Submit_Supplier_Invoice.html#Financials_Attachment_DataType # noqa
"""

field_type = "Financials_Attachment_DataType"
field_type: str = "Financials_Attachment_DataType"


class PrepaidAmortizationType(WorkdayReferenceBaseModel):
Expand Down Expand Up @@ -176,21 +175,21 @@ class SupplierInvoiceLine(BaseModel):
Reference: https://community.workday.com/sites/default/files/file-hosting/productionapi/Resource_Management/v40.2/Submit_Supplier_Invoice.html#Supplier_Invoice_Line_Replacement_DataType # noqa
"""

order: int | None
order: int | None = None
quantity: Decimal = Field(max_digits=22, decimal_places=2, default=Decimal(0))
description: str = "-No description-"
tax_rate_options_data: TaxRateOptionsData | None
tax_applicability: TaxApplicability | None
tax_code: TaxCode | None
spend_category: SpendCategory | None
cost_center: CostCenterWorktag | None
project: ProjectWorktag | None
tax_rate_options_data: TaxRateOptionsData | None = None
tax_applicability: TaxApplicability | None = None
tax_code: TaxCode | None = None
spend_category: SpendCategory | None = None
cost_center: CostCenterWorktag | None = None
project: ProjectWorktag | None = None
# Incl. VAT
gross_amount: Decimal = Field(max_digits=18, decimal_places=3)
# Excl. VAT
net_amount: Decimal | None = Field(max_digits=18, decimal_places=3)
tax_amount: Decimal | None = Field(max_digits=18, decimal_places=3)
budget_date: date | None
net_amount: Decimal | None = Field(max_digits=18, decimal_places=3, default=None)
tax_amount: Decimal | None = Field(max_digits=18, decimal_places=3, default=None)
budget_date: date | None = None


class BaseSupplierInvoice(WorkdayReferenceBaseModel):
Expand All @@ -200,21 +199,21 @@ class BaseSupplierInvoice(WorkdayReferenceBaseModel):
Main reference: https://community.workday.com/sites/default/files/file-hosting/productionapi/Resource_Management/v40.2/Submit_Supplier_Invoice.html#Supplier_Invoice_DataType # noqa
"""

invoice_number: str | None
invoice_number: str | None = None
company: Company
currency: Currency
supplier: Supplier
due_date: date
due_date: WorkdayDate
total_amount: Decimal = Field(max_digits=26, decimal_places=6)
tax_amount: Decimal = Field(max_digits=26, decimal_places=6)
tax_option: TaxOption | None
additional_reference_number: str | None
additional_type_reference: AdditionalReferenceType | None
external_po_number: str | None
tax_option: TaxOption | None = None
additional_reference_number: str | None = None
additional_type_reference: AdditionalReferenceType | None = None
external_po_number: str | None = None
prepayment_release_type_reference: PrepaidAmortizationType | None = None

lines: list[SupplierInvoiceLine]
attachments: list[FinancialAttachmentData] | None
attachments: list[FinancialAttachmentData] | None = None

# Submit to business process rather than uploading invoice in draft mode
submit: bool = True
Expand All @@ -231,13 +230,9 @@ class SupplierInvoice(BaseSupplierInvoice):
"Supplier_Invoice_Reference_ID"
] = "Supplier_Invoice_Reference_ID"

invoice_date: date
invoice_date: WorkdayDate
prepaid: bool = False

_normalize_dates = validator("invoice_date", "due_date", allow_reuse=True)(
parse_workday_date
)


class SupplierInvoiceAdjustment(BaseSupplierInvoice):
"""
Expand All @@ -248,11 +243,7 @@ class SupplierInvoiceAdjustment(BaseSupplierInvoice):
"Supplier_Invoice_Adjustment_Reference_ID"
] = "Supplier_Invoice_Adjustment_Reference_ID"

adjustment_date: date
adjustment_date: WorkdayDate
adjustment_reason: InvoiceAdjustmentReason = InvoiceAdjustmentReason(
workday_id="Other_Terms"
)

_normalize_dates = validator("adjustment_date", "due_date", allow_reuse=True)(
parse_workday_date
)
4 changes: 2 additions & 2 deletions oda_wd_client/service/staffing/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class OdaCategory(str, Enum):
other_employee_documents = "DOC_CAT_Other_employee_documents"

employee_number: str
wd_owned_category: WorkdayCategory | None
oda_category: OdaCategory | None
wd_owned_category: WorkdayCategory | None = None
oda_category: OdaCategory | None = None
filename: str
comment: str
Loading

0 comments on commit 28d9786

Please sign in to comment.