From eda2bcebe53b6d32190a4dee66fdf1ebe8d2e094 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Sat, 6 Apr 2013 11:52:06 +0200 Subject: [PATCH] _sessiondelete() method in diaspy/client.py implemented, delete action in other modules is done through this new method --- diaspy/client.py | 19 +++++++++++++++---- diaspy/conversations.py | 5 ++--- diaspy/models.py | 12 +++++------- tests.py | 4 ++-- tox.ini | 2 +- 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/diaspy/client.py b/diaspy/client.py index fcb12db..74a3769 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -57,6 +57,19 @@ class Client: else: r = self.session.post(string, data=data) return r + def _sessiondelete(self, string, data, headers={}): + """This method lets you send delete request to session. + Performs additional checks if needed. + + :param string: URL to use. + :type string: str + :param data: Data to use. + """ + string = '{0}/{1}'.format(self.pod, string) + if headers: r = self.session.delete(string, data=data, headers=headers) + else: r = self.session.delete(string, data=data) + return r + def get_token(self): """This function gets a token needed for authentication in most cases @@ -274,13 +287,11 @@ class Client: :type aspect_id: str """ - data = {'authenticity_token': self.get_token(), 'aspect_id': aspect_id, 'person_id': user_id} - r = self.session.delete('{0}/aspect_memberships/42.json'.format( - self.pod), + r = self._sessiondelete('aspect_memberships/42.json', data=data) if r.status_code != 200: @@ -293,7 +304,7 @@ class Client: """ data = {'authenticity_token': self.get_token()} - r = self.session.delete('{0}/aspects/{1}'.format(self.pod, aspect_id), + r = self._sessiondelete('aspects/{}'.format(aspect_id), data=data) if r.status_code != 404: diff --git a/diaspy/conversations.py b/diaspy/conversations.py index 862bdbb..631f77e 100644 --- a/diaspy/conversations.py +++ b/diaspy/conversations.py @@ -58,9 +58,8 @@ 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._sessiondelete('conversations/{0}/visibility/' + .format(self.conv_id), data=data, headers={'accept': 'application/json'}) diff --git a/diaspy/models.py b/diaspy/models.py index aa0d632..5c61185 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -52,9 +52,8 @@ class Post: post_data = self.get_data() - r = self._client.session.delete('{0}/posts/{1}/likes/{2}' - .format(self._client.pod, - self.post_id, + r = self._client._sessiondelete('posts/{0}/likes/{1}' + .format(self.post_id, post_data['interactions'] ['likes'][0]['id']), data=data) @@ -111,9 +110,8 @@ 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, + r = self._client._sessiondelete('posts/{0}/comments/{1}' + .format(self.post_id, comment_id), data=data, headers={'accept': 'application/json'}) @@ -126,7 +124,7 @@ class Post: """ This function deletes this 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._sessiondelete('posts/{0}'.format(self.post_id), data=data, headers={'accept': 'application/json'}) if r.status_code != 204: diff --git a/tests.py b/tests.py index 0415837..e3d6e2c 100644 --- a/tests.py +++ b/tests.py @@ -28,8 +28,8 @@ class ClientTests(unittest.TestCase): self.assertEqual({}, client._post_data) 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['user[username]'], __username__) + self.assertEqual(client._login_data['user[password]'], __passwd__) self.assertEqual(client._login_data['authenticity_token'], client.get_token()) diff --git a/tox.ini b/tox.ini index 00f8f44..99b7a88 100644 --- a/tox.ini +++ b/tox.ini @@ -1,3 +1,3 @@ [flake8] -ignore=E701 +ignore=E701,E226 max-line-length=120 -- 2.25.1