Skip to content

Commit c9223c0

Browse files
committed
Merge branch 'master' of github.com:jamalex/notion-py
2 parents f449a53 + 4f4a6e2 commit c9223c0

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

README.md

+20-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ result = cv.build_query(sort=sort_params).execute()
201201
print("Sorted results, showing most valuable first:", result)
202202
```
203203

204-
Note: You can combine `filter`, `aggregate`, and `sort`. See more examples of queries by setting up complex views in Notion, and then inspecting `cv.get("query")`
204+
Note: You can combine `filter`, `aggregate`, and `sort`. See more examples of queries by setting up complex views in Notion, and then inspecting the full query: `cv.get("query2")`.
205205

206206
You can also see [more examples in action in the smoke test runner](https://github.com/jamalex/notion-py/blob/master/notion/smoke_test.py). Run it using:
207207

@@ -226,6 +226,24 @@ page.locked = True
226226
page.locked = False
227227
```
228228

229+
## Example: Set the current user for multi-account user
230+
231+
```python
232+
from notion.client import NotionClient
233+
client = NotionClient(token_v2="<token_v2>")
234+
235+
# The initial current_user of a multi-account user may be an unwanted user
236+
print(client.current_user.email) #[email protected]
237+
238+
# Set current_user to the desired user
239+
client.set_user_by_email('[email protected]')
240+
print(client.current_user.email) #[email protected]
241+
242+
# You can also set the current_user by uid.
243+
client.set_user_by_uid('<uid>')
244+
print(client.current_user.email) #[email protected]
245+
```
246+
229247
# _Quick plug: Learning Equality needs your support!_
230248

231249
If you'd like to support notion-py development, please consider [donating to my open-source nonprofit, Learning Equality](https://learningequality.org/donate/), since when I'm not working on notion-py, it probably means I'm heads-down fundraising for our global education work (bringing resources like Khan Academy to communities with no Internet). COVID has further amplified needs, with over a billion kids stuck at home, and over half of them without the connectivity they need for distance learning. You can now also [support our work via GitHub Sponsors](https://github.com/sponsors/learningequality)!
@@ -234,6 +252,7 @@ If you'd like to support notion-py development, please consider [donating to my
234252

235253
- [md2notion](https://github.com/Cobertos/md2notion): import Markdown files to Notion
236254
- [notion-export-ics](https://github.com/evertheylen/notion-export-ics): Export Notion Databases to ICS calendar files
255+
- [notion-tqdm](https://github.com/shunyooo/notion-tqdm): Progress Bar displayed in Notion like tqdm
237256

238257
# TODO
239258

notion/client.py

+21
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,24 @@ def __init__(
9696

9797
def start_monitoring(self):
9898
self._monitor.poll_async()
99+
100+
def _fetch_guest_space_data(self, records):
101+
"""
102+
guest users have an empty `space` dict, so get the space_id from the `space_view` dict instead,
103+
and fetch the space data from the getPublicSpaceData endpoint.
104+
105+
Note: This mutates the records dict
106+
"""
107+
space_id = list(records["space_view"].values())[0]["value"]["space_id"]
108+
109+
space_data = self.post(
110+
"getPublicSpaceData", {"type": "space-ids", "spaceIds": [space_id]}
111+
).json()
112+
113+
records["space"] = {
114+
space["id"]: {"value": space} for space in space_data["results"]
115+
}
116+
99117

100118
def _set_token(self, email=None, password=None):
101119
if not email:
@@ -106,6 +124,9 @@ def _set_token(self, email=None, password=None):
106124

107125
def _update_user_info(self):
108126
records = self.post("loadUserContent", {}).json()["recordMap"]
127+
if not records["space"]:
128+
self._fetch_guest_space_data(records)
129+
109130
self._store.store_recordmap(records)
110131
self.current_user = self.get_user(list(records["notion_user"].keys())[0])
111132
self.current_space = self.get_space(list(records["space"].keys())[0])

0 commit comments

Comments
 (0)