From 0fa6bd2f08953c248745a0ef9b9521c564927e12 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Mon, 19 Feb 2024 20:09:03 -0800 Subject: [PATCH 1/5] Skip the channel owner when it's not present in response data When a user account is deleted, the channels which they owned may still exist without a user object associated as the owner. Issue #17 --- pnutpy/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pnutpy/models.py b/pnutpy/models.py index 0ed03f1..2afc3bc 100644 --- a/pnutpy/models.py +++ b/pnutpy/models.py @@ -292,7 +292,8 @@ class Channel(APIModel): @classmethod def from_response_data(cls, data, api=None): channel = super(Channel, cls).from_response_data(data, api) - channel.owner = User.from_response_data(channel.owner, api) + if channel.get('owner'): + channel.owner = User.from_response_data(channel.owner, api) return channel class File(APIModel): From 3d02a49d5451901e0d3fcf6c190fa0e336d3a368 Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Fri, 29 Nov 2024 09:58:31 -0800 Subject: [PATCH 2/5] update the refernce post used in unit tests --- tests/test_api.py | 11 +++++++---- tests/test_models.py | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 8f99325..72d7ba8 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -25,11 +25,11 @@ def test_post(self): post, meta = self.api.delete_post(post) - post, meta = self.api.repost_post(257434) - post, meta = self.api.unrepost_post(257434) + post, meta = self.api.repost_post(1309208) + post, meta = self.api.unrepost_post(1309208) - post, meta = self.api.bookmark_post(257434) - post, meta = self.api.unbookmark_post(257434) + post, meta = self.api.bookmark_post(1309208) + post, meta = self.api.unbookmark_post(1309208) posts, meta = self.api.get_posts(ids='1,2,3') self.assertEqual(len(posts), 3) @@ -49,7 +49,10 @@ def test_post(self): def test_user(self): display_name = u'tester %s' % (time.time()) + print(display_name) user, meta = self.api.get_user('me') + print(self.username) + print(user.username) self.assertEqual(self.username, user.username) old_name = user.name user.name = display_name diff --git a/tests/test_models.py b/tests/test_models.py index d19adfc..73e9a10 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -15,7 +15,7 @@ def test_post(self): post.bookmark() post.unbookmark() post.delete() - post, meta = self.api.get_post(257434) + post, meta = self.api.get_post(1309208) post.repost() post.unrepost() @@ -25,7 +25,7 @@ def test_user(self): user.name = new_display_name user.update_user() - self.assertEquals(user.name, new_display_name) + self.assertEqual(user.name, new_display_name) user, meta = self.api.get_user(9) user.follow_user() From 7f0c3d352c51f5ec1cf5bc56aeb58df76a928b4c Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Fri, 29 Nov 2024 10:04:22 -0800 Subject: [PATCH 3/5] Support for v1 of the pnut API Issue #18 --- pnutpy/api.py | 8 ++++---- setup.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pnutpy/api.py b/pnutpy/api.py index 9f3b537..ffedf64 100644 --- a/pnutpy/api.py +++ b/pnutpy/api.py @@ -29,7 +29,7 @@ class API(requests.Session): """ @classmethod - def build_api(cls, api_root='https://api.pnut.io/v0', access_token=None, verify_ssl=True, extra_headers=None): + def build_api(cls, api_root='https://api.pnut.io/v1', access_token=None, verify_ssl=True, extra_headers=None): api = cls() api.api_root = api_root if access_token: @@ -98,7 +98,7 @@ def request_json(self, method, *args, **kwargs): return self.request(method, *args, **kwargs) -re_path_template = re.compile('{\w+}') +re_path_template = re.compile(r'{\w+}') def bind_api_method(func_name, path, payload_type=None, payload_list=False, allowed_params=None, @@ -360,8 +360,8 @@ def run(self, *args, **kwargs): allowed_params=PAGINATION_PARAMS + CHANNEL_PARAMS, require_auth=True) -bind_api_method('num_unread_pm_channels', '/users/me/channels/num_unread/pm', payload_type=SimpleValueModel, - allowed_params=CHANNEL_PARAMS, require_auth=True) +bind_api_method('num_unread_pm_channels', '/users/me/channels/num_unread?channel_types=io.pnut.core.pm', + payload_type=SimpleValueModel, allowed_params=CHANNEL_PARAMS, require_auth=True) bind_api_method('update_channel', '/channels/{channel_id}', payload_type=Channel, method='PUT', diff --git a/setup.py b/setup.py index f7461a4..619cfe1 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup, find_packages setup(name='pnutpy', - version='0.4.0', + version='0.5.0', description='pnut.io API library for python', long_description=open('README.rst').read(), license='MIT', From e3007850da995412197e319e49a4a6c63eb55eeb Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Fri, 29 Nov 2024 10:38:25 -0800 Subject: [PATCH 4/5] remote print statements from testing --- tests/test_api.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 72d7ba8..c02ac11 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -49,10 +49,7 @@ def test_post(self): def test_user(self): display_name = u'tester %s' % (time.time()) - print(display_name) user, meta = self.api.get_user('me') - print(self.username) - print(user.username) self.assertEqual(self.username, user.username) old_name = user.name user.name = display_name From 920df362c990fab037a62fcb1e61709fea34fdee Mon Sep 17 00:00:00 2001 From: Morgan McMillian Date: Fri, 29 Nov 2024 13:35:25 -0800 Subject: [PATCH 5/5] Update channel model to reference user object if owner exists. --- pnutpy/models.py | 6 ++++-- tests/test_models.py | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pnutpy/models.py b/pnutpy/models.py index 2afc3bc..10d7505 100644 --- a/pnutpy/models.py +++ b/pnutpy/models.py @@ -292,8 +292,10 @@ class Channel(APIModel): @classmethod def from_response_data(cls, data, api=None): channel = super(Channel, cls).from_response_data(data, api) - if channel.get('owner'): - channel.owner = User.from_response_data(channel.owner, api) + if channel.get('user'): + channel.user = User.from_response_data(channel.user, api) + if channel.get('user_id'): + channel.user_id = int(channel.user_id) return channel class File(APIModel): diff --git a/tests/test_models.py b/tests/test_models.py index 73e9a10..22dbcfc 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -37,6 +37,15 @@ def test_user(self): user.block_user() user.unblock_user() + def test_channel(self): + # normal public channel + user_id = 9 + channel, meta = self.api.get_channel(85) + self.assertEqual(user_id, channel.user.id) + # channel where the owner user account has been deleted + user_id = 213 + channel, meta = self.api.get_channel(955) + self.assertEqual(user_id, channel.user_id) if __name__ == '__main__': unittest.main()