Changes in `Stream().post_picture()`
authorMarek Marecki <triviuss@gmail.com>
Tue, 7 May 2013 20:57:12 +0000 (22:57 +0200)
committerMarek Marecki <triviuss@gmail.com>
Tue, 7 May 2013 20:57:12 +0000 (22:57 +0200)
diaspy/models.py
diaspy/streams.py
tests.py

index 0a0fe646afa01fc7fda2dfe3b6958dd0eb249d32..aa28b70ec5b1e60ac41b797a64a0dc824f8707e0 100644 (file)
@@ -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.
         """
index d0246a7737189632e0729bc4a360e5eee547060a..778876c1d5a5d72de7bdf930c532ccb3f4a5a447 100644 (file)
@@ -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):
index 92b0f61db74e6677fb6dc931e641f927fcea169b..dee4158e322515dff80252dcd0e43ce3b10e17ff 100644 (file)
--- 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]))