Skip to content

Commit

Permalink
Simplify is_link parameter in SubredditFlairTemplates subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
leviroth committed Jun 18, 2017
1 parent 6d24e82 commit 5a57a0d
Showing 1 changed file with 41 additions and 43 deletions.
84 changes: 41 additions & 43 deletions praw/models/reddit/subreddit.py
Expand Up @@ -798,20 +798,22 @@ def __init__(self, subreddit):
.. note:: This class should not be initialized directly. Instead obtain
an instance via:
``reddit.subreddit('subreddit_name').flair.templates``
``reddit.subreddit('subreddit_name').flair.templates`` or
``reddit.subreddit('subreddit_name').flair.link_templates``.
"""
self.subreddit = subreddit

def add(self, text, css_class='', text_editable=False, is_link=False):
def add(self, text, css_class='', text_editable=False, is_link=None):
"""Add a flair template to the associated subreddit.
:param text: The flair template's text (required).
:param css_class: The flair template's css_class (default: '').
:param text_editable: (boolean) Indicate if the flair text can be
modified for each Redditor that sets it (default: False).
:param is_link: (boolean) When True, add a link flair template rather
than a Redditor flair template (default: False).
:param is_link: (boolean) When None, choose a template type in a
subclass-defined way. Otherwise, add a link flair template when
True and a Redditor flair template when false (default: None).
For example, to add an editable link flair try:
Expand All @@ -821,16 +823,20 @@ def add(self, text, css_class='', text_editable=False, is_link=False):
css_class='praw', text_editable=True, is_link=True)
"""
if is_link is None:
is_link = self.is_link
url = API_PATH['flairtemplate'].format(subreddit=self.subreddit)
data = {'css_class': css_class, 'flair_type': self.flair_type(is_link),
'text': text, 'text_editable': bool(text_editable)}
self.subreddit._reddit.post(url, data=data)

def clear(self, is_link=False):
def clear(self, is_link=None):
"""Remove all flair templates from the subreddit.
:param is_link: (boolean) When True, clear all link flair templates
rather than Redditor flair templates (default: False).
:param is_link: (boolean) When None, choose a template type in a
subclass-defined way. Otherwise, clear all link flair templates
when True and all Redditor flair template when false (default:
None).
For example, to clear all Redditor flair templates, run:
Expand All @@ -839,6 +845,8 @@ def clear(self, is_link=False):
reddit.subreddit('NAME').flair.templates.clear()
"""
if is_link is None:
is_link = self.is_link
url = API_PATH['flairtemplateclear'].format(subreddit=self.subreddit)
self.subreddit._reddit.post(
url, data={'flair_type': self.flair_type(is_link)})
Expand Down Expand Up @@ -886,6 +894,19 @@ def update(self, template_id, text, css_class='', text_editable=False):
class SubredditRedditorFlairTemplates(SubredditFlairTemplates):
"""Provide functions to interact with Redditor flair templates."""

def __init__(self, *args, **kwargs):
"""Create a SubredditRedditorFlairTemplate instance.
:param subreddit: The subreddit whose flair templates to work with.
.. note:: This class should not be initialized directly. Instead obtain
an instance via:
``reddit.subreddit('subreddit_name').flair.templates``
"""
super(SubredditRedditorFlairTemplates, self).__init__(*args, **kwargs)
self.is_link = False

def __iter__(self):
"""Iterate through the user flair templates.
Expand All @@ -907,6 +928,19 @@ def __iter__(self):
class SubredditLinkFlairTemplates(SubredditFlairTemplates):
"""Provide functions to interact with link flair templates."""

def __init__(self, *args, **kwargs):
"""Create a SubredditLinkFlairTemplate instance.
:param subreddit: The subreddit whose flair templates to work with.
.. note:: This class should not be initialized directly. Instead obtain
an instance via:
``reddit.subreddit('subreddit_name').flair.link_templates``
"""
super(SubredditLinkFlairTemplates, self).__init__(*args, **kwargs)
self.is_link = True

def __iter__(self):
"""Iterate through the link flair templates.
Expand All @@ -923,42 +957,6 @@ def __iter__(self):
for template in self.subreddit._reddit.get(url):
yield template

def add(self, text, css_class='', text_editable=False, is_link=True):
"""Add a link flair template to the associated subreddit.
:param text: The flair template's text (required).
:param css_class: The flair template's css_class (default: '').
:param text_editable: (boolean) Indicate if the flair text can be
modified for each Redditor that sets it (default: False).
:param is_link: (boolean) When False, add a Redditor flair template
rather than a link flair template (default: True).
For example, to add an editable flair try:
.. code-block:: python
reddit.subreddit('NAME').flair.templates.add(
css_class='praw', text_editable=True)
"""
super(SubredditLinkFlairTemplates, self).add(text, css_class,
text_editable, is_link)

def clear(self, is_link=True):
"""Remove all link flair templates from the subreddit.
:param is_link: (boolean) When False, clear all Redditor flair
templates rather than link flair templates (default: True).
For example, to clear all Redditor flair templates, run:
.. code-block:: python
reddit.subreddit('NAME').flair.templates.clear()
"""
super(SubredditLinkFlairTemplates, self).clear(is_link)


class SubredditModeration(object):
"""Provides a set of moderation functions to a Subreddit."""
Expand Down

0 comments on commit 5a57a0d

Please sign in to comment.