From 75d5b709f23a2a541786284aefe77959ee02363a Mon Sep 17 00:00:00 2001 From: bsimpson63 Date: Mon, 22 Apr 2013 14:28:27 -0400 Subject: [PATCH] Allow subreddit spam queries to be limited to links or comments. --- r2/r2/controllers/front.py | 38 ++++++++++++---- r2/r2/lib/db/queries.py | 88 +++++++++++++++++--------------------- r2/r2/models/subreddit.py | 17 +++++--- 3 files changed, 81 insertions(+), 62 deletions(-) diff --git a/r2/r2/controllers/front.py b/r2/r2/controllers/front.py index b5bb36bfc..79c61ea0a 100755 --- a/r2/r2/controllers/front.py +++ b/r2/r2/controllers/front.py @@ -499,13 +499,22 @@ class FrontController(RedditController, OAuth2ResourceController): location="log", extension_handling=False).render() - def _make_spamlisting(self, location, num, after, reverse, count): + def _make_spamlisting(self, location, only, num, after, reverse, count): + include_links, include_comments = True, True + if only == 'links': + include_comments = False + elif only == 'comments': + include_links = False + if location == 'reports': - query = c.site.get_reported() + query = c.site.get_reported(include_links=include_links, + include_comments=include_comments) elif location == 'spam': - query = c.site.get_spam() + query = c.site.get_spam(include_links=include_links, + include_comments=include_comments) elif location == 'modqueue': - query = c.site.get_modqueue() + query = c.site.get_modqueue(include_links=include_links, + include_comments=include_comments) elif location == 'unmoderated': query = c.site.get_unmoderated() else: @@ -637,14 +646,27 @@ class FrontController(RedditController, OAuth2ResourceController): @base_listing @prevent_framing_and_css(allow_cname_frame=True) @validate(VSrModerator(perms='posts'), - location=nop('location')) - def GET_spamlisting(self, location, num, after, reverse, count): + location=nop('location'), + only=VOneOf('only', ('links', 'comments'))) + def GET_spamlisting(self, location, only, num, after, reverse, count): c.allow_styles = True c.profilepage = True - pane = self._make_spamlisting(location, num, after, reverse, count) + pane = self._make_spamlisting(location, only, num, after, reverse, + count) if c.user.pref_private_feeds: extension_handling = "private" - return EditReddit(content=pane, location=location).render() + + if location in ('reports', 'spam', 'modqueue'): + buttons = [NavButton(_('links and comments'), None, opt='only'), + NavButton(_('links'), 'links', opt='only'), + NavButton(_('comments'), 'comments', opt='only')] + menus = [NavMenu(buttons, base_path=request.path, title=_('show'), + type='lightdrop')] + else: + menus = None + return EditReddit(content=pane, + location=location, + nav_menus=menus).render() @base_listing @prevent_framing_and_css(allow_cname_frame=True) diff --git a/r2/r2/lib/db/queries.py b/r2/r2/lib/db/queries.py index e0d2ec2c1..0c859c319 100755 --- a/r2/r2/lib/db/queries.py +++ b/r2/r2/lib/db/queries.py @@ -348,20 +348,29 @@ def get_spam_comments(sr_id): return Comment._query(Comment.c.sr_id == sr_id, Comment.c._spam == True, sort = db_sort('new')) -@merged_cached_query -def get_spam(sr, user=None): + +def moderated_srids(sr, user): if isinstance(sr, (ModContribSR, MultiReddit)): srs = Subreddit._byID(sr.sr_ids, return_dict=False) if user: srs = [sr for sr in srs if sr.is_moderator_with_perms(user, 'posts')] - q = [] - q.extend(get_spam_links(sr) for sr in srs) - q.extend(get_spam_comments(sr) for sr in srs) - return q + return [sr._id for sr in srs] + elif not user or sr.is_moderator_with_perms(user, 'posts'): + return [sr._id] else: - return [get_spam_links(sr), - get_spam_comments(sr)] + return [] + +@merged_cached_query +def get_spam(sr, user=None, include_links=True, include_comments=True): + sr_ids = moderated_srids(sr, user) + queries = [] + + if include_links: + queries.append(get_spam_links) + if include_comments: + queries.append(get_spam_comments) + return [query(sr_id) for sr_id, query in itertools.product(sr_ids, queries)] @cached_query(SubredditQueryCache) def get_spam_filtered_links(sr_id): @@ -399,19 +408,15 @@ def get_reported_comments(sr_id): sort = db_sort('new')) @merged_cached_query -def get_reported(sr, user=None): - if isinstance(sr, (ModContribSR, MultiReddit)): - srs = Subreddit._byID(sr.sr_ids, return_dict=False) - if user: - srs = [sr for sr in srs - if sr.is_moderator_with_perms(user, 'posts')] - q = [] - q.extend(get_reported_links(sr) for sr in srs) - q.extend(get_reported_comments(sr) for sr in srs) - return q - else: - return [get_reported_links(sr), - get_reported_comments(sr)] +def get_reported(sr, user=None, include_links=True, include_comments=True): + sr_ids = moderated_srids(sr, user) + queries = [] + + if include_links: + queries.append(get_reported_links) + if include_comments: + queries.append(get_reported_comments) + return [query(sr_id) for sr_id, query in itertools.product(sr_ids, queries)] @cached_query(SubredditQueryCache) def get_unmoderated_links(sr_id): @@ -425,36 +430,23 @@ def get_unmoderated_links(sr_id): return q @merged_cached_query -def get_modqueue(sr, user=None): - q = [] - if isinstance(sr, (ModContribSR, MultiReddit)): - srs = Subreddit._byID(sr.sr_ids, return_dict=False) - if user: - srs = [sr for sr in srs - if sr.is_moderator_with_perms(user, 'posts')] - q.extend(get_reported_links(sr) for sr in srs) - q.extend(get_reported_comments(sr) for sr in srs) - q.extend(get_spam_filtered_links(sr) for sr in srs) - q.extend(get_spam_filtered_comments(sr) for sr in srs) - else: - q.append(get_reported_links(sr)) - q.append(get_reported_comments(sr)) - q.append(get_spam_filtered_links(sr)) - q.append(get_spam_filtered_comments(sr)) - return q +def get_modqueue(sr, user=None, include_links=True, include_comments=True): + sr_ids = moderated_srids(sr, user) + queries = [] + + if include_links: + queries.append(get_reported_links) + queries.append(get_spam_filtered_links) + if include_comments: + queries.append(get_reported_comments) + queries.append(get_spam_filtered_comments) + return [query(sr_id) for sr_id, query in itertools.product(sr_ids, queries)] @merged_cached_query def get_unmoderated(sr, user=None): - q = [] - if isinstance(sr, MultiReddit): - srs = Subreddit._byID(sr.sr_ids, return_dict=False) - if user: - srs = [sr for sr in srs - if sr.is_moderator_with_perms(user, 'posts')] - q.extend(get_unmoderated_links(sr) for sr in srs) - else: - q.append(get_unmoderated_links(sr)) - return q + sr_ids = moderated_srids(sr, user) + queries = [get_unmoderated_links] + return [query(sr_id) for sr_id, query in itertools.product(sr_ids, queries)] def get_domain_links(domain, sort, time): from r2.lib.db import operators diff --git a/r2/r2/models/subreddit.py b/r2/r2/models/subreddit.py index 5c37a89e7..cc90d2827 100644 --- a/r2/r2/models/subreddit.py +++ b/r2/r2/models/subreddit.py @@ -497,17 +497,22 @@ class Subreddit(Thing, Printable): from r2.lib.db import queries return queries.get_links(self, sort, time) - def get_spam(self): + def get_spam(self, include_links=True, include_comments=True): from r2.lib.db import queries - return queries.get_spam(self, user=c.user) + return queries.get_spam(self, user=c.user, include_links=include_links, + include_comments=include_comments) - def get_reported(self): + def get_reported(self, include_links=True, include_comments=True): from r2.lib.db import queries - return queries.get_reported(self, user=c.user) + return queries.get_reported(self, user=c.user, + include_links=include_links, + include_comments=include_comments) - def get_modqueue(self): + def get_modqueue(self, include_links=True, include_comments=True): from r2.lib.db import queries - return queries.get_modqueue(self, user=c.user) + return queries.get_modqueue(self, user=c.user, + include_links=include_links, + include_comments=include_comments) def get_unmoderated(self): from r2.lib.db import queries