custom fields WIP
This commit is contained in:
parent
4dd1b79f54
commit
9d54744859
|
@ -9,6 +9,12 @@ import datetime
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
def map_opt(mapper, value):
|
||||||
|
if value is not None:
|
||||||
|
return mapper(value)
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
@dataclasses.dataclass(frozen=True)
|
||||||
class SeqId:
|
class SeqId:
|
||||||
raw_id: int
|
raw_id: int
|
||||||
|
@ -122,6 +128,13 @@ class CustomFieldItem:
|
||||||
custom_field_item_id: CustomFieldItemId
|
custom_field_item_id: CustomFieldItemId
|
||||||
name: str
|
name: str
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def from_json(json: dict[str, Any]) -> 'CustomFieldItem':
|
||||||
|
return CustomFieldItem(
|
||||||
|
custom_field_item_id=CustomFieldItemId(json['customFieldItemId']),
|
||||||
|
name=json['name'],
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
@dataclasses.dataclass(frozen=True)
|
||||||
class CustomFieldInfo:
|
class CustomFieldInfo:
|
||||||
|
@ -145,29 +158,40 @@ class CustomFieldInfo:
|
||||||
enabled: bool
|
enabled: bool
|
||||||
custom_field_items: list[CustomFieldItem]
|
custom_field_items: list[CustomFieldItem]
|
||||||
|
|
||||||
|
def get_field_item(self, field_item_id: CustomFieldItemId) -> CustomFieldItem | None:
|
||||||
|
for item in self.custom_field_items:
|
||||||
|
if item.custom_field_item_id == field_item_id:
|
||||||
|
return item
|
||||||
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_json(json: dict[str, Any]) -> 'CustomFieldInfo':
|
def from_json(json: dict[str, Any]) -> 'CustomFieldInfo':
|
||||||
return CustomFieldInfo(
|
return CustomFieldInfo(
|
||||||
organization_id=OrganizationId(json['organizationId']),
|
organization_id=OrganizationId(json['organizationId']),
|
||||||
custom_field_id=CustomFieldId(json['customFieldId']),
|
custom_field_id=CustomFieldId(json['customFieldId']),
|
||||||
widget_common_id=WidgetCommonId(json['widgetCommonId']),
|
widget_common_id=map_opt(WidgetCommonId,json.get('widgetCommonId')),
|
||||||
type=json['type'],
|
type=json['type'],
|
||||||
name=json['name'],
|
name=json['name'],
|
||||||
enabled=json['enabled'],
|
enabled=json['enabled'],
|
||||||
custom_field_items=json['customFieldItems'],
|
custom_field_items=[CustomFieldItem.from_json(f) for f in json['customFieldItems']],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(frozen=True)
|
@dataclasses.dataclass(frozen=True)
|
||||||
class CustomField:
|
class CustomField:
|
||||||
custom_field_id: CustomFieldId
|
custom_field_id: CustomFieldId
|
||||||
value: list[str] | str
|
value: list[CustomFieldItemId] | CustomFieldItemId
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_json(json: dict[str, Any]) -> 'CustomField':
|
def from_json(json: dict[str, Any]) -> 'CustomField':
|
||||||
|
value = json['value']
|
||||||
|
if isinstance(value, str):
|
||||||
|
typed_value = CustomFieldItemId(value)
|
||||||
|
else:
|
||||||
|
typed_value = [CustomFieldItemId(v) for v in value]
|
||||||
return CustomField(
|
return CustomField(
|
||||||
CustomFieldId(json['customFieldId']),
|
custom_field_id = CustomFieldId(json['customFieldId']),
|
||||||
json.get('value'),
|
value = typed_value,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -261,9 +285,3 @@ class Card:
|
||||||
],
|
],
|
||||||
attachments=json['attachments'], # TODO
|
attachments=json['attachments'], # TODO
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def map_opt(mapper, value):
|
|
||||||
if value is not None:
|
|
||||||
return mapper(value)
|
|
||||||
return None
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ from logging import getLogger
|
||||||
import fuse
|
import fuse
|
||||||
|
|
||||||
from .favro_client import FavroClient
|
from .favro_client import FavroClient
|
||||||
from .favro_data_model import Card, SeqId
|
from .favro_data_model import Card, SeqId, CustomFieldInfo, CustomFieldItemId
|
||||||
from .favro_markdown import CardContents, CardFileFormatter
|
from .favro_markdown import CardContents, CardFileFormatter
|
||||||
|
|
||||||
logger = getLogger(__name__)
|
logger = getLogger(__name__)
|
||||||
|
@ -58,6 +58,21 @@ class CardFileSystemItem(FileSystemItem):
|
||||||
seq_id: SeqId
|
seq_id: SeqId
|
||||||
|
|
||||||
|
|
||||||
|
def to_custom_field_value(value: CustomFieldItemId | list[CustomFieldItemId], field_def: CustomFieldInfo) -> str:
|
||||||
|
if field_def.type == 'Single select':
|
||||||
|
items = [field_def.get_field_item(item_id) for item_id in value]
|
||||||
|
items = [i for i in items if i]
|
||||||
|
return items[0].name
|
||||||
|
assert False, 'Unknown type: ' + field_def.type
|
||||||
|
|
||||||
|
def to_custom_fields(card: Card, favro_client: FavroClient) -> dict[str,str]:
|
||||||
|
custom_fields = {}
|
||||||
|
for field_assignment in card.custom_fields:
|
||||||
|
field_def = favro_client.get_custom_field(field_assignment.custom_field_id)
|
||||||
|
custom_fields[field_def.name] = to_custom_field_value(field_assignment.value, field_def)
|
||||||
|
del field_assignment
|
||||||
|
return custom_fields
|
||||||
|
|
||||||
def to_card_contents(card: Card, favro_client: FavroClient) -> str:
|
def to_card_contents(card: Card, favro_client: FavroClient) -> str:
|
||||||
tags = [favro_client.get_tag(tag_id).name for tag_id in card.tags]
|
tags = [favro_client.get_tag(tag_id).name for tag_id in card.tags]
|
||||||
assignments = [
|
assignments = [
|
||||||
|
@ -85,6 +100,7 @@ def to_card_contents(card: Card, favro_client: FavroClient) -> str:
|
||||||
is_archived=card.is_archived,
|
is_archived=card.is_archived,
|
||||||
start_date=card.start_date,
|
start_date=card.start_date,
|
||||||
due_date=card.due_date,
|
due_date=card.due_date,
|
||||||
|
custom_fields=to_custom_fields(card, favro_client)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@ class CardContents:
|
||||||
is_archived: bool
|
is_archived: bool
|
||||||
start_date: datetime.date | None
|
start_date: datetime.date | None
|
||||||
due_date: datetime.date | None
|
due_date: datetime.date | None
|
||||||
|
custom_fields: dict[str,str]
|
||||||
|
|
||||||
|
|
||||||
def format_obsidian_link(text: str) -> str:
|
def format_obsidian_link(text: str) -> str:
|
||||||
|
|
|
@ -42,6 +42,9 @@ def test_get_cards():
|
||||||
for card in client.get_cards(todo_list=True):
|
for card in client.get_cards(todo_list=True):
|
||||||
assert_valid_card(card)
|
assert_valid_card(card)
|
||||||
|
|
||||||
|
if card.seq_id == SeqId(8723):
|
||||||
|
assert card.is_archived
|
||||||
|
|
||||||
|
|
||||||
def create_client():
|
def create_client():
|
||||||
return FavroClient(
|
return FavroClient(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user