찬찬의 Notion API 정복기 13 - API 집중분석 - Update page

소찬 (Chan)·2022년 10월 3일
0
post-thumbnail

Create a page 를 한번 해보았다면 Update page 쯤은... 쉽게 할수 있지 않을까요?
하지만 친절한 찬찬은 찬찬히 Update page에 대해 설명해 드립니다.

우선 업데이트 하기 전에 걸림돌이 properties 입니다. 왜냐면 properties 가 제일 예민한 부분이기 때문에,
그래서 제가 제시하는 방법은 기존 properties 를 읽어온뒤 기존거를 고친 propertiesupdate를 진행하는 방법입니다.
page = notion.pages.retrieve(page_id=target_page_id)pprint로 찍어본 예시입니다.

...
 'properties': {'상태': {'id': '%3EQQn',
                       'select': {'color': 'brown',
                                  'id': '588ecfe9-05b0-40c2-aa32-3f26ee0b5281',
                                  'name': '★★★★'},
                       'type': 'select'},
                '이름': {'id': 'title',
                       'title': [{'annotations': {'bold': False,
                                                  'code': False,
                                                  'color': 'default',
                                                  'italic': False,
                                                  'strikethrough': False,
                                                  'underline': False},
                                  'href': None,
                                  'plain_text': '네오피자',
                                  'text': {'content': '네오피자', 'link': None},
                                  'type': 'text'}],
                       'type': 'title'}}

여기서 properties를 변수에 입력해줍니다.

properties_new = page['properties']

properties_new 에서 고유 id 로 보여지는 부분은 제거를 해줍니다.

del properties_new['상태']['id']
del properties_new['상태']['select']['id']

그리고 나는 '상태'만 업데이트 할꺼야 라고 하시면 '이름' property 는 제거해주셔도 되겠죠?

del properties_new['이름']

이제 별점을 바꿔보겠습니다. 5점 만점으로 바꿔볼께요.

properties_new['상태']['select']['name'] = '★★★★★'

주의하실 점은 color가 들어간 property는 기존에 설정된 값 대로 넣어줘야 된다는 점입니다.
가령 별 다섯개가 색상이 'green' 이다, 그런데 'orange'로 넣게되면 API 에서 오류를 리턴합니다.

properties_new['상태']['select']['name'] = '★★★★★'
properties_new['상태']['select']['color'] = 'green'

이제 업데이트를 해보겠습니다.

notion.pages.update(page_id=target_page_id, properties=properties_new)

그러면은, 궁금한게 properties 에서 미리 지정되지 않은 값을 설정하고 update 하면 어떻게 되나요? 가령 이렇게요.

properties_new['상태']['select']['name'] = '★★★★★★'
properties_new['상태']['select']['color'] = 'orange'

기존에는 별 여섯개 짜리가 없는데, 위와 같이 설정하고 update를 했다면,
별 여섯개에 오렌지 색상의 분류 항목이 추가로 생성됩니다. 이런식으로 말이죠.

나머지 parameter 값들도 살펴보겠습니다.

  • archived (boolean)
    Set to true to archive (delete) a page. Set to false to un-archive (restore) a page.
    페이지는 삭제하실 수 있는데, 삭제하면 바로 지워지는게 아니고 휴지통으로 들어갑니다. archive를 True로 값을 주면 page가 휴지통으로 이동됩니다. 역으로 False로 바꾸시면 휴지통에 있던 Page가 복원됩니다. 휴지통에 페이지 최장 보관기간은 30일 입니다.

  • icon (json)
    Page icon for the new page.
    아이콘은 페이지 앞에 붙는 Emoji 아이콘을 뜻합니다.

icon_value = {'emoji': '🍕', 'type': 'emoji'}
notion.pages.create(parent=parent_object, properties=properties_new, icon=icon_value)
  • cover (json)
    Page cover for the new page
    커버 추가 예시입니다.
url = 'https://images.unsplash.com/photo-1566843972142-a7fcb70de55a?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&cs=srgb&ixid=eyJhcHBfaWQiOjYzOTIxfQ'
cover_value = {'external': {'url': url},'type': 'external'}
notion.pages.create(parent=parent_object, properties=properties_new, cover=cover_value)
profile
QA Specialist

1개의 댓글

comment-user-thumbnail
2024년 3월 27일

노션API로 데이터베이스 자동 업데이트를 시키려고 하다가 업데이트 과정에서 자꾸 충돌나는 속성이 생겨서 머리가 아팠는데, 포스팅하신 내용 보고 충돌나는 속성은 그냥 업데이트 대상에서 제거해버리면 되는걸 배워서 해결했습니다!! 감사합니다~

답글 달기