-
Notifications
You must be signed in to change notification settings - Fork 474
/
space.py
47 lines (38 loc) · 1.54 KB
/
space.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from .logger import logger
from .maps import property_map, field_map
from .records import Record
class Space(Record):
_table = "space"
child_list_key = "pages"
name = field_map("name")
domain = field_map("domain")
icon = field_map("icon")
@property
def pages(self):
# The page list includes pages the current user might not have permissions on, so it's slow to query.
# Instead, we just filter for pages with the space as the parent.
return self._client.search_pages_with_parent(self.id)
@property
def users(self):
user_ids = [permission["user_id"] for permission in self.get("permissions")]
self._client.refresh_records(notion_user=user_ids)
return [self._client.get_user(user_id) for user_id in user_ids]
def _str_fields(self):
return super()._str_fields() + ["name", "domain"]
def add_page(self, title, type="page", shared=False):
assert type in ["page", "collection_view_page"], "'type' must be one of 'page' or 'collection_view_page'"
if shared:
permissions = [{
"role": "editor",
"type": "space_permission",
}]
else:
permissions = [{
"role": "editor",
"type": "user_permission",
"user_id": self._client.current_user.id,
}]
page_id = self._client.create_record("block", self, type=type, permissions=permissions)
page = self._client.get_block(page_id)
page.title = title
return page