Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add link_flair support to set_flair.

  • Loading branch information...
commit 0456b8ef76f1eddcdf21846b4e7751577b985a00 1 parent 5053d97
@bboe bboe authored
Showing with 28 additions and 9 deletions.
  1. +13 −5 reddit/__init__.py
  2. +15 −4 reddit/reddit_test.py
View
18 reddit/__init__.py
@@ -424,16 +424,24 @@ def get_stylesheet(self, subreddit):
@reddit.decorators.require_login
@reddit.decorators.require_moderator
- def set_flair(self, subreddit, user, flair_text='', flair_css_class=''):
- """Set flair for the user in the given subreddit."""
+ def set_flair(self, subreddit, item, flair_text='', flair_css_class=''):
+ """Set flair for the user in the given subreddit.
+
+ Item can be a string, Redditor object, or Submission object. If item is
+ a string it will be treated as a Redditor.
+ """
params = {'r': six.text_type(subreddit),
- 'name': six.text_type(user),
'text': flair_text or '',
'css_class': flair_css_class or ''}
+ if isinstance(item, reddit.objects.Submission):
+ params['link'] = item.content_id
+ evict = item.permalink
+ else:
+ params['name'] = six.text_type(item)
+ evict = self.config['flairlist'] % six.text_type(subreddit)
response = self.request_json(self.config['flair'], params)
- stale_url = self.config['flairlist'] % six.text_type(subreddit)
# pylint: disable-msg=E1101,W0212
- reddit.helpers._request.evict([stale_url])
+ reddit.helpers._request.evict([evict])
return response
@reddit.decorators.require_login
View
19 reddit/reddit_test.py
@@ -347,20 +347,31 @@ def setUp(self):
self.configure()
self.subreddit = self.r.get_subreddit(self.sr)
- def test_add_flair_by_subreddit_name(self):
+ def test_add_link_flair(self):
+ flair_text = 'Flair: %s' % uuid.uuid4()
+ sub = six_next(self.subreddit.get_new_by_date())
+ self.subreddit.set_flair(sub, flair_text)
+ sub = self.r.get_submission(sub.permalink)
+ self.assertEqual(sub.link_flair_text, flair_text)
+
+ def test_add_link_flair_to_invalid_subreddit(self):
+ sub = six_next(self.r.get_subreddit('bboe').get_new_by_date())
+ self.assertRaises(HTTPError, self.subreddit.set_flair, sub, 'text')
+
+ def test_add_user_flair_by_subreddit_name(self):
flair_text = 'Flair: %s' % uuid.uuid4()
self.r.set_flair(self.sr, self.r.user, flair_text)
flair = self.r.get_flair(self.sr, self.r.user)
self.assertEqual(flair['flair_text'], flair_text)
self.assertEqual(flair['flair_css_class'], None)
- def test_add_flair_to_invalid_user(self):
+ def test_add_user_flair_to_invalid_user(self):
self.assertRaises(errors.APIException, self.subreddit.set_flair, 'b')
- def test_add_flair_by_name(self):
+ def test_add_user_flair_by_name(self):
flair_text = 'Flair: %s' % uuid.uuid4()
flair_css = 'a%d' % random.randint(0, 1024)
- self.subreddit.set_flair(self.r.user, flair_text, flair_css)
+ self.subreddit.set_flair(text_type(self.r.user), flair_text, flair_css)
flair = self.subreddit.get_flair(self.r.user)
self.assertEqual(flair['flair_text'], flair_text)
self.assertEqual(flair['flair_css_class'], flair_css)
Please sign in to comment.
Something went wrong with that request. Please try again.