python2 -m unittest --verbose --catch --failfast tests.py
clean:
- rm -rv {*/,}__pycache__/
- rm -rv {*/,}*.pyc
+ rm -rv {*,.}/__pycache__/
import diaspy.streams as streams
import diaspy.client as client
import diaspy.people as people
+import diaspy.notifications as notifications
#!/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.
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
: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.
: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.
"""
-class Generic:
- """Object representing generic stream. Used in Tag(),
- Stream(), Activity() etc.
+class Generic():
+ """Object representing generic stream.
"""
_location = 'stream.json'
_stream = []
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()
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))