return request.json()['data']['photo']['id']
-class Activity(Generic):
+class Activity(Stream):
"""Stream representing user's activity.
"""
_location = 'activity.json'
An example call would be `aspects.json?aspect_ids=23,5,42`
"""
_location = 'aspects.json'
- _id_regexp = re.compile(r'<a href="/aspects/[0-9]+/edit" rel="facebox"')
- def getID(self, aspect):
+ def getAspectID(self, aspect_name):
"""Returns id of an aspect of given name.
Returns -1 if aspect is not found.
- :param aspect: aspect name (must be spelled exactly as when created)
- :type aspect: str
+ :param aspect_name: aspect name (must be spelled exactly as when created)
+ :type aspect_name: str
:returns: int
"""
id = -1
aspects = self._connection.getUserInfo()['aspects']
- for item in aspects:
- if item['name'] == aspect: id = item['id']
+ for aspect in aspects:
+ if aspect['name'] == aspect_name: id = aspect['id']
return id
def filterByIDs(self, ids):
self._location += '?{0}'.format(','.join(ids))
self.fill()
- def _getaid(self, response):
- """Extracts id of just created aspect.
- """
- id = self._id_regexp.search(response.text).group(0).split('/')[2]
- return int(id)
-
def add(self, aspect_name, visible=0):
"""This function adds a new aspect.
- Status code 422 is accepteb because it is returned by D* when
+ Status code 422 is accepted because it is returned by D* when
you try to add aspect already present on your aspect list.
- :returns: id of created aspect (or -1 if status_code was 422)
+ :returns: id of created aspect
"""
data = {'authenticity_token': self._connection.get_token(),
'aspect[name]': aspect_name,
if request.status_code not in [200, 422]:
raise Exception('wrong status code: {0}'.format(request.status_code))
- if request.status_code == 422: id = -1
- else: id = self._getaid(request)
+ id = self.getAspectID(aspect_name)
return id
- def remove(self, aspect_id=0, name=''):
+ def remove(self, aspect_id=-1, name=''):
"""This method removes an aspect.
- 500 is accepted because although the D* will
+ You can give it either id or name of the aspect.
+ When both are specified, id takes precedence over name.
+
+ Status code 500 is accepted because although the D* will
go nuts it will remove the aspect anyway.
:param aspect_id: id fo aspect to remove
:param name: name of aspect to remove
:type name: str
"""
- if not aspect_id and name: aspect_id = self.getID(name)
+ if aspect_id == -1 and name: aspect_id = self.getAspectID(name)
data = {'authenticity_token': self._connection.get_token()}
request = self._connection.delete('aspects/{}'.format(aspect_id),
data=data)
print('[ CONNECTED ]\n')
post_text = '#diaspy test no. {0}'.format(test_count)
-test_aspect_id = -1
#######################################
def testAspectsAdd(self):
aspects = diaspy.streams.Aspects(test_connection)
- test_aspect_id = aspects.add('diaspy-test')
- aspects.add('diaspy-test-false')
+ aspects.add(testconf.test_aspect_name_fake)
+ testconf.test_aspect_id = aspects.add(testconf.test_aspect_name)
def testAspectsGettingID(self):
aspects = diaspy.streams.Aspects(test_connection)
- id = aspects.getID('diaspy-test')
- self.assertEqual(int, type(id))
- self.assertEqual(test_aspect_id, id)
+ id = aspects.getAspectID(testconf.test_aspect_name)
+ self.assertEqual(testconf.test_aspect_id, id)
def testAspectsRemoveById(self):
aspects = diaspy.streams.Aspects(test_connection)
- aspects.remove(test_aspect_id)
+ aspects.remove(testconf.test_aspect_id)
+ self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name))
def testAspectsRemoveByName(self):
aspects = diaspy.streams.Aspects(test_connection)
- aspects.remove(name='diaspy-test-false')
+ aspects.remove(name=testconf.test_aspect_name_fake)
+ self.assertEqual(-1, aspects.getAspectID(testconf.test_aspect_name_fake))
def testActivity(self):
activity = diaspy.streams.Activity(test_connection)
class PostTests(unittest.TestCase):
def testStringConversion(self):
s = diaspy.streams.Stream(test_connection)
- print(str(s[0]))
def testRepr(self):
s = diaspy.streams.Stream(test_connection)
- print(repr(s[0]))
if __name__ == '__main__': unittest.main()