Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added short_link attribute to the submission class.

  • Loading branch information...
commit b7b5b20d1460f2fc9930219c6536d133d924758c 1 parent 2670978
@bboe bboe authored
View
11 reddit/__init__.py
@@ -119,6 +119,10 @@ def __init__(self, site_name):
self.default_content_limit = int(obj['default_content_limit'])
self.domain = obj['domain']
self.more_comments_max = int(obj['more_comments_max'])
+ if 'short_domain' in obj:
+ self._short_domain = 'http://' + obj['short_domain']
+ else:
+ self._short_domain = None
self.timeout = float(obj['timeout'])
try:
self.user = obj['user']
@@ -133,6 +137,13 @@ def __getitem__(self, key):
return urljoin(self._ssl_url, self.API_PATHS[key])
return urljoin(self._site_url, self.API_PATHS[key])
+ @property
+ def short_domain(self):
+ if self._short_domain:
+ return self._short_domain
+ else:
+ raise reddit.errors.ClientException('No short domain specified.')
+
class BaseReddit(object):
"""The base class for a reddit session."""
View
4 reddit/objects.py
@@ -603,6 +603,10 @@ def comments_flat(self):
self._comments_flat.append(comment)
return self._comments_flat
+ @property
+ def short_link(self):
+ return urljoin(self.reddit_session.config.short_domain, self.id)
+
class Subreddit(Messageable):
"""A class for Subreddits."""
View
1  reddit/reddit_api.cfg
@@ -44,6 +44,7 @@ userlist_kind: UserList
[reddit]
domain: www.reddit.com
+short_domain: redd.it
ssl_domain: ssl.reddit.com
[local]
View
94 reddit/reddit_test.py
@@ -604,6 +604,18 @@ class SubmissionTest(unittest.TestCase, AuthenticatedHelper):
def setUp(self):
self.configure()
+ def test_clear_vote(self):
+ submission = None
+ for submission in self.r.user.get_submitted():
+ if submission.likes is False:
+ break
+ if not submission or submission.likes is not False:
+ self.fail('Could not find a down-voted submission.')
+ submission.clear_vote()
+ # reload the submission
+ submission = self.r.get_submission(submission_id=submission.id)
+ self.assertEqual(submission.likes, None)
+
def test_delete(self):
submission = list(self.r.user.get_submitted())[-1]
submission.delete()
@@ -611,6 +623,37 @@ def test_delete(self):
submission = self.r.get_submission(submission_id=submission.id)
self.assertEqual(None, submission.author)
+ def test_downvote(self):
+ submission = None
+ for submission in self.r.user.get_submitted():
+ if submission.likes is True:
+ break
+ if not submission or submission.likes is not True:
+ self.fail('Could not find an up-voted submission.')
+ submission.downvote()
+ # reload the submission
+ submission = self.r.get_submission(submission_id=submission.id)
+ self.assertEqual(submission.likes, False)
+
+ def test_report(self):
+ # login as new user to report submission
+ oth = Reddit(USER_AGENT)
+ oth.login('PyApiTestUser3', '1111')
+ subreddit = oth.get_subreddit(self.sr)
+ submission = None
+ for submission in subreddit.get_new_by_date():
+ if not submission.hidden:
+ break
+ if not submission or submission.hidden:
+ self.fail('Could not find a non-reported submission.')
+ submission.report()
+ # check if submission was reported
+ for report in self.r.get_subreddit(self.sr).get_reports():
+ if report.id == submission.id:
+ break
+ else:
+ self.fail('Could not find reported submission.')
+
def test_save(self):
submission = None
for submission in self.r.user.get_submitted():
@@ -629,6 +672,14 @@ def test_save(self):
else:
self.fail('Could not find submission in saved links.')
+ def test_short_link(self):
+ submission = six_next(self.r.get_front_page())
+ if self.r.config.is_reddit:
+ self.assertTrue(submission.id in submission.short_link)
+ else:
+ self.assertRaises(errors.ClientException, getattr, submission,
+ 'short_link')
+
def test_unsave(self):
submission = None
for submission in self.r.user.get_submitted():
@@ -641,30 +692,6 @@ def test_unsave(self):
submission = self.r.get_submission(submission_id=submission.id)
self.assertFalse(submission.saved)
- def test_clear_vote(self):
- submission = None
- for submission in self.r.user.get_submitted():
- if submission.likes is False:
- break
- if not submission or submission.likes is not False:
- self.fail('Could not find a down-voted submission.')
- submission.clear_vote()
- # reload the submission
- submission = self.r.get_submission(submission_id=submission.id)
- self.assertEqual(submission.likes, None)
-
- def test_downvote(self):
- submission = None
- for submission in self.r.user.get_submitted():
- if submission.likes is True:
- break
- if not submission or submission.likes is not True:
- self.fail('Could not find an up-voted submission.')
- submission.downvote()
- # reload the submission
- submission = self.r.get_submission(submission_id=submission.id)
- self.assertEqual(submission.likes, False)
-
def test_upvote(self):
submission = None
for submission in self.r.user.get_submitted():
@@ -677,25 +704,6 @@ def test_upvote(self):
submission = self.r.get_submission(submission_id=submission.id)
self.assertEqual(submission.likes, True)
- def test_report(self):
- # login as new user to report submission
- oth = Reddit(USER_AGENT)
- oth.login('PyApiTestUser3', '1111')
- subreddit = oth.get_subreddit(self.sr)
- submission = None
- for submission in subreddit.get_new_by_date():
- if not submission.hidden:
- break
- if not submission or submission.hidden:
- self.fail('Could not find a non-reported submission.')
- submission.report()
- # check if submission was reported
- for report in self.r.get_subreddit(self.sr).get_reports():
- if report.id == submission.id:
- break
- else:
- self.fail('Could not find reported submission.')
-
class SubmissionCreateTest(unittest.TestCase, AuthenticatedHelper):
def setUp(self):
Please sign in to comment.
Something went wrong with that request. Please try again.