Fixed posting and many style fixes
authorMoritz Kiefer <moritz.kiefer@gmail.com>
Fri, 29 Mar 2013 23:33:18 +0000 (00:33 +0100)
committerMoritz Kiefer <moritz.kiefer@gmail.com>
Fri, 29 Mar 2013 23:33:18 +0000 (00:33 +0100)
diaspy/client.py
diaspy/conversations.py
diaspy/models.py
setup.py
tests.py

index aae0681978c1d9d69c003028d9827b7b90a07889..b558c4da2c7d41998cccb3a7d0d8c6b042f53c7f 100644 (file)
@@ -35,19 +35,17 @@ class Client:
         return token
 
     def _setlogindata(self, username, password):
-        """This function is used to set data for login. 
-        
-        .. note::
+        """This function is used to set data for login.
+
+         .. note::
             It should be called before _login() function.
         """
         #r = self.session.get(self.pod + '/users/sign_in')
         #token = self._token_regex.search(r.text).group(1)
         self._username, self._password = username, password
-        self._login_data =  {
-                            'user[username]': self._username,
+        self._login_data = {'user[username]': self._username,
                             'user[password]': self._password,
-                            'authenticity_token': self.get_token(),
-                            }
+                            'authenticity_token': self.get_token()}
 
     def _login(self):
         """This function is used to connect to the pod and log in.
@@ -55,21 +53,23 @@ class Client:
         r = self.session.post('{0}/users/sign_in'.format(self.pod),
                               data=self._login_data,
                               headers={'accept': 'application/json'})
-        
-        if r.status_code != 201: raise Exception('{0}: Login failed.'.format(r.status_code))
 
-    def _setpostdata(self, text, aspect_id, photos):
+        if r.status_code != 201:
+            raise Exception('{0}: Login failed.'.format(r.status_code))
+
+    def _setpostdata(self, text, aspect_ids, photos):
         """This function prepares data for posting.
+
         :param text: Text to post.
         :type text: str
-        :param aspect_id: Aspect id to send post to.
-        :type aspect_id: str
+        :param aspect_ids: Aspect ids to send post to.
+        :type aspect_ids: str
         """
         data = {}
-        data['aspect_id'] = aspect_id
+        data['aspect_ids'] = aspect_ids
         data['status_message'] = {'text': text}
-        if photos: data['photos'] = photos
+        if photos:
+            data['photos'] = photos
         self._post_data = data
 
     def _post(self):
@@ -82,21 +82,23 @@ class Client:
                               headers={'content-type': 'application/json',
                                        'accept': 'application/json',
                                        'x-csrf-token': self.get_token()})
-        if r.status_code != 201: raise Exception('{0}: Post could not be posted.'.format(r.status_code))
+        if r.status_code != 201:
+            raise Exception('{0}: Post could not be posted.'.format(
+                            r.status_code))
 
         return diaspy.models.Post(str(r.json()['id']), self)
 
-    def post(self, text, aspect_id='public', photos=None):
+    def post(self, text, aspect_ids='public', photos=None):
         """This function sends a post to an aspect
 
         :param text: Text to post.
         :type text: str
-        :param aspect_id: Aspect id to send post to.
-        :type aspect_id: str
+        :param aspect_ids: Aspect ids to send post to.
+        :type aspect_ids: str
 
         :returns: diaspy.models.Post -- the Post which has been created
         """
-        self._setpostdata(text, aspect_id, photos)
+        self._setpostdata(text, aspect_ids, photos)
         post = self._post()
         self._post_data = {}
         return post
@@ -144,14 +146,13 @@ class Client:
 
         """
 
-        data = {'authenticity_token': self.get_token()}
         r = self.session.get('{0}/stream.json'.format(self.pod))
 
         if r.status_code != 200:
             raise Exception('wrong status code: {0}'.format(r.status_code))
 
         stream = r.json()
-        posts = [ diaspy.models.Post(str(post['id']), self) for post in stream ]
+        posts = [diaspy.models.Post(str(post['id']), self) for post in stream]
 
         return posts
 
@@ -162,7 +163,6 @@ class Client:
 
         """
 
