Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions plane/models/work_item_properties.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
from typing import Any
from typing import Annotated, Any, get_args

from pydantic import BaseModel, ConfigDict, Field, field_serializer, model_validator

from .enums import PropertyType, RelationType
from .work_item_property_configurations import (
DateAttributeSettings,
NumberAttributeSettings,
TextAttributeSettings,
)

# Settings type used by TEXT and DATETIME properties
PropertySettings = TextAttributeSettings | DateAttributeSettings | None
# Settings type used by TEXT, DATETIME and DECIMAL properties. Every `display_format`
# value is unique across the five models, so it picks the model on its own.
PropertySettings = (
Annotated[
TextAttributeSettings | DateAttributeSettings | NumberAttributeSettings,
Field(discriminator="display_format"),
]
| None
)

# The number settings models, unpacked from the NumberAttributeSettings union
_NUMBER_SETTINGS: tuple[type, ...] = get_args(get_args(NumberAttributeSettings)[0])

# `settings={}` on a DECIMAL property: the server resets it to the default format
_EmptySettings = Annotated[dict, Field(max_length=0)]


def _check_decimal_settings(settings: object) -> None:
"""DECIMAL settings are optional: none, or {}, means the server's default (standard, 2
places, thousand separator). When given, they must be a number display format."""
if settings is not None and settings != {} and not isinstance(settings, _NUMBER_SETTINGS):
raise ValueError(
"settings must be Standard/Percentage/CurrencyNumberAttributeSettings "
"for DECIMAL properties"
)


class WorkItemProperty(BaseModel):
Expand Down Expand Up @@ -63,7 +87,7 @@ class CreateWorkItemProperty(BaseModel):
property_type: PropertyType
is_required: bool | None = None
default_value: list[str] | None = None
settings: PropertySettings = None
settings: PropertySettings | _EmptySettings = None
is_active: bool | None = None
is_multi: bool | None = None
validation_rules: Any | None = None
Expand Down Expand Up @@ -104,6 +128,9 @@ def validate_settings_and_relation_type(self) -> "CreateWorkItemProperty":
if not isinstance(settings, DateAttributeSettings):
raise ValueError("settings must be DateAttributeSettings for DATETIME properties")

if prop_type == PropertyType.DECIMAL:
_check_decimal_settings(settings)

# RELATION properties require relation_type
if prop_type == PropertyType.RELATION:
if relation_type is None:
Expand All @@ -123,7 +150,7 @@ class UpdateWorkItemProperty(BaseModel):
property_type: PropertyType | None = None
is_required: bool | None = None
default_value: list[str] | None = None
settings: PropertySettings = None
settings: PropertySettings | _EmptySettings = None
is_active: bool | None = None
is_multi: bool | None = None
validation_rules: Any | None = None
Expand Down Expand Up @@ -169,6 +196,9 @@ def validate_settings_and_relation_type(self) -> "UpdateWorkItemProperty":
if not isinstance(settings, DateAttributeSettings):
raise ValueError("settings must be DateAttributeSettings for DATETIME properties")

if prop_type == PropertyType.DECIMAL:
_check_decimal_settings(settings)

# RELATION properties require relation_type
if prop_type == PropertyType.RELATION:
if relation_type is None:
Expand Down
51 changes: 48 additions & 3 deletions plane/models/work_item_property_configurations.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Literal

from pydantic import BaseModel, ConfigDict
from typing import Annotated, Literal

from pydantic import BaseModel, ConfigDict, Field

# -------------------------- LITERALS --------------------------

TextDisplayFormat = Literal["single-line", "multi-line", "readonly"]
DateDisplayFormat = Literal["MMM dd, yyyy", "dd/MM/yyyy", "MM/dd/yyyy", "yyyy/MM/dd"]
NumberDisplayFormat = Literal["standard", "percentage", "currency"]
DropdownAttributeKey = Literal["single_select", "multi_select"]
IconKey = Literal[
"AlignLeft", "Hash", "CircleChevronDown", "ToggleLeft", "Calendar", "UsersRound", "Link2"
Expand All @@ -33,6 +33,51 @@ class DateAttributeSettings(BaseModel):
display_format: DateDisplayFormat


class StandardNumberAttributeSettings(BaseModel):
"""Settings for DECIMAL properties displayed as plain numbers, e.g. 1,234.50."""

model_config = ConfigDict(extra="forbid", populate_by_name=True)

display_format: Literal["standard"]
decimal_places: int = Field(..., ge=0, le=4)
thousand_separator: bool


class PercentageNumberAttributeSettings(BaseModel):
"""Settings for DECIMAL properties displayed as percentages, e.g. 12.5%."""

model_config = ConfigDict(extra="forbid", populate_by_name=True)

display_format: Literal["percentage"]
decimal_places: int = Field(..., ge=0, le=4)


class CurrencyNumberAttributeSettings(BaseModel):
"""Settings for DECIMAL properties displayed as an amount of money, e.g. $1,234.50.

Only the shape of `currency_code` is checked here; the server checks it against
ISO 4217. Leave `decimal_places` unset to take the currency's own (2 for USD, 0
for JPY) -- the server rejects a value that doesn't match the currency.
"""

model_config = ConfigDict(extra="forbid", populate_by_name=True)

display_format: Literal["currency"]
currency_code: str = Field(..., pattern=r"^[A-Z]{3}$", description="ISO 4217, e.g. 'USD'")
decimal_places: int | None = Field(None, ge=0, le=4)


# Settings for DECIMAL properties, picked by `display_format`. The three models forbid
# unknown keys, as the server does, so a typo like `decimal_place` fails here instead of
# being dropped. A response with a key they don't know still reads, as a dict.
NumberAttributeSettings = Annotated[
StandardNumberAttributeSettings
| PercentageNumberAttributeSettings
| CurrencyNumberAttributeSettings,
Field(discriminator="display_format"),
]


# -------------------------- UI CONFIGURATIONS (optional helpers) --------------------------


Expand Down
Loading
Loading