Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added block/unblock/bulk block methods #160

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions pytumblr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,58 @@ def notes(self, blogname, id, **kwargs):
kwargs.update({"id":id})
return self.send_api_request('get', url, kwargs, valid_options)

@validate_blogname
def block(self, blogname, block_user=None, block_post=None):
"""
Block a blog from the given blog.
:param blogname: a string, the url of the blog doing the blocking
:param block_user: a string, the url of the blog to be blocked. Only if block_post is not passed.
:param block_post: a string, the id of the post to be blocked. Only if block_user is not passed.

:returns: a dict created from the JSON response
"""
url = '/v2/blog/{}/blocks'.format(blogname)

if block_user is not None and block_post is not None:
raise RuntimeError('Either block_user or block_post needs to be unassigned.')

valid_options = ['blocked_tumblelog', 'post_id']
if block_user is not None:
return self.send_api_request('post', url, {'blocked_tumblelog' : block_user}, valid_options)
elif block_post is not None:
return self.send_api_request('post', url, {'post_id' : block_post}, valid_options)

@validate_blogname
def bulk_block(self, blogname, block_users):
"""
Block a whole bunch of blogs from the given blog.
:param blogname: a string, the url of the blog doing the blocking
:param block_users: a list of strings, the urls of all blogs to be blocked.
"""
url = '/v2/blog/{}/blocks/bulk'.format(blogname)

#format the list into comma-seperated string
block_string = block_users[0]
if len(block_users) > 1:
for identifier in block_users[1:]:
block_string += ',{}'.format(identifier)
valid_options = ['blocked_tumblelogs']
return self.send_api_request('post', url, {'blocked_tumblelogs' : block_string}, valid_options)

@validate_blogname
def unblock(self, blogname, unblock_user):
"""
Unblock a blog from the given blog.
:param blogname: a string, the url of the blog holding the block
:param ubblock_user: a string, the url of the blog to be unblocked.

:returns: a dict created from the JSON response
"""
url = '/v2/blog/{}/blocks'.format(blogname)

valid_options = ['blocked_tumblelog']
return self.send_api_request('delete', url, {'blocked_tumblelog' : unblock_user}, valid_options)

# Parameters valid for /post, /post/edit, and /post/reblog.
def _post_valid_options(self, post_type=None):
# These options are always valid
Expand Down
20 changes: 20 additions & 0 deletions tests/test_pytumblr.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,26 @@ def test_create_video(self, mock_post):
response = self.client.create_video('codingjester.tumblr.com', embed="blahblahembed")
assert response == []

@mock.patch('requests.post')
def test_block_user(self, mock_post):
mock_post.side_effect = wrap_response_storing_data(
'{"meta": {"status": 200, "msg": "OK"}, "response": []}',
mock_post)

response = self.client.block("flareongirlfloof.tumblr.com", "codingjester.tumblr.com")
assert response == []

@mock.patch('requests.delete')
def test_unblock_user(self, mock_post):
mock_post.side_effect = wrap_response_storing_data(
'{"meta": {"status": 200, "msg": "OK"}, "response": []}',
mock_post)

response = self.client.unblock("flareongirlfloof.tumblr.com", "codingjester.tumblr.com")
assert response == []



@mock.patch('requests.delete')
def test_api_delete(self, mock_delete):
mock_delete.side_effect = wrap_response('{"meta": {"status": 200, "msg": "OK"}, "response": []}')
Expand Down