Separated post() procedure into data setting and sending
authorMarek Marecki <triviuss@gmail.com>
Fri, 29 Mar 2013 09:40:29 +0000 (10:40 +0100)
committerMarek Marecki <triviuss@gmail.com>
Fri, 29 Mar 2013 09:40:29 +0000 (10:40 +0100)
diaspy/client.py
diaspy/models.py

index 0ec0074eb63185074a8f56bb6f25483ccecbcdaa..7960eefe4e80605ddb43de4a3d3c44774769ef31 100644 (file)
@@ -20,9 +20,10 @@ class Client:
         """
         self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token')
         self.pod = pod
-        self.logged_in = False
         self.session = requests.Session()
+        self._post_data = None
         self._setlogindata(username, password)
+        self._login()
 
     def get_token(self):
         """This function gets a token needed for authentication in most cases
@@ -57,26 +58,28 @@ class Client:
                               headers={'accept': 'application/json'})
         
         if r.status_code != 201: raise Exception('{0}: Login failed.'.format(r.status_code))
-        else: self.logged_in = True
-    
-    def post(self, text, aspect_id='public', photos=None):
-        """This function sends a post to an aspect
-
+   
+    def _setpostdata(self, text, aspect_id, 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
+        """
+        data = {}
+        data['aspect_id'] = aspect_id
+        data['status_message'] = {'text': text}
+        if photos: data['photos'] = photos
+        self._post_data = data
 
-        :returns: diaspy.models.Post -- the Post which has been created
+    def _post(self):
+        """Sends post to an aspect.
 
+        :returns: diaspy.models.Post -- the Post which has been created
         """
-        data = {'aspect_ids': aspect_id,
-                'status_message': {'text': text}}
-
-        if photos:
-            data['photos'] = photos
         r = self.session.post('{0}/status_messages'.format(self.pod),
-                              data=json.dumps(data),
+                              data=json.dumps(self._post_data),
                               headers={'content-type': 'application/json',
                                        'accept': 'application/json',
                                        'x-csrf-token': self.get_token()})
@@ -84,6 +87,20 @@ class Client:
 
         return diaspy.models.Post(str(r.json()['id']), self)
 
+    def post(self, text, aspect_id='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
+
+        :returns: diaspy.models.Post -- the Post which has been created
+
+        """
+        self._setpostdata(text, aspect_id, photos)
+        return self._post()
+
     def get_user_info(self):
         """This function returns the current user's attributes.
 
@@ -96,6 +113,11 @@ class Client:
         return userdata
 
     def post_picture(self, filename):
+        """This method posts a picture to D*.
+
+        :param filename: Path to picture file.
+        :type filename: str
+        """
         aspects = self.get_user_info()['aspects']
         params = {}
         params['photo[pending]'] = 'true'
index f3aaf1313b3363b8f93d8a5e870059de967f2bfa..f04328e86fd7cfe153510f61a996a2a3f0929fd4 100644 (file)
@@ -15,11 +15,6 @@ class Post:
         :type post_id: str
         :param client: client object used to authenticate
         :type client: client.Client
-
-        .. note::
-            The login function of the client should be called,
-            before calling any of the post functions.
-
         """
         self._client = client
         self.post_id = post_id