1
0

custom fields WIP

This commit is contained in:
Jon Michael Aanes 2024-10-02 13:34:49 +02:00
parent 4dd1b79f54
commit 9d54744859
4 changed files with 50 additions and 12 deletions

View File

@ -9,6 +9,12 @@ import datetime
from typing import Any
def map_opt(mapper, value):
if value is not None:
return mapper(value)
return None
@dataclasses.dataclass(frozen=True)
class SeqId:
raw_id: int
@ -122,6 +128,13 @@ class CustomFieldItem:
custom_field_item_id: CustomFieldItemId
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)
class CustomFieldInfo:
@ -145,29 +158,40 @@ class CustomFieldInfo:
enabled: bool
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
def from_json(json: dict[str, Any]) -> 'CustomFieldInfo':
return CustomFieldInfo(
organization_id=OrganizationId(json['organizationId']),
custom_field_id=CustomFieldId(json['customFieldId']),
widget_common_id=WidgetCommonId(json['widgetCommonId']),
widget_common_id=map_opt(WidgetCommonId,json.get('widgetCommonId')),
type=json['type'],
name=json['name'],
enabled=json['enabled'],
custom_field_items=json['customFieldItems'],
custom_field_items=[CustomFieldItem.from_json(f) for f in json['customFieldItems']],
)
@dataclasses.dataclass(frozen=True)
class CustomField:
custom_field_id: CustomFieldId
value: list[str] | str
value: list[CustomFieldItemId] | CustomFieldItemId
@staticmethod
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(
CustomFieldId(json['customFieldId']),
json.get('value'),
custom_field_id = CustomFieldId(json['customFieldId']),
value = typed_value,
)
@ -261,9 +285,3 @@ class Card:
],
attachments=json['attachments'], # TODO
)
def map_opt(mapper, value):
if value is not None:
return mapper(value)
return None

View File

@ -8,7 +8,7 @@ from logging import getLogger
import fuse
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
logger = getLogger(__name__)
@ -58,6 +58,21 @@ class CardFileSystemItem(FileSystemItem):
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:
tags = [favro_client.get_tag(tag_id).name for tag_id in card.tags]
assignments = [
@ -85,6 +100,7 @@ def to_card_contents(card: Card, favro_client: FavroClient) -> str:
is_archived=card.is_archived,
start_date=card.start_date,
due_date=card.due_date,
custom_fields=to_custom_fields(card, favro_client)
)

View File

@ -35,6 +35,7 @@ class CardContents:
is_archived: bool
start_date: datetime.date | None
due_date: datetime.date | None
custom_fields: dict[str,str]
def format_obsidian_link(text: str) -> str:

View File

@ -42,6 +42,9 @@ def test_get_cards():
for card in client.get_cards(todo_list=True):
assert_valid_card(card)
if card.seq_id == SeqId(8723):
assert card.is_archived
def create_client():
return FavroClient(