From: Marek Marecki Date: Sat, 8 Jun 2013 23:06:59 +0000 (+0200) Subject: Work done to improve notifications X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=7a818fdb628b824e0588adedf40e808b263d2509;p=diaspy.git Work done to improve notifications --- diff --git a/Makefile b/Makefile index 34c92e1..4bb7617 100644 --- a/Makefile +++ b/Makefile @@ -10,5 +10,4 @@ test-python2: python2 -m unittest --verbose --catch --failfast tests.py clean: - rm -rv {*/,}__pycache__/ - rm -rv {*/,}*.pyc + rm -rv {*,.}/__pycache__/ diff --git a/diaspy/__init__.py b/diaspy/__init__.py index ace13e2..172431a 100644 --- a/diaspy/__init__.py +++ b/diaspy/__init__.py @@ -3,3 +3,4 @@ import diaspy.conversations as conversations import diaspy.streams as streams import diaspy.client as client import diaspy.people as people +import diaspy.notifications as notifications diff --git a/diaspy/models.py b/diaspy/models.py index 5d39226..28a16d6 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -1,6 +1,47 @@ #!/usr/bin/env python3 +class Aspect(): + """This class represents an aspect. + """ + def __init__(self, connection, id=-1): + self._connection = connection + self.id = id + self.name = '' + + def addUser(self, user_id): + """Add user to current aspect. + + :param user_id: user to add to aspect + :type user: int + """ + data = {'authenticity_token': self._connection.get_token(), + 'aspect_id': self.id, + 'person_id': user_id} + + request = self._connection.post('aspect_memberships.json', data=data) + + if request.status_code != 201: + raise Exception('wrong status code: {0}'.format(request.status_code)) + return request.json() + + def removeUser(self, user_id): + """Remove user from current aspect. + + :param user_id: user to remove from aspect + :type user: int + """ + data = {'authenticity_token': self._connection.get_token(), + 'aspect_id': self.id, + 'person_id': user_id} + + request = self.connection.delete('aspect_memberships/{0}.json'.format(self.id), data=data) + + if request.status_code != 200: + raise Exception('wrong status code: {0}'.format(request.status_code)) + return request.json() + + class Post(): """This class represents a post. @@ -136,62 +177,3 @@ class Post(): headers={'accept': 'application/json'}) if r.status_code != 204: raise Exception('{0}: Post could not be deleted'.format(r.status_code)) - - -class Aspect(): - """This class represents an aspect. - """ - def __init__(self, connection, id=-1): - self._connection = connection - self.id = id - self.name = '' - - def addUser(self, user_id): - """Add user to current aspect. - - :param user_id: user to add to aspect - :type user: int - """ - data = {'authenticity_token': self._connection.get_token(), - 'aspect_id': self.id, - 'person_id': user_id} - - request = self._connection.post('aspect_memberships.json', data=data) - - if request.status_code != 201: - raise Exception('wrong status code: {0}'.format(request.status_code)) - return request.json() - - def removeUser(self, user_id): - """Remove user from current aspect. - - :param user_id: user to remove from aspect - :type user: int - """ - data = {'authenticity_token': self._connection.get_token(), - 'aspect_id': self.id, - 'person_id': user_id} - - request = self.connection.delete('aspect_memberships/{0}.json'.format(self.id), data=data) - - if request.status_code != 200: - raise Exception('wrong status code: {0}'.format(request.status_code)) - return request.json() - - -class Notifications(): - """This class represents notifications of a user. - """ - def __init__(self, connection): - self._connection = connection - - def get(self): - """Returns list of notifications. - """ - request = self._connection.get('notifications.json') - - if r.status_code != 200: - raise Exception('status code: {0}: cannot retreive notifications'.format(r.status_code)) - - notifications = request.json() - return notifications diff --git a/diaspy/people.py b/diaspy/people.py index 79e2582..9377b48 100644 --- a/diaspy/people.py +++ b/diaspy/people.py @@ -97,8 +97,7 @@ class Contacts(): :param aspect_ids: list of aspect ids :type aspect_ids: list """ - for aid in aspect_ids: - Aspect(self._connection, aid).addUser(user_id) + for aid in aspect_ids: Aspect(self._connection, aid).addUser(user_id) def remove(self, user_id, aspect_ids): """Remove user from aspects of given ids. @@ -108,18 +107,19 @@ class Contacts(): :param aspect_ids: list of aspect ids :type aspect_ids: list """ - for aid in aspect_ids: - Aspect(self._connection, aid).removeUser(user_id) + for aid in aspect_ids: Aspect(self._connection, aid).removeUser(user_id) def get(self, set=''): """Returns list of user contacts. Contact is a User() who is in one or more of user's aspects. - By default, it will return list of users who are in logged - user aspects. + By default, it will return list of users who are in + user's aspects. + If `set` is `all` it will also include users who only share with logged user and are not in his/hers aspects. + If `set` is `only_sharing` it will return users who are only sharing with logged user and ARE NOT in his/hers aspects. diff --git a/diaspy/streams.py b/diaspy/streams.py index 64d5c84..e1cae15 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -10,9 +10,8 @@ http://pad.spored.de/ro/r.qWmvhSZg7rk4OQam """ -class Generic: - """Object representing generic stream. Used in Tag(), - Stream(), Activity() etc. +class Generic(): + """Object representing generic stream. """ _location = 'stream.json' _stream = [] @@ -348,3 +347,18 @@ class FollowedTags(Generic): if request.status_code not in [201, 403]: raise Exception('wrong error code: {0}'.format(request.status_code)) return request.status_code + + +class Tag(Generic): + """This stream contains all posts containing a tag. + """ + def __init__(connection, tag): + """ + :param connection: Connection() object + :type connection: diaspy.connection.Connection + :param tag: tag name + :type tag: str + """ + self._connection = connection + self._location = 'tags/{0}'.format(tag) + self.fill() diff --git a/manual/contacts.mdown b/manual/contacts.mdown new file mode 100644 index 0000000..e69de29 diff --git a/tests.py b/tests.py index a136ee5..66a54eb 100644 --- a/tests.py +++ b/tests.py @@ -178,20 +178,20 @@ class UserTests(unittest.TestCase): class ContactsTest(unittest.TestCase): def testGetOnlySharing(self): contacts = diaspy.people.Contacts(test_connection) - only_sharing = contacts.get_only_sharing() - for i in only_sharing: + result = contacts.get(set='only_sharing') + for i in result: self.assertEqual(diaspy.people.User, type(i)) def testGetAll(self): contacts = diaspy.people.Contacts(test_connection) - only_sharing = contacts.get_all() - for i in only_sharing: + result = contacts.get(set='all') + for i in result: self.assertEqual(diaspy.people.User, type(i)) def testGet(self): contacts = diaspy.people.Contacts(test_connection) - only_sharing = contacts.get() - for i in only_sharing: + result = contacts.get() + for i in result: self.assertEqual(diaspy.people.User, type(i))