Skip to content

Commit

Permalink
Ensure moderator metadata is associated with Redditor instances when …
Browse files Browse the repository at this point in the history
…it should be
  • Loading branch information
bboe committed Feb 18, 2017
1 parent 66b6683 commit 5b743ee
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 27 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Unreleased
* Uploading an image resulting in too large of a request (>500 KB) now
raises ``prawcore.TooLarge`` instead of an ``AssertionError``.
* Uploading an invalid image raises :class:`.APIException`.
* :class:`.Redditor` instances obtained via :attr:`.moderator` (e.g.,
``reddit.subreddit('subreddit').moderator()``) will contain attributes with
the relationship metadata (e.g., ``mod_permissions``).

4.3.0 (2017/01/19)
------------------
Expand Down
6 changes: 1 addition & 5 deletions praw/models/list/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ class BaseList(PRAWBase):

CHILD_ATTRIBUTE = None

@staticmethod
def _convert(reddit, item):
raise NotImplementedError('BaseList must be extended.')

def __init__(self, reddit, _data):
"""Initialize a BaseList instance.
Expand All @@ -24,7 +20,7 @@ def __init__(self, reddit, _data):

child_list = getattr(self, self.CHILD_ATTRIBUTE)
for index, item in enumerate(child_list):
child_list[index] = self._convert(reddit, item)
child_list[index] = reddit._objector.objectify(item)

def __contains__(self, item):
"""Test if item exists in the list."""
Expand Down
6 changes: 0 additions & 6 deletions praw/models/list/redditor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
"""Provide the RedditorList class."""
from ..reddit.redditor import Redditor
from .base import BaseList


class RedditorList(BaseList):
"""A list of Redditors. Works just like a regular list."""

CHILD_ATTRIBUTE = 'children'

@staticmethod
def _convert(reddit, data):
"""Return a :class:`.Redditor` instance from the data."""
return Redditor(reddit, data['name'])
32 changes: 31 additions & 1 deletion praw/models/reddit/subreddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ def banned(self):
reddit.subreddit('SUBREDDIT').banned.add('NAME', reason='a reason')
To list the banned users along with any notes, try:
.. code-block:: python
for ban in reddit.subreddit('SUBREDDIT').banned():
print('{}: {}'.format(ban, ban.note))
"""
if self._banned is None:
self._banned = SubredditRelationship(self, 'banned')
Expand Down Expand Up @@ -170,7 +177,22 @@ def mod(self):

@property
def moderator(self):
"""An instance of :class:`.ModeratorRelationship`."""
"""An instance of :class:`.ModeratorRelationship`.
For example to add a moderator try:
.. code-block:: python
reddit.subreddit('SUBREDDIT').moderator.add('NAME')
To list the moderators along with their permissions try:
.. code-block:: python
for moderator in reddit.subreddit('SUBREDDIT').moderator():
print('{}: {}'.format(moderator, moderator.mod_permissions))
"""
if self._moderator is None:
self._moderator = ModeratorRelationship(self, 'moderator')
return self._moderator
Expand Down Expand Up @@ -1285,6 +1307,14 @@ def __call__(self, redditor=None):
moderators = reddit.subreddit('nameofsub').moderator()
For example, to list the moderators along with their permissions try:
.. code:: python
for moderator in reddit.subreddit('SUBREDDIT').moderator():
print('{}: {}'.format(moderator, moderator.mod_permissions))
"""
params = {} if redditor is None else {'user': redditor}
url = API_PATH['list_{}'.format(self.relationship)].format(
Expand Down
1 change: 1 addition & 0 deletions tests/integration/models/reddit/test_subreddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,7 @@ def test_moderator__user_filter(self):
'TestSubredditRelationships.moderator__user_filter'):
moderator = self.subreddit.moderator(redditor='pyapitestuser3')
assert len(moderator) == 1
assert 'mod_permissions' in moderator[0].__dict__

def test_muted(self):
self.reddit.read_only = False
Expand Down
30 changes: 15 additions & 15 deletions tests/unit/models/list/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,51 @@
from praw.models.list.base import BaseList


class DummyObjector(object):
@staticmethod
def objectify(value):
return value


class Dummy(object):
def __init__(self):
self._objector = DummyObjector


class TestBaseList(object):
def setup(self):
self._prev_child_attribute = BaseList.CHILD_ATTRIBUTE
self._prev_convert = BaseList._convert

def teardown(self):
BaseList.CHILD_ATTRIBUTE = self._prev_child_attribute
BaseList._convert = staticmethod(self._prev_convert)

def test__init__CHILD_ATTRIBUTE_not_set(self):
with pytest.raises(NotImplementedError):
BaseList(None, None)

def test__init___convert_not_extended(self):
BaseList.CHILD_ATTRIBUTE = 'praw'
with pytest.raises(NotImplementedError):
BaseList(None, {'praw': [1]})

def test__contains__(self):
BaseList._convert = staticmethod(lambda _a, _b: None)
BaseList.CHILD_ATTRIBUTE = 'praw'
items = ['foo', 1, {'a': 'b'}]
base_list = BaseList(None, {'praw': items})
base_list = BaseList(Dummy(), {'praw': items})
for item in items:
assert item in base_list

def test__getitem__(self):
BaseList._convert = staticmethod(lambda _a, _b: None)
BaseList.CHILD_ATTRIBUTE = 'praw'
items = ['foo', 1, {'a': 'b'}]
base_list = BaseList(None, {'praw': items})
base_list = BaseList(Dummy(), {'praw': items})
for i, item in enumerate(items):
assert item == base_list[i]

def test__iter__(self):
BaseList._convert = staticmethod(lambda _a, _b: None)
BaseList.CHILD_ATTRIBUTE = 'praw'
items = ['foo', 1, {'a': 'b'}]
base_list = BaseList(None, {'praw': items})
base_list = BaseList(Dummy(), {'praw': items})
for i, item in enumerate(base_list):
assert items[i] == item

def test__str__(self):
BaseList._convert = staticmethod(lambda _a, _b: None)
BaseList.CHILD_ATTRIBUTE = 'praw'
items = ['foo', 1, {'a': 'b'}]
base_list = BaseList(None, {'praw': items})
base_list = BaseList(Dummy(), {'praw': items})
assert str(items) == str(base_list)

0 comments on commit 5b743ee

Please sign in to comment.