-        data = {'authenticity_token': self.get_token()}
         r = self.session.get('{0}/notifications.json'.format(self.pod))
 
         if r.status_code != 200:
@@ -179,14 +179,14 @@ class Client:
 
         """
 
-        data = {'authenticity_token': self.get_token()}
         r = self.session.get('/mentions.json'.format(self.pod))
 
         if r.status_code != 200:
             raise Exception('wrong status code: {0}'.format(r.status_code))
 
         mentions = r.json()
-        posts = [ diaspy.models.Post(str(post['id']), self) for post in mentions ]
+        posts = [diaspy.models.Post(str(post['id']), self) for
+                 post in mentions]
 
         return posts
 
@@ -199,14 +199,14 @@ class Client:
 
         """
 
-        data = {'authenticity_token': self.get_token()}
         r = self.session.get('{0}/tags/{1}.json'.format(self.pod, tag))
 
         if r.status_code != 200:
             raise Exception('wrong status code: {0}'.format(r.status_code))
 
         tagged_posts = r.json()
-        posts = [ diaspy.models.Post(str(post['id']), self) for post in tagged_posts ]
+        posts = [diaspy.models.Post(str(post['id']), self) for
+                 post in tagged_posts]
 
         return posts
 
@@ -245,7 +245,8 @@ class Client:
                 'aspect_id': aspect_id,
                 'person_id': user_id}
 
-        r = self.session.delete('{0}/aspect_memberships/42.json'.format(self.pod),
+        r = self.session.delete('{0}/aspect_memberships/42.json'.format(
+                                self.pod),
                                 data=data)
 
         if r.status_code != 200:
@@ -286,14 +287,15 @@ class Client:
 
         """
 
-        data = {'authenticity_token': self.get_token()}
         r = self.session.get('{0}/conversations.json'.format(self.pod))
 
         if r.status_code != 200:
             raise Exception('wrong status code: {0}'.format(r.status_code))
 
         mailbox = r.json()
-        conversations = [ diaspy.conversations.Conversation(str(conversation['conversation']['id']), self) for conversation in mailbox ]
+        conversations = [diaspy.conversations.Conversation(
+                         str(conversation['conversation']['id']), self) for
+                         conversation in mailbox]
 
         return conversations
 
@@ -319,6 +321,7 @@ class Client:
                               data=data,
                               headers={'accept': 'application/json'})
         if r.status_code != 200:
-            raise Exception('{0}: Conversation could not be started.'.format(r.status_code))
+            raise Exception('{0}: Conversation could not be started.'
+                            .format(r.status_code))
 
         return r.json()
index 22afe551f7aac8a08a8daa5c4e9e60e6fd7f31b0..0bf4b62fb97f6472a6f969649d48179ce18de123 100644 (file)
@@ -1,5 +1,3 @@
-import requests
-
 class Conversation:
     """This class represents a conversation.
 
@@ -25,7 +23,8 @@ class Conversation:
     def get_data(self):
         """ returns the plain json data representing conversation.
         """
-        r = self._client.session.get('{0}/conversations/{1}.json'.format(self._client.pod, self.conv_id))
+        r = self._client.session.get('{0}/conversations/{1}.json'
+                                     .format(self._client.pod, self.conv_id))
         if r.status_code == 200:
             return r.json()['conversation']
         else:
@@ -43,11 +42,13 @@ class Conversation:
                 'utf8': '&#x2713;',
                 'authenticity_token': self._client.get_token()}
 
