From a1a09a06037080703eb99e624064d9d31ccb6c04 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Fri, 29 Mar 2013 10:40:29 +0100 Subject: [PATCH] Separated post() procedure into data setting and sending --- diaspy/client.py | 48 +++++++++++++++++++++++++++++++++++------------- diaspy/models.py | 5 ----- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/diaspy/client.py b/diaspy/client.py index 0ec0074..7960eef 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -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' diff --git a/diaspy/models.py b/diaspy/models.py index f3aaf13..f04328e 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -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 -- 2.25.1