Support more custom field types
This commit is contained in:
parent
9d54744859
commit
9b7a95d6e4
|
@ -173,7 +173,7 @@ class CustomFieldInfo:
|
||||||
type=json['type'],
|
type=json['type'],
|
||||||
name=json['name'],
|
name=json['name'],
|
||||||
enabled=json['enabled'],
|
enabled=json['enabled'],
|
||||||
custom_field_items=[CustomFieldItem.from_json(f) for f in json['customFieldItems']],
|
custom_field_items=[CustomFieldItem.from_json(f) for f in json.get('customFieldItems', [])],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -181,17 +181,21 @@ class CustomFieldInfo:
|
||||||
class CustomField:
|
class CustomField:
|
||||||
custom_field_id: CustomFieldId
|
custom_field_id: CustomFieldId
|
||||||
value: list[CustomFieldItemId] | CustomFieldItemId
|
value: list[CustomFieldItemId] | CustomFieldItemId
|
||||||
|
color: str
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_json(json: dict[str, Any]) -> 'CustomField':
|
def from_json(json: dict[str, Any]) -> 'CustomField':
|
||||||
value = json['value']
|
value = json.get('value')
|
||||||
if isinstance(value, str):
|
if value is None:
|
||||||
|
typed_value = []
|
||||||
|
elif isinstance(value, str):
|
||||||
typed_value = CustomFieldItemId(value)
|
typed_value = CustomFieldItemId(value)
|
||||||
else:
|
else:
|
||||||
typed_value = [CustomFieldItemId(v) for v in value]
|
typed_value = [CustomFieldItemId(v) for v in value]
|
||||||
return CustomField(
|
return CustomField(
|
||||||
custom_field_id = CustomFieldId(json['customFieldId']),
|
custom_field_id = CustomFieldId(json['customFieldId']),
|
||||||
value = typed_value,
|
value = typed_value,
|
||||||
|
color = json.get('color'),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -258,9 +262,11 @@ class Card:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def from_json(json: dict[str, Any]) -> 'Card':
|
def from_json(json: dict[str, Any]) -> 'Card':
|
||||||
|
card_id = CardId(json['cardId'])
|
||||||
|
seq_id = SeqId(json['sequentialId'])
|
||||||
return Card(
|
return Card(
|
||||||
card_id=CardId(json['cardId']),
|
card_id=card_id,
|
||||||
seq_id=SeqId(json['sequentialId']),
|
seq_id=seq_id,
|
||||||
common_id=CardCommonId(json['cardCommonId']),
|
common_id=CardCommonId(json['cardCommonId']),
|
||||||
detailed_description=json.get('detailedDescription'),
|
detailed_description=json.get('detailedDescription'),
|
||||||
is_archived=json['archived'],
|
is_archived=json['archived'],
|
||||||
|
|
|
@ -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, CustomFieldInfo, CustomFieldItemId
|
from .favro_data_model import Card, SeqId, CustomFieldInfo, CustomFieldItemId, CustomField
|
||||||
from .favro_markdown import CardContents, CardFileFormatter
|
from .favro_markdown import CardContents, CardFileFormatter
|
||||||
|
|
||||||
logger = getLogger(__name__)
|
logger = getLogger(__name__)
|
||||||
|
@ -58,18 +58,21 @@ class CardFileSystemItem(FileSystemItem):
|
||||||
seq_id: SeqId
|
seq_id: SeqId
|
||||||
|
|
||||||
|
|
||||||
def to_custom_field_value(value: CustomFieldItemId | list[CustomFieldItemId], field_def: CustomFieldInfo) -> str:
|
def to_custom_field_value(custom_field: CustomField, field_def: CustomFieldInfo) -> str:
|
||||||
if field_def.type == 'Single select':
|
value: CustomFieldItemId | list[CustomFieldItemId] = custom_field.value
|
||||||
|
if field_def.type in {'Single select','Multiple select'}:
|
||||||
items = [field_def.get_field_item(item_id) for item_id in value]
|
items = [field_def.get_field_item(item_id) for item_id in value]
|
||||||
items = [i for i in items if i]
|
items = [i for i in items if i]
|
||||||
return items[0].name
|
return items[0].name
|
||||||
|
if field_def.type in {'Color'}:
|
||||||
|
return custom_field.color
|
||||||
assert False, 'Unknown type: ' + field_def.type
|
assert False, 'Unknown type: ' + field_def.type
|
||||||
|
|
||||||
def to_custom_fields(card: Card, favro_client: FavroClient) -> dict[str,str]:
|
def to_custom_fields(card: Card, favro_client: FavroClient) -> dict[str,str]:
|
||||||
custom_fields = {}
|
custom_fields = {}
|
||||||
for field_assignment in card.custom_fields:
|
for field_assignment in card.custom_fields:
|
||||||
field_def = favro_client.get_custom_field(field_assignment.custom_field_id)
|
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)
|
custom_fields[field_def.name] = to_custom_field_value(field_assignment, field_def)
|
||||||
del field_assignment
|
del field_assignment
|
||||||
return custom_fields
|
return custom_fields
|
||||||
|
|
||||||
|
@ -100,7 +103,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)
|
custom_fields=to_custom_fields(card, favro_client),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -102,6 +102,8 @@ class CardFileFormatter:
|
||||||
frontmatter_data[FM_KEY_DUE_DATE] = card.due_date
|
frontmatter_data[FM_KEY_DUE_DATE] = card.due_date
|
||||||
if card.start_date is not None:
|
if card.start_date is not None:
|
||||||
frontmatter_data[FM_KEY_DUE_DATE] = card.start_date
|
frontmatter_data[FM_KEY_DUE_DATE] = card.start_date
|
||||||
|
if len(card.custom_fields) > 0:
|
||||||
|
frontmatter_data |= card.custom_fields
|
||||||
|
|
||||||
# Card name
|
# Card name
|
||||||
ls = []
|
ls = []
|
||||||
|
@ -171,4 +173,5 @@ class CardFileFormatter:
|
||||||
is_archived=is_archived,
|
is_archived=is_archived,
|
||||||
start_date=start_date,
|
start_date=start_date,
|
||||||
due_date=due_date,
|
due_date=due_date,
|
||||||
|
custom_fields={}, # TODO
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
import requests_cache
|
||||||
from favro_sync import secrets
|
from favro_sync import secrets
|
||||||
from favro_sync.favro_client import FavroClient, OrganizationId, SeqId
|
from favro_sync.favro_client import FavroClient, OrganizationId, SeqId
|
||||||
|
|
||||||
|
@ -36,22 +37,28 @@ def test_get_archived_card():
|
||||||
assert card.is_archived
|
assert card.is_archived
|
||||||
|
|
||||||
|
|
||||||
|
@needs_secrets
|
||||||
|
def test_get_card_with_color_custom_field():
|
||||||
|
client = create_client()
|
||||||
|
card = client.get_card(SeqId(8779))
|
||||||
|
assert len(card.custom_fields) > 0
|
||||||
|
|
||||||
|
|
||||||
@needs_secrets
|
@needs_secrets
|
||||||
def test_get_cards():
|
def test_get_cards():
|
||||||
client = create_client()
|
client = create_client()
|
||||||
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():
|
||||||
|
session = requests_cache.CachedSession('output/test-http-cache.sqlite')
|
||||||
return FavroClient(
|
return FavroClient(
|
||||||
favro_org_id=OrganizationId(secrets.favro_org_id()),
|
favro_org_id=OrganizationId(secrets.favro_org_id()),
|
||||||
favro_username=secrets.favro_username(),
|
favro_username=secrets.favro_username(),
|
||||||
favro_password=secrets.favro_password(),
|
favro_password=secrets.favro_password(),
|
||||||
read_only=True,
|
read_only=True,
|
||||||
|
session=session,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user