From a98c67925a78332edb0645c900e531f0c06e34fa Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Wed, 8 May 2013 00:01:24 +0200 Subject: [PATCH] One-to-rule-them-all version of `post()` method (with tests) --- diaspy/streams.py | 31 ++++++++++--------------------- tests.py | 4 ++-- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/diaspy/streams.py b/diaspy/streams.py index 778876c..195882b 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -137,28 +137,34 @@ class Stream(Generic): def _setlocation(self): self._location = 'stream.json' - def post(self, text, aspect_ids='public', photos=None): - """This function sends a post to an aspect + def post(self, text='', aspect_ids='public', photos=None, photo=''): + """This function sends a post to an aspect. + If both `photo` and `photos` are specified `photos` takes precedence. :param text: Text to post. :type text: str :param aspect_ids: Aspect ids to send post to. :type aspect_ids: str + :param photo: filename of photo to post + :type photo: str + :param photos: id of photo to post (obtained from _photoupload()) + :type photos: int :returns: diaspy.models.Post -- the Post which has been created """ data = {} data['aspect_ids'] = aspect_ids data['status_message'] = {'text': text} + if photo: data['photos'] = self._photoupload(photo) if photos: data['photos'] = photos + request = self._connection.post('status_messages', data=json.dumps(data), headers={'content-type': 'application/json', 'accept': 'application/json', 'x-csrf-token': self._connection.get_token()}) if request.status_code != 201: - raise Exception('{0}: Post could not be posted.'.format( - request.status_code)) + raise Exception('{0}: Post could not be posted.'.format(request.status_code)) post = Post(str(request.json()['id']), self._connection) return post @@ -192,23 +198,6 @@ class Stream(Generic): 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): """Stream representing user's activity. diff --git a/tests.py b/tests.py index dafbc01..007ecb9 100644 --- a/tests.py +++ b/tests.py @@ -105,12 +105,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(text=post_text, photo='test-image.png') def testingAddingTag(self): ft = diaspy.streams.FollowedTags(test_connection) -- 2.25.1