From 66c3bb7661a5d1737d67476a26f1ca7b5b84a127 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Tue, 7 May 2013 22:57:12 +0200 Subject: [PATCH] Changes in `Stream().post_picture()` --- diaspy/models.py | 5 +++++ diaspy/streams.py | 29 ++++++++++++++++++++++++----- tests.py | 14 +++++--------- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/diaspy/models.py b/diaspy/models.py index 0a0fe64..aa28b70 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -18,6 +18,11 @@ class Post: self._connection = connection self.post_id = post_id + def __str__(self): + """Returns text of a post. + """ + return self.get_data['text'] + def get_data(self): """This function retrieves data of the post. """ diff --git a/diaspy/streams.py b/diaspy/streams.py index d0246a7..778876c 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -163,11 +163,13 @@ class Stream(Generic): post = Post(str(request.json()['id']), self._connection) return post - def post_picture(self, filename): - """This method posts a picture to D*. + def _photoupload(self, filename): + """Uploads picture to the pod. - :param filename: Path to picture file. + :param filename: path to picture file :type filename: str + + :returns: id of the photo being uploaded """ data = open(filename, 'rb') image = data.read() @@ -187,8 +189,25 @@ class Stream(Generic): request = self._connection.post('photos', data=image, params=params, headers=headers) if request.status_code != 200: - raise Exception('wrong error code: {0}'.format()) - return request + raise Exception('wrong error code: {0}'.format(request.status_code)) + return request.json()['data']['photo']['id'] + + def _photopost(self, id, text, aspect_ids): + """Posts a photo after it has been uploaded. + """ + post = self.post(text=text, aspect_ids=aspect_ids, photos=id) + return post + + def post_picture(self, filename, text='', aspect_ids='public'): + """This method posts a picture to D*. + + :param filename: path to picture file + :type filename: str + + :returns: Post object + """ + id = self._photoupload(filename) + return self._photopost(id, text, aspect_ids) class Activity(Generic): diff --git a/tests.py b/tests.py index 92b0f61..dee4158 100644 --- a/tests.py +++ b/tests.py @@ -37,6 +37,8 @@ test_connection = diaspy.connection.Connection(pod=__pod__, username=__username_ test_connection.login() print('[ CONNECTED ]\n') +post_text = '#diaspy test no. {0}'.format(test_count) + #### Test suite code class StreamTest(unittest.TestCase): @@ -62,12 +64,12 @@ class StreamTest(unittest.TestCase): def testPostingText(self): stream = diaspy.streams.Stream(test_connection) - post = stream.post('#diaspy test no. {0}'.format(test_count)) + post = stream.post(post_text) self.assertEqual(diaspy.models.Post, type(post)) def testPostingImage(self): stream = diaspy.streams.Stream(test_connection) - stream.post_picture('./test-image.png') + stream.post_picture('./test-image.png', post_text) def testingAddingTag(self): ft = diaspy.streams.FollowedTags(test_connection) @@ -100,15 +102,9 @@ class ClientTests(unittest.TestCase): self.assertEqual(list, type(notifications)) if notifications: self.assertEqual(dict, type(notifications[0])) - def testGettingTagAsList(self): + def testGettingTag(self): client = diaspy.client.Client(test_connection) tag = client.get_tag('foo') - self.assertEqual(list, type(tag)) - if tag: self.assertEqual(diaspy.models.Post, type(tag[0])) - - def testGettingTagAsStream(self): - client = diaspy.client.Client(test_connection) - tag = client.get_tag('foo', stream=True) self.assertEqual(diaspy.streams.Generic, type(tag)) if tag: self.assertEqual(diaspy.models.Post, type(tag[0])) -- 2.25.1