From 4882952fbea2c92730af5a438797fb27f1608389 Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Tue, 20 Aug 2013 14:33:52 +0200 Subject: [PATCH] See changes in Changelog --- Changelog.markdown | 16 +++++++++- diaspy/__init__.py | 2 +- diaspy/conversations.py | 70 ----------------------------------------- diaspy/messages.py | 32 +++++++++++++++++++ diaspy/models.py | 66 ++++++++++++++++++++++++++++++++++++++ tests.py | 16 +++------- 6 files changed, 119 insertions(+), 83 deletions(-) delete mode 100644 diaspy/conversations.py create mode 100644 diaspy/messages.py diff --git a/Changelog.markdown b/Changelog.markdown index edf3c1c..92eb643 100644 --- a/Changelog.markdown +++ b/Changelog.markdown @@ -21,7 +21,21 @@ up-to-date than manual and if conflicts appear they should follow the order: ---- -Version `0.3.2` (2013-08-): +#### Version `0.4.0` (2013-08-20): + +This release is **not backwards compatible with `0.3.x` line**! You'll have to check your code for corrections. +Also, this release if first to officially released fork version. + +* __dep__: `diaspy.client` is officially deprecated (will be removed in `0.4.1`), + +* __upd__: `diaspy.conversations` renamed to `diaspy.messages`, +* __udp__: `diaspy.conversations.Conversation` moved to `diaspy.models`, + +* __new__: `diaspy.messages.Mailbox()` object representing diaspora\* mailbox, + +---- + +Version `0.3.2` (2013-08-20): * __upd__: `diaspy.connection.getUserData()` raises `DiaspyError` when it cannot find user data, diff --git a/diaspy/__init__.py b/diaspy/__init__.py index b6f722a..1a7d7c4 100644 --- a/diaspy/__init__.py +++ b/diaspy/__init__.py @@ -1,7 +1,7 @@ import diaspy.connection as connection import diaspy.models as models import diaspy.streams as streams -import diaspy.conversations as conversations +import diaspy.messages as messages import diaspy.people as people import diaspy.notifications as notifications import diaspy.settings as settings diff --git a/diaspy/conversations.py b/diaspy/conversations.py deleted file mode 100644 index 0351106..0000000 --- a/diaspy/conversations.py +++ /dev/null @@ -1,70 +0,0 @@ -#!/usr/bin/env python3 - - -from diaspy import errors - - -class Conversation(): - """This class represents a conversation. - - .. note:: - Remember that you need to have access to the conversation. - """ - def __init__(self, connection, id, fetch=True): - """ - :param conv_id: id of the post and not the guid! - :type conv_id: str - :param connection: connection object used to authenticate - :type connection: connection.Connection - """ - self._connection = connection - self.id = id - self.data = {} - if fetch: self._fetch() - - def _fetch(self): - """Fetches JSON data representing conversation. - """ - request = self._connection.get('conversations/{}.json'.format(self.id)) - if request.status_code == 200: - self.data = request.json()['conversation'] - else: - raise errors.ConversationError('cannot download conversation data: {0}'.format(request.status_code)) - - def answer(self, text): - """Answer that conversation - - :param text: text to answer. - :type text: str - """ - data = {'message[text]': text, - 'utf8': '✓', - 'authenticity_token': repr(self._connection)} - - request = self._connection.post('conversations/{}/messages'.format(self.id), - data=data, - headers={'accept': 'application/json'}) - if request.status_code != 200: - raise errors.ConversationError('{0}: Answer could not be posted.' - .format(request.status_code)) - return request.json() - - def delete(self): - """Delete this conversation. - Has to be implemented. - """ - data = {'authenticity_token': repr(self._connection)} - - request = self._connection.delete('conversations/{0}/visibility/' - .format(self.id), - data=data, - headers={'accept': 'application/json'}) - - if request.status_code != 404: - raise errors.ConversationError('{0}: Conversation could not be deleted.' - .format(request.status_code)) - - def get_subject(self): - """Returns the subject of this conversation - """ - return self.data['subject'] diff --git a/diaspy/messages.py b/diaspy/messages.py new file mode 100644 index 0000000..a81afca --- /dev/null +++ b/diaspy/messages.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + + +from diaspy import errors, models + + +class Mailbox(): + """Object implementing diaspora* mailbox. + """ + def __init__(self, connection, fetch=True): + self._connection = connection + self._mailbox = [] + if fetch: self._fetch() + + def __len__(self): + return len(self._mailbox) + + def __iter__(self): + return iter(self._mailbox) + + def __getitem__(self, n): + return self._mailbox[n] + + def _fetch(self): + """This method will fetch messages from user's mailbox. + """ + request = self._connection.get('conversations.json') + + if request.status_code != 200: + raise errors.DiaspyError('wrong status code: {0}'.format(r.status_code)) + mailbox = request.json() + self._mailbox = [models.Conversation(self._connection, c['conversation']['id']) for c in mailbox] diff --git a/diaspy/models.py b/diaspy/models.py index 1d0156f..9df0ae7 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -209,6 +209,72 @@ class Notification(): self.data['unread'] = unread +class Conversation(): + """This class represents a conversation. + + .. note:: + Remember that you need to have access to the conversation. + """ + def __init__(self, connection, id, fetch=True): + """ + :param conv_id: id of the post and not the guid! + :type conv_id: str + :param connection: connection object used to authenticate + :type connection: connection.Connection + """ + self._connection = connection + self.id = id + self.data = {} + if fetch: self._fetch() + + def _fetch(self): + """Fetches JSON data representing conversation. + """ + request = self._connection.get('conversations/{}.json'.format(self.id)) + if request.status_code == 200: + self.data = request.json()['conversation'] + else: + raise errors.ConversationError('cannot download conversation data: {0}'.format(request.status_code)) + + def answer(self, text): + """Answer that conversation + + :param text: text to answer. + :type text: str + """ + data = {'message[text]': text, + 'utf8': '✓', + 'authenticity_token': repr(self._connection)} + + request = self._connection.post('conversations/{}/messages'.format(self.id), + data=data, + headers={'accept': 'application/json'}) + if request.status_code != 200: + raise errors.ConversationError('{0}: Answer could not be posted.' + .format(request.status_code)) + return request.json() + + def delete(self): + """Delete this conversation. + Has to be implemented. + """ + data = {'authenticity_token': repr(self._connection)} + + request = self._connection.delete('conversations/{0}/visibility/' + .format(self.id), + data=data, + headers={'accept': 'application/json'}) + + if request.status_code != 404: + raise errors.ConversationError('{0}: Conversation could not be deleted.' + .format(request.status_code)) + + def get_subject(self): + """Returns the subject of this conversation + """ + return self.data['subject'] + + class Comment(): """Represents comment on post. diff --git a/tests.py b/tests.py index 5e0adfa..83c9e39 100644 --- a/tests.py +++ b/tests.py @@ -60,18 +60,12 @@ class ConnectionTest(unittest.TestCase): self.assertEqual(dict, type(info)) -class ClientTests(unittest.TestCase): - def testGettingTag(self): - client = dclient.Client(test_connection) - tag = client.get_tag('foo') - self.assertEqual(diaspy.streams.Generic, type(tag)) - if tag: self.assertEqual(diaspy.models.Post, type(tag[0])) - +class MessagesTests(unittest.TestCase): def testGettingMailbox(self): - client = dclient.Client(test_connection) - mailbox = client.get_mailbox() - self.assertEqual(list, type(mailbox)) - self.assertEqual(diaspy.conversations.Conversation, type(mailbox[0])) + mailbox = diaspy.messages.Mailbox(test_connection) + if mailbox: + for i in range(len(mailbox)): + self.assertEqual(diaspy.models.Conversation, type(mailbox[i])) class StreamTest(unittest.TestCase): -- 2.25.1