Skip to content

Commit e0ae32e

Browse files
Tailszefoxbboe
authored andcommitted
Added ability to report a submission, and get the reported submissions of a subreddit (must be a mod)
Alignment issue
1 parent 759be65 commit e0ae32e

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

reddit/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class Config(object): # pylint: disable-msg=R0903
6060
'read_message': 'api/read_message/',
6161
'reddit_url': '/',
6262
'register': 'api/register/',
63+
'report': 'api/report/',
64+
'reports': 'r/%s/about/reports/',
6365
'save': 'api/save/',
6466
'saved': 'saved/',
6567
'search_reddit_names': 'api/search_reddit_names/',
@@ -281,6 +283,12 @@ def get_contributors(self, subreddit):
281283
def get_moderators(self, subreddit):
282284
"""Get the list of moderators for a subreddit."""
283285
return self.request_json(self.config['moderators'] % str(subreddit))
286+
287+
@reddit.decorators.require_login
288+
def get_reports(self, subreddit, limit=None):
289+
"""Get the list of reported submissions for a subreddit."""
290+
return self.get_content(self.config['reports'] % str(subreddit),
291+
limit=limit, url_data={'uh':self.user.modhash})
284292

285293
@reddit.decorators.require_login
286294
def flair_list(self, subreddit, limit=None):

reddit/objects.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ def content_id(self):
8686
return '%s_%s' % (by_object[self.__class__], self.id)
8787

8888

89+
class Reportable(RedditContentObject):
90+
"""
91+
Additional interface for Reddit content objects that can be reported.
92+
"""
93+
@require_login
94+
def report(self):
95+
url = self.reddit_session.config['report']
96+
params = {'id': self.content_id,
97+
'uh': self.reddit_session.modhash,
98+
'api_type': 'json'}
99+
response = self.reddit_session.request_json(url, params)
100+
# pylint: disable-msg=E1101
101+
_request.is_stale([self.reddit_session.config['user']])
102+
return response
103+
89104
class Saveable(RedditContentObject):
90105
"""
91106
Additional interface for Reddit content objects that can be saved.
@@ -315,7 +330,7 @@ def get_unread(self, limit=0):
315330
return self.reddit_session.get_content(url, limit=limit)
316331

317332

318-
class Submission(Deletable, Saveable, Voteable):
333+
class Submission(Deletable, Reportable, Saveable, Voteable):
319334
"""A class for submissions to Reddit."""
320335
def __init__(self, reddit_session, json_dict):
321336
super(Submission, self).__init__(reddit_session, json_dict)
@@ -449,6 +464,11 @@ def subscribe(self):
449464
def unsubscribe(self):
450465
"""Unsubscribe from the given subreddit."""
451466
return self._subscribe(unsubscribe=True)
467+
468+
@require_login
469+
def get_reports(self, *args, **kwargs):
470+
"""Get the reported submissions on the given subreddit."""
471+
return self.reddit_session.get_reports(self, *args, **kwargs)
452472

453473

454474
class UserList(RedditContentObject):

reddit/reddit_test.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,24 @@ def test_upvote(self):
443443
# reload the submission
444444
submission = self.r.get_submission(submission_id=submission.id)
445445
self.assertEqual(submission.likes, True)
446-
446+
447+
def test_report(self):
448+
# login as new user to report submission
449+
oth = Reddit('reddit_api test suite')
450+
oth.login('PyApiTestUser3', '1111')
451+
submission = None
452+
for submission in oth.get_redditor(self.r.user.name).get_submitted():
453+
if submission.hidden is False:
454+
break
455+
if not submission or submission.hidden is True:
456+
self.fail('Could not find a non-reported submission.')
457+
submission.report()
458+
# check if submission was reported
459+
for report in self.r.get_subreddit(self.sr).get_reports():
460+
if report.id == submission.id:
461+
break
462+
else:
463+
self.fail('Could not find reported submission.')
447464

448465
class SubmissionCreateTest(unittest.TestCase, AuthenticatedHelper):
449466
def setUp(self):

0 commit comments

Comments
 (0)