-        r = self._client.session.post('{0}/conversations/{1}/messages'.format(self._client.pod, self.conv_id),
+        r = self._client.session.post('{0}/conversations/{1}/messages'
+                                      .format(self._client.pod, self.conv_id),
                                       data=data,
                                       headers={'accept': 'application/json'})
         if r.status_code != 200:
-            raise Exception('{0}: Answer could not be posted.'.format(r.status_code))
+            raise Exception('{0}: Answer could not be posted.'
+                            .format(r.status_code))
 
         return r.json()
 
@@ -57,12 +58,15 @@ class Conversation:
         """
         data = {'authenticity_token': self._client.get_token()}
 
-        r = self._client.session.delete('{0}/conversations/{1}/visibility/'.format(self._client.pod, self.conv_id),
+        r = self._client.session.delete('{0}/conversations/{1}/visibility/'
+                                        .format(self._client.pod,
+                                                self.conv_id),
                                         data=data,
                                         headers={'accept': 'application/json'})
 
         if r.status_code != 404:
-            raise Exception('{0}: Conversation could not be deleted.'.format(r.status_code))
+            raise Exception('{0}: Conversation could not be deleted.'
+                            .format(r.status_code))
 
     def get_subject(self):
         """ return the subject of this conversation
index f04328e86fd7cfe153510f61a996a2a3f0929fd4..95e2043040d56d75a17923f617552702ca95e783 100644 (file)
@@ -1,6 +1,3 @@
-import requests
-
-
 class Post:
     """This class represents a post.
 
@@ -22,14 +19,15 @@ class Post:
     def get_data(self):
         """This function retrieves data of the post.
         """
-        r = self._client.session.get('{0}/posts/{1}.json'.format(self._client.pod, self.post_id))
-        if r.status_code == 200: 
+        r = self._client.session.get('{0}/posts/{1}.json'
+                                     .format(self._client.pod, self.post_id))
+        if r.status_code == 200:
             return r.json()
-        else: 
+        else:
             raise Exception('wrong status code: {0}'.format(r.status_code))
 
     def like(self):
-        """This function likes a post. 
+        """This function likes a post.
         It abstracts the 'Like' functionality.
 
         :returns: dict -- json formatted like object.
@@ -37,12 +35,14 @@ class Post:
         """
         data = {'authenticity_token': self._client.get_token()}
 
-        r = self._client.session.post('{0}/posts/{1}/likes'.format(self._client.pod, self.post_id),
+        r = self._client.session.post('{0}/posts/{1}/likes'
+                                      .format(self._client.pod, self.post_id),
                                       data=data,
                                       headers={'accept': 'application/json'})
 
         if r.status_code != 201:
-            raise Exception('{0}: Post could not be liked.'.format(r.status_code))
+            raise Exception('{0}: Post could not be liked.'
+                            .format(r.status_code))
 
         return r.json()
 
@@ -54,11 +54,16 @@ class Post:
 
         post_data = self.get_data()
 
-        r = self._client.session.delete('{0}/posts/{1}/likes/{2}'.format(self._client.pod, self.post_id, post_data['interactions']['likes'][0]['id']),
+        r = self._client.session.delete('{0}/posts/{1}/likes/{2}'
+                                        .format(self._client.pod,
+                                                self.post_id,
+                                                post_data['interactions']
+                                                         ['likes'][0]['id']),
                                         data=data)
 
         if r.status_code != 204:
-            raise Exception('{0}: Like could not be removed.'.format(r.status_code))
+            raise Exception('{0}: Like could not be removed.'
+                            .format(r.status_code))
 
     def reshare(self):
         """This function reshares a post
@@ -74,7 +79,8 @@ class Post:
                                       headers={'accept': 'application/json'})
 
         if r.status_code != 201:
-            raise Exception('{0}: Post could not be reshared.'.format(r.status_code))
+            raise Exception('{0}: Post could not be reshared.'
+                            .format(r.status_code))
 
         return r.json()
 
@@ -88,12 +94,14 @@ class Post:
         data = {'text': text,
                 'authenticity_token': self._client.get_token()}
 
-        r = self._client.session.post('{0}/posts/{1}/comments'.format(self._client.pod, self.post_id),
+        r = self._client.session.post('{0}/posts/{1}/comments'
+                                      .format(self._client.pod, self.post_id),
                                       data=data,
                                       headers={'accept': 'application/json'})
 
         if r.status_code != 201:
-            raise Exception('{0}: Comment could not be posted.'.format(r.status_code))
+            raise Exception('{0}: Comment could not be posted.'
+                            .format(r.status_code))
 
         return r.json()
 
@@ -106,12 +114,16 @@ class Post:
         """
         data = {'authenticity_token': self._client.get_token()}
 
-        r = self._client.session.delete('{0}/posts/{1}/comments/{2}'.format(self._client.pod, self.post_id, comment_id),
+        r = self._client.session.delete('{0}/posts/{1}/comments/{2}'
+                                        .format(self._client.pod,
+                                                self.post_id,
+                                                comment_id),
                                         data=data,
                                         headers={'accept': 'application/json'})
 
         if r.status_code != 204:
-            raise Exception('{0}: Comment could not be deleted.'.format(r.status_code))
+            raise Exception('{0}: Comment could not be deleted.'
+                            .format(r.status_code))
 
     def delete(self):
         """ This function deletes this post
@@ -119,6 +131,11 @@ class Post:
         """
         data = {'authenticity_token': self._client.get_token()}
 
-        r = self._client.session.delete('{0}/posts/{1}'.format(self._client.pod, self.post_id),
+        r = self._client.session.delete('{0}/posts/{1}'
+                                        .format(self._client.pod,
+                                                self.post_id),
                                         data=data,
                                         headers={'accept': 'application/json'})
+        if r.status_code != 204:
+            raise Exception('{0}: Post could not be deleted.'
+                            .format(r.status_code))
index d2f5a3c8483920987484d108cfba217ea6585138..2fad8a63f8ec6ab1b92034ee16879ca629eae859 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -3,6 +3,6 @@ setup(name='diaspy',
       version='0.0.1',
       author='Moritz Kiefer',
       author_email='moritz.kiefer@gmail.com',
-      packages = find_packages(),
+      packages=find_packages(),
       install_requires=['requests']
       )
index d64b235fdbb325aea7dc52d0e31d89a400bb4e72..cc48bcc3faa2795ed1265c635117d280a00f96e8 100644 (file)
--- a/tests.py
+++ b/tests.py
@@ -5,7 +5,8 @@ import getpass
 
 #   failure to import any of the modules below indicates failed tests
 #   modules used by diaspy
-import requests, re
+import requests
+import re
 #   actual diaspy code
 import diaspy
 
@@ -21,23 +22,27 @@ class ClientTests(unittest.TestCase):
     def testInitialization(self):
         """This test checks initialization of Client() instance.
         """
-        client = diaspy.client.Client(pod=__pod__, username=__username__, password=__passwd__)
+        client = diaspy.client.Client(pod=__pod__,
+                                      username=__username__,
+                                      password=__passwd__)
         self.assertEqual(__pod__, client.pod)
         self.assertEqual(__username__, client._username)
         self.assertEqual(__passwd__, client._password)
         self.assertEqual(None, client._post_data)
-        self.assertEqual(client._token_regex, re.compile(r'content="(.*?)"\s+name="csrf-token'))
+        self.assertEqual(client._token_regex,
+                         re.compile(r'content="(.*?)"\s+name="csrf-token'))
         self.assertEqual(client._login_data['user[username]'], 'testuser')
         self.assertEqual(client._login_data['user[password]'], 'testpassword')
-        self.assertEqual(client._login_data['authenticity_token'], client.get_token())
+        self.assertEqual(client._login_data['authenticity_token'],
+                         client.get_token())
 
     def testPreparationOfPostData(self):
         """This test checks correctness of data set for posting.
         """
 
 
-    
-if __name__ == '__main__': 
+if __name__ == '__main__':
     __passwd__ = getpass.getpass(prompt='Password used for testing: ')
-    if __passwd__ == '': __passwd__ = 'testpassword'
+    if __passwd__ == '':
+        __passwd__ = 'testpassword'
     unittest.main()