def create_draft_sr(self): from r2.models.subreddit import Subreddit Subreddit.subscribe_defaults(self) # Create a drafts subredit for this user return Subreddit._create_and_subscribe( self.draft_sr_name, self, { 'title': "Drafts for " + self.name, 'type': "private", 'default_listing': 'new', })
def create_draft_sr(self): from r2.models.subreddit import Subreddit Subreddit.subscribe_defaults(self) # Create a drafts subredit for this user return Subreddit._create_and_subscribe( self.draft_sr_name, self, { 'title': "Drafts for " + self.name, 'type': "private", 'default_listing': 'new', } )
def add_props(cls, user, wrapped): #fetch parent links links = Link._byID(set(l.link_id for l in wrapped), True) #get srs for comments that don't have them (old comments) for cm in wrapped: if not hasattr(cm, 'sr_id'): cm.sr_id = links[cm.link_id].sr_id subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped), data=True, return_dict=False) can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) min_score = c.user.pref_min_comment_score cids = dict((w._id, w) for w in wrapped) for item in wrapped: item.link = links.get(item.link_id) if not hasattr(item, 'subreddit'): item.subreddit = item.subreddit_slow if hasattr(item, 'parent_id'): parent = Comment._byID(item.parent_id, data=True) parent_author = Account._byID(parent.author_id, data=True) item.parent_author = parent_author if not c.full_comment_listing and cids.has_key(item.parent_id): item.parent_permalink = '#' + utils.to36(item.parent_id) else: item.parent_permalink = parent.make_anchored_permalink( item.link, item.subreddit) else: item.parent_permalink = None item.parent_author = None item.can_reply = (item.sr_id in can_reply_srs) # Don't allow users to vote on their own comments item.votable = bool(c.user != item.author and not item.retracted) # not deleted on profile pages, # deleted if spam and not author or admin item.deleted = (not c.profilepage and (item._deleted or (item._spam and item.author != c.user and not item.show_spam))) # don't collapse for admins, on profile pages, or if deleted item.collapsed = ( (item.score < min_score) and not (c.profilepage or item.deleted or c.user_is_admin)) if not hasattr(item, 'editted'): item.editted = False #will get updated in builder item.num_children = 0 item.score_fmt = Score.points item.permalink = item.make_permalink(item.link, item.subreddit) item.can_be_deleted = item.can_delete()
def _link_nav_query(self, clause = None, sort = None): sr = Subreddit.default() q = Link._query(self._nav_query_date_clause(sort), Link.c._deleted == False, Link.c._spam == False, Link.c.sr_id == sr._id, limit = 1, sort = sort, data = True) if clause is not None: q._filter(clause) return q
def add_props(cls, user, wrapped): #fetch parent links links = Link._byID(set(l.link_id for l in wrapped), True) #get srs for comments that don't have them (old comments) for cm in wrapped: if not hasattr(cm, 'sr_id'): cm.sr_id = links[cm.link_id].sr_id subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped), data=True,return_dict=False) can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) min_score = c.user.pref_min_comment_score cids = dict((w._id, w) for w in wrapped) for item in wrapped: item.link = links.get(item.link_id) if not hasattr(item, 'subreddit'): item.subreddit = item.subreddit_slow if hasattr(item, 'parent_id'): parent = Comment._byID(item.parent_id, data=True) parent_author = Account._byID(parent.author_id, data=True) item.parent_author = parent_author if not c.full_comment_listing and cids.has_key(item.parent_id): item.parent_permalink = '#' + utils.to36(item.parent_id) else: item.parent_permalink = parent.make_anchored_permalink(item.link, item.subreddit) else: item.parent_permalink = None item.parent_author = None item.can_reply = (item.sr_id in can_reply_srs) # Don't allow users to vote on their own comments item.votable = bool(c.user != item.author) # not deleted on profile pages, # deleted if spam and not author or admin item.deleted = (not c.profilepage and (item._deleted or (item._spam and item.author != c.user and not item.show_spam))) # don't collapse for admins, on profile pages, or if deleted item.collapsed = ((item.score < min_score) and not (c.profilepage or item.deleted or c.user_is_admin)) if not hasattr(item,'editted'): item.editted = False #will get updated in builder item.num_children = 0 item.score_fmt = Score.points item.permalink = item.make_permalink(item.link, item.subreddit)
def karma(self, kind, sr=None): from subreddit import Subreddit suffix = "_" + kind + "_karma" # if no sr, return the sum if sr is None: total = 0 for k, v in self._t.iteritems(): if k.endswith(suffix): if kind == "link": try: karma_sr_name = k[0 : k.rfind(suffix)] karma_sr = Subreddit._by_name(karma_sr_name) multiplier = karma_sr.post_karma_multiplier except NotFound: multiplier = 1 else: multiplier = 1 total += v * multiplier return total else: try: return getattr(self, sr.name + suffix) except AttributeError: # if positive karma elsewhere, you get min_up_karma if self.karma(kind) > 0: return g.MIN_UP_KARMA else: return 0
def karma(self, kind, sr = None): from subreddit import Subreddit suffix = '_' + kind + '_karma' #if no sr, return the sum if sr is None: total = 0 for k, v in self._t.iteritems(): if k.endswith(suffix): if kind == 'link': try: karma_sr_name = k[0:k.rfind(suffix)] karma_sr = Subreddit._by_name(karma_sr_name) multiplier = karma_sr.post_karma_multiplier except NotFound: multiplier = 1 else: multiplier = 1 total += v * multiplier return total else: try: return getattr(self, sr.name + suffix) except AttributeError: #if positive karma elsewhere, you get min_up_karma if self.karma(kind) > 0: return g.MIN_UP_KARMA else: return 0
def subreddit_slow(self): from subreddit import Subreddit """return's a link's subreddit. in most case the subreddit is already on the wrapped link (as .subreddit), and that should be used when possible. """ return Subreddit._byID(self.sr_id, True, return_dict=False)
def __setattr__(self, name, value): if name == "subreddit": value = Subreddit(self.reddit_session, value, fetch=False) elif name == "redditor" or name == "author": # Special case for deleted users if value != '[deleted]': value = Redditor(self.reddit_session, value, fetch=False) object.__setattr__(self, name, value)
def main(): subreddit = Subreddit(sys.argv[1]) graph = Graph(subreddit) runTextRankOnGraph(graph, 1, 0.85) top5Vertices = graph.getTopNVertices(5) print " ".join([subreddit.idToItem[v.id].body for v in top5Vertices])
def can_submit(self, user): if c.user_is_admin: return True elif self.author_id == c.user._id: # They can submit if they are the author and still have access # to the subreddit of the article sr = Subreddit._byID(self.sr_id, data=True) return sr.can_submit(user) else: return False
def define_subreddit(self, subreddit_model, moderators, recent_submissions, recent_comments): subreddit = Subreddit(self.data_object.get_length(), subreddit_model.display_name_prefixed, subreddit_model.community_icon, subreddit_model.public_description, subreddit_model.subscribers, subreddit_model.created, subreddit_model.over18, moderators, recent_submissions, recent_comments) self.data_object.add_subreddit(subreddit)
def subreddit_slow(self): from subreddit import Subreddit """return's a comments's subreddit. in most case the subreddit is already on the wrapped link (as .subreddit), and that should be used when possible. if sr_id does not exist, then use the parent link's""" self._safe_load() if hasattr(self, 'sr_id'): sr_id = self.sr_id else: l = Link._byID(self.link_id, True) sr_id = l.sr_id return Subreddit._byID(sr_id, True, return_dict=False)
def subreddit_slow(self): from subreddit import Subreddit """return's a comments's subreddit. in most case the subreddit is already on the wrapped link (as .subreddit), and that should be used when possible. if sr_id does not exist, then use the parent link's""" self._safe_load() if hasattr(self, 'sr_id'): sr_id = self.sr_id else: l = Link._byID(self.link_id, True) sr_id = l.sr_id return Subreddit._byID(sr_id, True, return_dict = False)
def can_view_slow(self): if c.user_is_loggedin: # simple case from before: if (c.user_is_admin or c.user._id in (self.author_id, self.to_id)): return True elif self.sr_id: sr = Subreddit._byID(self.sr_id) is_moderator = sr.is_moderator(c.user) # moderators can view messages on subreddits they moderate if is_moderator: return True elif self.first_message: first = Message._byID(self.first_message, True) return (first.author_id == c.user._id)
def _next_link_for_tag(self, tag, sort): """Returns a query navigation by tag using the supplied sort""" from r2.lib.db import tdb_sql as tdb import sqlalchemy as sa # List of the subreddit ids this user has access to sr = Subreddit.default() # Get a reference to reddit_rel_linktag linktag_type = tdb.rel_types_id[LinkTag._type_id] linktag_thing_table = linktag_type.rel_table[0] # Get a reference to the reddit_thing_link & reddit_data_link tables link_type = tdb.types_id[Link._type_id] link_data_table = link_type.data_table[0] link_thing_table = link_type.thing_table # Subreddit subquery aliased as link_sr link_sr = sa.select([ link_data_table.c.thing_id, sa.cast(link_data_table.c.value, sa.INT).label('sr_id') ], link_data_table.c.key == 'sr_id').alias('link_sr') # Determine the date clause based on the sort order requested if isinstance(sort, operators.desc): date_clause = link_thing_table.c.date < self._date sort = sa.desc(link_thing_table.c.date) else: date_clause = link_thing_table.c.date > self._date sort = sa.asc(link_thing_table.c.date) query = sa.select( [linktag_thing_table.c.thing1_id], sa.and_( linktag_thing_table.c.thing2_id == tag._id, linktag_thing_table.c.thing1_id == link_sr.c.thing_id, linktag_thing_table.c.thing1_id == link_thing_table.c.thing_id, linktag_thing_table.c.name == 'tag', link_thing_table.c.spam == False, link_thing_table.c.deleted == False, date_clause, link_sr.c.sr_id == sr._id), order_by=sort, limit=1) row = query.execute().fetchone() return Link._byID(row.thing1_id, data=True) if row else None
def _next_link_for_tag(self, tag, sort): """Returns a query navigation by tag using the supplied sort""" from r2.lib.db import tdb_sql as tdb import sqlalchemy as sa # List of the subreddit ids this user has access to sr = Subreddit.default() # Get a reference to reddit_rel_linktag linktag_type = tdb.rel_types_id[LinkTag._type_id] linktag_thing_table = linktag_type.rel_table[0] # Get a reference to the reddit_thing_link & reddit_data_link tables link_type = tdb.types_id[Link._type_id] link_data_table = link_type.data_table[0] link_thing_table = link_type.thing_table # Subreddit subquery aliased as link_sr link_sr = sa.select([ link_data_table.c.thing_id, sa.cast(link_data_table.c.value, sa.INT).label('sr_id')], link_data_table.c.key == 'sr_id').alias('link_sr') # Determine the date clause based on the sort order requested if isinstance(sort, operators.desc): date_clause = link_thing_table.c.date < self._date sort = sa.desc(link_thing_table.c.date) else: date_clause = link_thing_table.c.date > self._date sort = sa.asc(link_thing_table.c.date) query = sa.select([linktag_thing_table.c.thing1_id], sa.and_(linktag_thing_table.c.thing2_id == tag._id, linktag_thing_table.c.thing1_id == link_sr.c.thing_id, linktag_thing_table.c.thing1_id == link_thing_table.c.thing_id, linktag_thing_table.c.name == 'tag', link_thing_table.c.spam == False, link_thing_table.c.deleted == False, date_clause, link_sr.c.sr_id == sr._id), order_by = sort, limit = 1) row = query.execute().fetchone() return Link._byID(row.thing1_id, data=True) if row else None
def main(): config = importConfig() creds = [] sub_data = [] print("Extracting credentials...", end="") try: creds = config["credentials"] except: error( "\ncredentials not found, use keyword 'credentials' in config.yaml" ) print("DONE.") print("Extracting subreddits...", end="") try: sub_data = config["subreddits"] except: error( "\nsubreddits not found, use keyword 'subreddits' in config.yaml") print("DONE.") print("Validating Subreddits...", end="") subreddits = [] for sub in sub_data.items(): subreddits.append(Subreddit(sub)) print("DONE.") reddit = [] try: reddit = praw.Reddit(client_id=creds['client_id'], client_secret=creds['client_secret'], user_agent=creds['user_agent']) except: error("Bad Credentials") mail = [] try: mail = Mail(config['mail']) except: print("WARN: no mail found in conf.yaml") notify.start(subreddits, reddit, mail)
def add_props(cls, user, wrapped): #TODO global-ish functions that shouldn't be here? #reset msgtime after this request msgtime = c.have_messages #load the "to" field if required to_ids = set(w.to_id for w in wrapped) tos = Account._byID(to_ids, True) if to_ids else {} links = Link._byID(set(l.link_id for l in wrapped if l.was_comment), data = True, return_dict = True) subreddits = Subreddit._byID(set(l.sr_id for l in links.values()), data = True, return_dict = True) parents = Comment._byID(set(l.parent_id for l in wrapped if hasattr(l, "parent_id") and l.was_comment), data = True, return_dict = True) for item in wrapped: item.to = tos[item.to_id] if msgtime and item._date >= msgtime: item.new = True else: item.new = False item.score_fmt = Score.none item.message_style = "" if item.was_comment: link = links[item.link_id] sr = subreddits[link.sr_id] item.link_title = link.title item.link_permalink = link.make_permalink(sr) if hasattr(item, "parent_id"): item.subject = _('comment reply') item.message_style = "comment-reply" parent = parents[item.parent_id] item.parent = parent._fullname item.parent_permalink = parent.make_permalink(link, sr) else: item.subject = _('post reply') item.message_style = "post-reply" # Run this last Printable.add_props(user, wrapped)
def karma_ups_downs(self, kind, sr=None): # NOTE: There is a legacy inconsistency in this method. If no subreddit # is specified, karma from all subreddits will be totaled, with each # scaled according to its karma multiplier before being summed. But if # a subreddit IS specified, the return value will NOT be scaled. assert kind in ('link', 'comment', 'adjustment') from subreddit import Subreddit # prevent circular import # If getting karma for a single sr, it's easy if sr is not None: ups = getattr(self, 'karma_ups_{0}_{1}'.format(kind, sr.name), 0) downs = getattr(self, 'karma_downs_{0}_{1}'.format(kind, sr.name), 0) return (ups, downs) # Otherwise, loop through attributes and sum all karmas totals = [0, 0] for k, v in self._t.iteritems(): for pre, idx in (('karma_ups_' + kind + '_', 0), ('karma_downs_' + kind + '_', 1)): if k.startswith(pre): karma_sr_name = k[len(pre):] index = idx break else: continue multiplier = 1 if kind == 'link': try: karma_sr = Subreddit._by_name(karma_sr_name) multiplier = karma_sr.post_karma_multiplier except NotFound: pass totals[index] += v * multiplier return tuple(totals)
def karma_ups_downs(self, kind, sr = None): # NOTE: There is a legacy inconsistency in this method. If no subreddit # is specified, karma from all subreddits will be totaled, with each # scaled according to its karma multiplier before being summed. But if # a subreddit IS specified, the return value will NOT be scaled. assert kind in ('link', 'comment', 'adjustment') from subreddit import Subreddit # prevent circular import # If getting karma for a single sr, it's easy if sr is not None: ups = getattr(self, 'karma_ups_{0}_{1}'.format(kind, sr.name), 0) downs = getattr(self, 'karma_downs_{0}_{1}'.format(kind, sr.name), 0) return (ups, downs) # Otherwise, loop through attributes and sum all karmas totals = [0, 0] for k, v in self._t.iteritems(): for pre, idx in (('karma_ups_' + kind + '_', 0), ('karma_downs_' + kind + '_', 1)): if k.startswith(pre): karma_sr_name = k[len(pre):] index = idx break else: continue multiplier = 1 if kind == 'link': try: karma_sr = Subreddit._by_name(karma_sr_name) multiplier = karma_sr.post_karma_multiplier except NotFound: pass totals[index] += v * multiplier return tuple(totals)
def add_props(cls, user, wrapped): from r2.lib.template_helpers import add_attr from r2.lib import promote from r2.lib.wrapped import CachedVariable # fetch parent links links = Link._byID(set(l.link_id for l in wrapped), data=True, return_dict=True, stale=True) # get srs for comments that don't have them (old comments) for cm in wrapped: if not hasattr(cm, "sr_id"): cm.sr_id = links[cm.link_id].sr_id subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped), data=True, return_dict=False, stale=True) cids = dict((w._id, w) for w in wrapped) parent_ids = set(cm.parent_id for cm in wrapped if getattr(cm, "parent_id", None) and cm.parent_id not in cids) parents = {} if parent_ids: parents = Comment._byID(parent_ids, data=True, stale=True) can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) if c.user_is_loggedin else set() can_reply_srs.add(promote.get_promote_srid()) min_score = user.pref_min_comment_score profilepage = c.profilepage user_is_admin = c.user_is_admin user_is_loggedin = c.user_is_loggedin focal_comment = c.focal_comment for item in wrapped: # for caching: item.profilepage = c.profilepage item.link = links.get(item.link_id) if item.link._score <= 1 or item.score < 3 or item.link._spam or item._spam or item.author._spam: item.nofollow = True else: item.nofollow = False if not hasattr(item, "subreddit"): item.subreddit = item.subreddit_slow if item.author_id == item.link.author_id and not item.link._deleted: add_attr(item.attribs, "S", link=item.link.make_permalink(item.subreddit)) if not hasattr(item, "target"): item.target = None if item.parent_id: if item.parent_id in cids: item.parent_permalink = "#" + utils.to36(item.parent_id) else: parent = parents[item.parent_id] item.parent_permalink = parent.make_permalink(item.link, item.subreddit) else: item.parent_permalink = None item.can_reply = False if c.can_reply or (item.sr_id in can_reply_srs): age = c.start_time - item._date if age.days < g.REPLY_AGE_LIMIT: item.can_reply = True # not deleted on profile pages, # deleted if spam and not author or admin item.deleted = not profilepage and ( item._deleted or (item._spam and item.author != user and not item.show_spam) ) extra_css = "" if item.deleted: extra_css += "grayed" if not user_is_admin: item.author = DeletedUser() item.body = "[deleted]" if focal_comment == item._id36: extra_css += " border" # don't collapse for admins, on profile pages, or if deleted item.collapsed = (item.score < min_score) and not (profilepage or item.deleted or user_is_admin) item.editted = getattr(item, "editted", False) item.render_css_class = "comment %s" % CachedVariable("time_period") # will get updated in builder item.num_children = 0 item.score_fmt = Score.points item.permalink = item.make_permalink(item.link, item.subreddit) item.is_author = user == item.author item.is_focal = focal_comment == item._id36 # will seem less horrible when add_props is in pages.py from r2.lib.pages import UserText item.usertext = UserText( item, item.body, editable=item.is_author, nofollow=item.nofollow, target=item.target, extra_css=extra_css, ) # Run this last Printable.add_props(user, wrapped)
def _new(cls, author, to, subject, body, ip, parent=None, sr=None, from_sr=False): m = Message(subject=subject, body=body, author_id=author._id, new=True, ip=ip, from_sr=from_sr) m._spam = author._spam if author._spam: g.stats.simple_event('spam.autoremove.message') sr_id = None # check to see if the recipient is a subreddit and swap args accordingly if to and isinstance(to, Subreddit): if from_sr: raise CreationError("Cannot send from SR to SR") to_subreddit = True to, sr = None, to else: to_subreddit = False if sr: sr_id = sr._id if parent: m.parent_id = parent._id if parent.first_message: m.first_message = parent.first_message else: m.first_message = parent._id if parent.sr_id: sr_id = parent.sr_id if not to and not sr_id: raise CreationError("Message created with neither to nor sr_id") if from_sr and not sr_id: raise CreationError("Message sent from_sr without setting sr") m.to_id = to._id if to else None if sr_id is not None: m.sr_id = sr_id m._commit() if sr_id and not sr: sr = Subreddit._byID(sr_id) inbox_rel = [] if sr_id: # if there is a subreddit id, and it's either a reply or # an initial message to an SR, add to the moderator inbox # (i.e., don't do it for automated messages from the SR) if parent or to_subreddit and not from_sr: inbox_rel.append(ModeratorInbox._add(sr, m, 'inbox')) if author.name in g.admins: m.distinguished = 'admin' m._commit() elif sr.is_moderator(author): m.distinguished = 'yes' m._commit() # if there is a "to" we may have to create an inbox relation as well # also, only global admins can be message spammed. if to and (not m._spam or to.name in g.admins): # if the current "to" is not a sr moderator, # they need to be notified if not sr_id or not sr.is_moderator(to): # Record the inbox relation, but don't give the user # an orangered, if they PM themselves. # Don't notify on PMs from blocked users, either orangered = (to.name != author.name and author._id not in to.enemies) inbox_rel.append(Inbox._add(to, m, 'inbox', orangered=orangered)) # find the message originator elif sr_id and m.first_message: first = Message._byID(m.first_message, True) orig = Account._byID(first.author_id, True) # if the originator is not a moderator... if not sr.is_moderator(orig) and orig._id != author._id: inbox_rel.append(Inbox._add(orig, m, 'inbox')) return (m, inbox_rel)
from redditobject import RedditObject except ImportError: from reddit.subreddit import Subreddit from reddit.redditor import Redditor from reddit.redditpost import RedditPost, RedditComment from reddit.redditobject import RedditObject from datetime import datetime import os if __name__ == "__main__": directory = "scraper_cache_%s" % (datetime.now().isoformat()) if not os.path.exists(directory): os.makedirs(directory) create_filepath = lambda x: directory + "/" + x s = Subreddit.get('memes') print('retrieving subreddit posts') s.retrievePosts() while len(s.getPosts()) < 1000 and s.hasSubsequentPages(): print('retrieving additional posts...') s.retrievePosts(use_next=True) posts = s.getPosts() try: for i, p in enumerate(posts): print('downloading image from post %d of %d' % (i + 1, len(posts))) if p.getImageUrl(): ext = p.getImageUrl().split('/')[-1].split('.')[-1] path = create_filepath(s.getName() + '_' + p.getId() + '.' + ext) if not os.path.exists(path):
def subreddits(self): from subreddit import Subreddit return Subreddit.user_subreddits(self)
def subreddit_slow(self): from subreddit import Subreddit if self.sr_id: return Subreddit._byID(self.sr_id)
def main( config_file, client_id, client_secret, username, password, redirect_uri, ): config = Config(config_file) config.setup_logging() logger = verboselogs.VerboseLogger("update") try: logger.info("Initializing the update.") updater = StylesheetAssetsUpdater(config) logger.info("Loading the Subreddit Data file:") updater.load() data = updater.stylesheet_data logger.verbose(f"Loaded data for /r/{data.subreddit_name}") logger.verbose(f"Revision comment: {data.revision_comment}") logger.verbose(f"CSS size: {len(updater.css_content)} bytes") logger.info("Authenticating to Reddit:") subreddit = Subreddit( subreddit_name=data.subreddit_name, client_id=client_id, client_secret=client_secret, username=username, password=password, redirect_uri=redirect_uri, ) logger.verbose(f"Logged in as: {username}") if updater.unused_images: logger.info("Removing unused images:") for unused_image in updater.unused_images.by_reddit_name: logger.verbose(f"Removing image '{unused_image.reddit_name}'") updater.remove_image(subreddit, unused_image) if updater.new_images: logger.info("Uploading new images:") for new_image in updater.new_images.by_reddit_name: logger.verbose("Uploading image '{}' as '{}'".format( new_image.filename, new_image.reddit_name, )) updater.upload_image(subreddit, new_image) logger.info("Updating the Stylesheet:") updater.update_stylesheet(subreddit) logger.verbose("Stylesheet updated successfully.") logger.info("Saving the Data Page (/r/{}/wiki/{}):".format( data.subreddit_name, data.data_page_name, )) updater.save_data_page(subreddit, client_secret) logger.verbose("The Data Page saved successfully.") logger.info("The Reddit Stylesheet update has been completed. Please " "perform a manual validation.") logger.verbose("Make sure there are enough cute anime girls featured " "in the Stylesheet!") except StylesheetException as error: show_traceback = logger.getEffectiveLevel() == logging.DEBUG logger.error(f"Update procedure failed: {error}", exc_info=show_traceback) exit(1) except Exception as error: logger.error(f"An unexpected error has occurred.", exc_info=True) exit(1)
def add_props(cls, user, wrapped): from r2.lib.template_helpers import add_attr from r2.lib import promote #fetch parent links links = Link._byID(set(l.link_id for l in wrapped), data = True, return_dict = True) #get srs for comments that don't have them (old comments) for cm in wrapped: if not hasattr(cm, 'sr_id'): cm.sr_id = links[cm.link_id].sr_id subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped), data=True,return_dict=False) can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) \ if c.user_is_loggedin else set() can_reply_srs.add(promote.PromoteSR._id) min_score = user.pref_min_comment_score cids = dict((w._id, w) for w in wrapped) profilepage = c.profilepage user_is_admin = c.user_is_admin user_is_loggedin = c.user_is_loggedin focal_comment = c.focal_comment for item in wrapped: # for caching: item.profilepage = c.profilepage item.link = links.get(item.link_id) if (item.link._score <= 1 or item.score < 3 or item.link._spam or item._spam or item.author._spam): item.nofollow = True else: item.nofollow = False if not hasattr(item, 'subreddit'): item.subreddit = item.subreddit_slow if item.author_id == item.link.author_id: add_attr(item.attribs, 'S', link = item.link.make_permalink(item.subreddit)) if not hasattr(item, 'target'): item.target = None if hasattr(item, 'parent_id'): if cids.has_key(item.parent_id): item.parent_permalink = '#' + utils.to36(item.parent_id) else: parent = Comment._byID(item.parent_id) item.parent_permalink = parent.make_permalink(item.link, item.subreddit) else: item.parent_permalink = None item.can_reply = (item.sr_id in can_reply_srs) # not deleted on profile pages, # deleted if spam and not author or admin item.deleted = (not profilepage and (item._deleted or (item._spam and item.author != user and not item.show_spam))) extra_css = '' if item.deleted: extra_css += "grayed" if not user_is_admin: item.author = DeletedUser() item.body = '[deleted]' if focal_comment == item._id36: extra_css += " border" # don't collapse for admins, on profile pages, or if deleted item.collapsed = ((item.score < min_score) and not (profilepage or item.deleted or user_is_admin)) item.editted = getattr(item, "editted", False) #will get updated in builder item.num_children = 0 item.score_fmt = Score.points item.permalink = item.make_permalink(item.link, item.subreddit) item.is_author = (user == item.author) item.is_focal = (focal_comment == item._id36) #will seem less horrible when add_props is in pages.py from r2.lib.pages import UserText item.usertext = UserText(item, item.body, editable = item.is_author, nofollow = item.nofollow, target = item.target, extra_css = extra_css) # Run this last Printable.add_props(user, wrapped)
def add_props(cls, user, wrapped): from r2.lib.template_helpers import add_attr, get_domain from r2.lib import promote from r2.lib.wrapped import CachedVariable from r2.lib.pages import WrappedUser #fetch parent links links = Link._byID(set(l.link_id for l in wrapped), data = True, return_dict = True, stale=True) # fetch authors authors = Account._byID(set(l.author_id for l in links.values()), data=True, return_dict=True, stale=True) #get srs for comments that don't have them (old comments) for cm in wrapped: if not hasattr(cm, 'sr_id'): cm.sr_id = links[cm.link_id].sr_id subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped), data=True, return_dict=False, stale=True) cids = dict((w._id, w) for w in wrapped) parent_ids = set(cm.parent_id for cm in wrapped if getattr(cm, 'parent_id', None) and cm.parent_id not in cids) parents = {} if parent_ids: parents = Comment._byID(parent_ids, data=True, stale=True) can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) \ if c.user_is_loggedin else set() can_reply_srs.add(promote.get_promote_srid()) min_score = user.pref_min_comment_score profilepage = c.profilepage user_is_admin = c.user_is_admin user_is_loggedin = c.user_is_loggedin focal_comment = c.focal_comment cname = c.cname site = c.site for item in wrapped: # for caching: item.profilepage = c.profilepage item.link = links.get(item.link_id) if (item.link._score <= 1 or item.score < 3 or item.link._spam or item._spam or item.author._spam): item.nofollow = True else: item.nofollow = False if not hasattr(item, 'subreddit'): item.subreddit = item.subreddit_slow if item.author_id == item.link.author_id and not item.link._deleted: add_attr(item.attribs, 'S', link = item.link.make_permalink(item.subreddit)) if not hasattr(item, 'target'): item.target = "_top" if cname else None if item.parent_id: if item.parent_id in cids: item.parent_permalink = '#' + utils.to36(item.parent_id) else: parent = parents[item.parent_id] item.parent_permalink = parent.make_permalink(item.link, item.subreddit) else: item.parent_permalink = None item.can_reply = False if c.can_reply or (item.sr_id in can_reply_srs): age = datetime.now(g.tz) - item._date if age.days < g.REPLY_AGE_LIMIT: item.can_reply = True # not deleted on profile pages, # deleted if spam and not author or admin item.deleted = (not profilepage and (item._deleted or (item._spam and item.author != user and not item.show_spam))) extra_css = '' if item.deleted: extra_css += "grayed" if not user_is_admin: item.author = DeletedUser() item.body = '[deleted]' if focal_comment == item._id36: extra_css += " border" if profilepage: item.link_author = WrappedUser(authors[item.link.author_id]) item.subreddit_path = item.subreddit.path if cname: item.subreddit_path = ("http://" + get_domain(cname = (site == item.subreddit), subreddit = False)) if site != item.subreddit: item.subreddit_path += item.subreddit.path item.full_comment_path = item.link.make_permalink(item.subreddit) # don't collapse for admins, on profile pages, or if deleted item.collapsed = False if ((item.score < min_score) and not (profilepage or item.deleted or user_is_admin)): item.collapsed = True item.collapsed_reason = _("comment score below threshold") if user_is_loggedin and item.author_id in c.user.enemies: if "grayed" not in extra_css: extra_css += " grayed" item.collapsed = True item.collapsed_reason = _("blocked user") item.editted = getattr(item, "editted", False) item.render_css_class = "comment %s" % CachedVariable("time_period") #will get updated in builder item.num_children = 0 item.score_fmt = Score.points item.permalink = item.make_permalink(item.link, item.subreddit) item.is_author = (user == item.author) item.is_focal = (focal_comment == item._id36) item_age = c.start_time - item._date if item_age.days > g.VOTE_AGE_LIMIT: item.votable = False else: item.votable = True #will seem less horrible when add_props is in pages.py from r2.lib.pages import UserText item.usertext = UserText(item, item.body, editable = item.is_author, nofollow = item.nofollow, target = item.target, extra_css = extra_css) # Run this last Printable.add_props(user, wrapped)
def add_props(cls, user, wrapped): from r2.lib.db import queries #TODO global-ish functions that shouldn't be here? #reset msgtime after this request msgtime = c.have_messages # make sure there is a sr_id set: for w in wrapped: if not hasattr(w, "sr_id"): w.sr_id = None # load the to fields if one exists to_ids = set(w.to_id for w in wrapped if w.to_id is not None) tos = Account._byID(to_ids, True) if to_ids else {} # load the subreddit field if one exists: sr_ids = set(w.sr_id for w in wrapped if w.sr_id is not None) m_subreddits = Subreddit._byID(sr_ids, data = True, return_dict = True) # load the links and their subreddits (if comment-as-message) links = Link._byID(set(l.link_id for l in wrapped if l.was_comment), data = True, return_dict = True) # subreddits of the links (for comment-as-message) l_subreddits = Subreddit._byID(set(l.sr_id for l in links.values()), data = True, return_dict = True) parents = Comment._byID(set(l.parent_id for l in wrapped if l.parent_id and l.was_comment), data = True, return_dict = True) # load the unread list to determine message newness unread = set(queries.get_unread_inbox(user)) msg_srs = set(m_subreddits[x.sr_id] for x in wrapped if x.sr_id is not None and isinstance(x.lookups[0], Message)) # load the unread mod list for the same reason mod_unread = set(queries.get_unread_subreddit_messages_multi(msg_srs)) for item in wrapped: item.to = tos.get(item.to_id) if item.sr_id: item.recipient = (item.author_id != c.user._id) else: item.recipient = (item.to_id == c.user._id) # new-ness is stored on the relation if item.author_id == c.user._id: item.new = False elif item._fullname in unread: item.new = True # wipe new messages if preferences say so, and this isn't a feed # and it is in the user's personal inbox if (item.new and c.user.pref_mark_messages_read and c.extension not in ("rss", "xml", "api", "json")): queries.set_unread(item.lookups[0], c.user, False) else: item.new = (item._fullname in mod_unread and not item.to_id) item.score_fmt = Score.none item.message_style = "" # comment as message: if item.was_comment: link = links[item.link_id] sr = l_subreddits[link.sr_id] item.to_collapse = False item.author_collapse = False item.link_title = link.title item.permalink = item.lookups[0].make_permalink(link, sr=sr) item.link_permalink = link.make_permalink(sr) if item.parent_id: item.subject = _('comment reply') item.message_style = "comment-reply" parent = parents[item.parent_id] item.parent = parent._fullname item.parent_permalink = parent.make_permalink(link, sr) else: item.subject = _('post reply') item.message_style = "post-reply" elif item.sr_id is not None: item.subreddit = m_subreddits[item.sr_id] item.hide_author = False if getattr(item, "from_sr", False): if not (item.subreddit.is_moderator(c.user) or c.user_is_admin): item.author = item.subreddit item.hide_author = True item.is_collapsed = None if not item.new: if item.recipient: item.is_collapsed = item.to_collapse if item.author_id == c.user._id: item.is_collapsed = item.author_collapse if c.user.pref_collapse_read_messages: item.is_collapsed = (item.is_collapsed is not False) if item.author_id in c.user.enemies and not item.was_comment: item.is_collapsed = True if not c.user_is_admin: item.subject = _('[message from blocked user]') item.body = _('[unblock user to see this message]') taglinetext = '' if item.hide_author: taglinetext = _("subreddit message %(author)s sent %(when)s ago") elif item.author_id == c.user._id: taglinetext = _("to %(dest)s sent %(when)s ago") elif item.to_id == c.user._id or item.to_id is None: taglinetext = _("from %(author)s sent %(when)s ago") else: taglinetext = _("to %(dest)s from %(author)s sent %(when)s ago") item.taglinetext = taglinetext item.dest = item.to.name if item.to else "" if item.sr_id: if item.hide_author: item.updated_author = _("via %(subreddit)s") else: item.updated_author = _("%(author)s via %(subreddit)s") else: item.updated_author = '' # Run this last Printable.add_props(user, wrapped)
def add_props(cls, user, wrapped): from r2.lib.db import queries # TODO global-ish functions that shouldn't be here? # reset msgtime after this request msgtime = c.have_messages # make sure there is a sr_id set: for w in wrapped: if not hasattr(w, "sr_id"): w.sr_id = None # load the to fields if one exists to_ids = set(w.to_id for w in wrapped if w.to_id is not None) tos = Account._byID(to_ids, True) if to_ids else {} # load the subreddit field if one exists: sr_ids = set(w.sr_id for w in wrapped if w.sr_id is not None) m_subreddits = Subreddit._byID(sr_ids, data=True, return_dict=True) # load the links and their subreddits (if comment-as-message) links = Link._byID(set(l.link_id for l in wrapped if l.was_comment), data=True, return_dict=True) # subreddits of the links (for comment-as-message) l_subreddits = Subreddit._byID(set(l.sr_id for l in links.values()), data=True, return_dict=True) parents = Comment._byID( set(l.parent_id for l in wrapped if l.parent_id and l.was_comment), data=True, return_dict=True ) # load the inbox relations for the messages to determine new-ness # TODO: query cache? inbox = Inbox._fast_query(c.user, [item.lookups[0] for item in wrapped], ["inbox", "selfreply"]) # we don't care about the username or the rel name inbox = dict((m._fullname, v) for (u, m, n), v in inbox.iteritems() if v) msgs = filter(lambda x: isinstance(x.lookups[0], Message), wrapped) modinbox = ModeratorInbox._fast_query(m_subreddits.values(), msgs, ["inbox"]) # best to not have to eager_load the things def make_message_fullname(mid): return "t%s_%s" % (utils.to36(Message._type_id), utils.to36(mid)) modinbox = dict((make_message_fullname(v._thing2_id), v) for (u, m, n), v in modinbox.iteritems() if v) for item in wrapped: item.to = tos.get(item.to_id) if item.sr_id: item.recipient = item.author_id != c.user._id else: item.recipient = item.to_id == c.user._id # new-ness is stored on the relation if item.author_id == c.user._id: item.new = False elif item._fullname in inbox: item.new = getattr(inbox[item._fullname], "new", False) # wipe new messages if preferences say so, and this isn't a feed # and it is in the user's personal inbox if item.new and c.user.pref_mark_messages_read and c.extension not in ("rss", "xml", "api", "json"): queries.set_unread(inbox[item._fullname]._thing2, c.user, False) elif item._fullname in modinbox: item.new = getattr(modinbox[item._fullname], "new", False) else: item.new = False item.score_fmt = Score.none item.message_style = "" # comment as message: if item.was_comment: link = links[item.link_id] sr = l_subreddits[link.sr_id] item.to_collapse = False item.author_collapse = False item.link_title = link.title item.link_permalink = link.make_permalink(sr) if item.parent_id: item.subject = _("comment reply") item.message_style = "comment-reply" parent = parents[item.parent_id] item.parent = parent._fullname item.parent_permalink = parent.make_permalink(link, sr) else: item.subject = _("post reply") item.message_style = "post-reply" elif item.sr_id is not None: item.subreddit = m_subreddits[item.sr_id] if c.user.pref_no_profanity: item.subject = profanity_filter(item.subject) item.is_collapsed = None if not item.new: if item.recipient: item.is_collapsed = item.to_collapse if item.author_id == c.user._id: item.is_collapsed = item.author_collapse if c.user.pref_collapse_read_messages: item.is_collapsed = item.is_collapsed is not False # Run this last Printable.add_props(user, wrapped)
def add_props(cls, user, wrapped): #fetch parent links links = Link._byID(set(l.link_id for l in wrapped), True) #get srs for comments that don't have them (old comments) for cm in wrapped: if not hasattr(cm, 'sr_id'): cm.sr_id = links[cm.link_id].sr_id subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped), data=True, return_dict=False) can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) if c.user_is_loggedin: min_score = c.user.pref_min_comment_score else: min_score = g.default_min_comment_score cids = dict((w._id, w) for w in wrapped) for item in wrapped: item.link = links.get(item.link_id) if not hasattr(item, 'subreddit'): item.subreddit = item.subreddit_slow if hasattr(item, 'parent_id'): parent = Comment._byID(item.parent_id, data=True) parent_author = Account._byID(parent.author_id, data=True) item.parent_author = parent_author if not c.full_comment_listing and cids.has_key(item.parent_id): item.parent_permalink = '#' + utils.to36(item.parent_id) else: item.parent_permalink = parent.make_anchored_permalink( item.link, item.subreddit) else: item.parent_permalink = None item.parent_author = None item.can_reply = (item.sr_id in can_reply_srs) # Don't allow users to vote on their own comments # Also require user karma to be >= global threshold as anti-sockpuppet measure item.votable = bool(c.user_is_loggedin and c.user != item.author and not item.retracted and c.user.safe_karma >= g.karma_to_vote) if item.votable and c.profilepage: # Can only vote on profile page under certain conditions item.votable = bool( (c.user.safe_karma > g.karma_to_vote_in_overview) and (g.karma_percentage_to_be_voted > item.author.percent_up())) # not deleted on profile pages, # deleted if spam and not author or admin item.deleted = (not c.profilepage and (item._deleted or (item._spam and item.author != c.user and not item.show_spam))) # don't collapse for admins, on profile pages, or if deleted item.collapsed = ( (item.score < min_score) and not (c.profilepage or item.deleted or c.user_is_admin)) if not hasattr(item, 'editted'): item.editted = False #will get updated in builder item.num_children = 0 item.score_fmt = Score.points item.permalink = item.make_permalink(item.link, item.subreddit) item.can_be_deleted = item.can_delete()
def add_props(cls, user, wrapped): from r2.lib.db import queries #TODO global-ish functions that shouldn't be here? #reset msgtime after this request msgtime = c.have_messages # make sure there is a sr_id set: for w in wrapped: if not hasattr(w, "sr_id"): w.sr_id = None # load the to fields if one exists to_ids = set(w.to_id for w in wrapped if w.to_id is not None) tos = Account._byID(to_ids, True) if to_ids else {} # load the subreddit field if one exists: sr_ids = set(w.sr_id for w in wrapped if w.sr_id is not None) m_subreddits = Subreddit._byID(sr_ids, data=True, return_dict=True) # load the links and their subreddits (if comment-as-message) links = Link._byID(set(l.link_id for l in wrapped if l.was_comment), data=True, return_dict=True) # subreddits of the links (for comment-as-message) l_subreddits = Subreddit._byID(set(l.sr_id for l in links.values()), data=True, return_dict=True) parents = Comment._byID(set(l.parent_id for l in wrapped if l.parent_id and l.was_comment), data=True, return_dict=True) # load the inbox relations for the messages to determine new-ness # TODO: query cache? inbox = Inbox._fast_query(c.user, [item.lookups[0] for item in wrapped], ['inbox', 'selfreply']) # we don't care about the username or the rel name inbox = dict( (m._fullname, v) for (u, m, n), v in inbox.iteritems() if v) msgs = filter(lambda x: isinstance(x.lookups[0], Message), wrapped) modinbox = ModeratorInbox._fast_query(m_subreddits.values(), msgs, ['inbox']) # best to not have to eager_load the things def make_message_fullname(mid): return "t%s_%s" % (utils.to36(Message._type_id), utils.to36(mid)) modinbox = dict((make_message_fullname(v._thing2_id), v) for (u, m, n), v in modinbox.iteritems() if v) for item in wrapped: item.to = tos.get(item.to_id) if item.sr_id: item.recipient = (item.author_id != c.user._id) else: item.recipient = (item.to_id == c.user._id) # new-ness is stored on the relation if item.author_id == c.user._id: item.new = False elif item._fullname in inbox: item.new = getattr(inbox[item._fullname], "new", False) # wipe new messages if preferences say so, and this isn't a feed # and it is in the user's personal inbox if (item.new and c.user.pref_mark_messages_read and c.extension not in ("rss", "xml", "api", "json")): queries.set_unread(inbox[item._fullname]._thing2, c.user, False) elif item._fullname in modinbox: item.new = getattr(modinbox[item._fullname], "new", False) else: item.new = False item.score_fmt = Score.none item.message_style = "" # comment as message: if item.was_comment: link = links[item.link_id] sr = l_subreddits[link.sr_id] item.to_collapse = False item.author_collapse = False item.link_title = link.title item.link_permalink = link.make_permalink(sr) if item.parent_id: item.subject = _('comment reply') item.message_style = "comment-reply" parent = parents[item.parent_id] item.parent = parent._fullname item.parent_permalink = parent.make_permalink(link, sr) else: item.subject = _('post reply') item.message_style = "post-reply" elif item.sr_id is not None: item.subreddit = m_subreddits[item.sr_id] if c.user.pref_no_profanity: item.subject = profanity_filter(item.subject) item.is_collapsed = None if not item.new: if item.recipient: item.is_collapsed = item.to_collapse if item.author_id == c.user._id: item.is_collapsed = item.author_collapse if c.user.pref_collapse_read_messages: item.is_collapsed = (item.is_collapsed is not False) # Run this last Printable.add_props(user, wrapped)
def _new(cls, author, to, subject, body, ip, parent=None, sr=None): m = Message(subject=subject, body=body, author_id=author._id, new=True, ip=ip) m._spam = author._spam sr_id = None # check to see if the recipient is a subreddit and swap args accordingly if to and isinstance(to, Subreddit): to_subreddit = True to, sr = None, to else: to_subreddit = False if sr: sr_id = sr._id if parent: m.parent_id = parent._id if parent.first_message: m.first_message = parent.first_message else: m.first_message = parent._id if parent.sr_id: sr_id = parent.sr_id if not to and not sr_id: raise CreationError, "Message created with neither to nor sr_id" m.to_id = to._id if to else None if sr_id is not None: m.sr_id = sr_id m._commit() inbox_rel = None if sr_id and not sr: sr = Subreddit._byID(sr_id) inbox_rel = [] if sr_id: # if there is a subreddit id, and it's either a reply or # an initial message to an SR, add to the moderator inbox # (i.e., don't do it for automated messages from the SR) if parent or to_subreddit: inbox_rel.append(ModeratorInbox._add(sr, m, 'inbox')) if author.name in g.admins: m.distinguished = 'admin' m._commit() elif sr.is_moderator(author): m.distinguished = 'yes' m._commit() # if there is a "to" we may have to create an inbox relation as well # also, only global admins can be message spammed. if to and (not m._spam or to.name in g.admins): # if the current "to" is not a sr moderator, # they need to be notified if not sr_id or not sr.is_moderator(to): # Record the inbox relation, but don't give the user # an orangered, if they PM themselves. # Don't notify on PMs from blocked users, either orangered = (to.name != author.name and author._id not in to.enemies) inbox_rel.append( Inbox._add(to, m, 'inbox', orangered=orangered)) # find the message originator elif sr_id and m.first_message: first = Message._byID(m.first_message, True) orig = Account._byID(first.author_id, True) # if the originator is not a moderator... if not sr.is_moderator(orig) and orig._id != author._id: inbox_rel.append(Inbox._add(orig, m, 'inbox')) return (m, inbox_rel)
def _new(cls, author, to, subject, body, ip, parent=None, sr=None): m = Message( subject=subject, body=body, author_id=author._id, new=True, ip=ip) m._spam = author._spam sr_id = None # check to see if the recipient is a subreddit and swap args accordingly if to and isinstance(to, Subreddit): to_subreddit = True to, sr = None, to else: to_subreddit = False if sr: sr_id = sr._id if parent: m.parent_id = parent._id if parent.first_message: m.first_message = parent.first_message else: m.first_message = parent._id if parent.sr_id: sr_id = parent.sr_id if not to and not sr_id: raise CreationError, "Message created with neither to nor sr_id" m.to_id = to._id if to else None if sr_id is not None: m.sr_id = sr_id m._commit() inbox_rel = None if sr_id and not sr: sr = Subreddit._byID(sr_id) inbox_rel = [] if sr_id: # if there is a subreddit id, and it's either a reply or # an initial message to an SR, add to the moderator inbox # (i.e., don't do it for automated messages from the SR) if parent or to_subreddit: inbox_rel.append(ModeratorInbox._add(sr, m, 'inbox')) if author.name in g.admins: m.distinguished = 'admin' m._commit() elif sr.is_moderator(author): m.distinguished = 'yes' m._commit() # if there is a "to" we may have to create an inbox relation as well # also, only global admins can be message spammed. if to and (not m._spam or to.name in g.admins): # if the current "to" is not a sr moderator, # they need to be notified if not sr_id or not sr.is_moderator(to): inbox_rel.append(Inbox._add(to, m, 'inbox')) # find the message originator elif sr_id and m.first_message: first = Message._byID(m.first_message, True) orig = Account._byID(first.author_id, True) # if the originator is not a moderator... if not sr.is_moderator(orig) and orig._id != author._id: inbox_rel.append(Inbox._add(orig, m, 'inbox')) return (m, inbox_rel)
def add_props(cls, user, wrapped): from r2.lib.template_helpers import add_attr, get_domain from r2.lib import promote from r2.lib.wrapped import CachedVariable from r2.lib.pages import WrappedUser #fetch parent links links = Link._byID(set(l.link_id for l in wrapped), data=True, return_dict=True, stale=True) # fetch authors authors = Account._byID(set(l.author_id for l in links.values()), data=True, return_dict=True, stale=True) #get srs for comments that don't have them (old comments) for cm in wrapped: if not hasattr(cm, 'sr_id'): cm.sr_id = links[cm.link_id].sr_id subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped), data=True, return_dict=False, stale=True) cids = dict((w._id, w) for w in wrapped) parent_ids = set( cm.parent_id for cm in wrapped if getattr(cm, 'parent_id', None) and cm.parent_id not in cids) parents = {} if parent_ids: parents = Comment._byID(parent_ids, data=True, stale=True) can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) \ if c.user_is_loggedin else set() can_reply_srs.add(promote.get_promote_srid()) min_score = user.pref_min_comment_score profilepage = c.profilepage user_is_admin = c.user_is_admin user_is_loggedin = c.user_is_loggedin focal_comment = c.focal_comment cname = c.cname site = c.site for item in wrapped: # for caching: item.profilepage = c.profilepage item.link = links.get(item.link_id) if (item.link._score <= 1 or item.score < 3 or item.link._spam or item._spam or item.author._spam): item.nofollow = True else: item.nofollow = False if not hasattr(item, 'subreddit'): item.subreddit = item.subreddit_slow if item.author_id == item.link.author_id and not item.link._deleted: add_attr(item.attribs, 'S', link=item.link.make_permalink(item.subreddit)) if not hasattr(item, 'target'): item.target = "_top" if cname else None if item.parent_id: if item.parent_id in cids: item.parent_permalink = '#' + utils.to36(item.parent_id) else: parent = parents[item.parent_id] item.parent_permalink = parent.make_permalink( item.link, item.subreddit) else: item.parent_permalink = None item.can_reply = False if c.can_reply or (item.sr_id in can_reply_srs): age = datetime.now(g.tz) - item._date if age.days < g.REPLY_AGE_LIMIT: item.can_reply = True # not deleted on profile pages, # deleted if spam and not author or admin item.deleted = ( not profilepage and (item._deleted or (item._spam and item.author != user and not item.show_spam))) extra_css = '' if item.deleted: extra_css += "grayed" if not user_is_admin: item.author = DeletedUser() item.body = '[deleted]' if focal_comment == item._id36: extra_css += " border" if profilepage: item.link_author = WrappedUser(authors[item.link.author_id]) item.subreddit_path = item.subreddit.path if cname: item.subreddit_path = ("http://" + get_domain( cname=(site == item.subreddit), subreddit=False)) if site != item.subreddit: item.subreddit_path += item.subreddit.path item.full_comment_path = item.link.make_permalink(item.subreddit) # don't collapse for admins, on profile pages, or if deleted item.collapsed = False if ((item.score < min_score) and not (profilepage or item.deleted or user_is_admin)): item.collapsed = True item.collapsed_reason = _("comment score below threshold") if user_is_loggedin and item.author_id in c.user.enemies: if "grayed" not in extra_css: extra_css += " grayed" item.collapsed = True item.collapsed_reason = _("blocked user") item.editted = getattr(item, "editted", False) item.render_css_class = "comment %s" % CachedVariable( "time_period") #will get updated in builder item.num_children = 0 item.score_fmt = Score.points item.permalink = item.make_permalink(item.link, item.subreddit) item.is_author = (user == item.author) item.is_focal = (focal_comment == item._id36) item_age = c.start_time - item._date if item_age.days > g.VOTE_AGE_LIMIT: item.votable = False else: item.votable = True #will seem less horrible when add_props is in pages.py from r2.lib.pages import UserText item.usertext = UserText(item, item.body, editable=item.is_author, nofollow=item.nofollow, target=item.target, extra_css=extra_css) # Run this last Printable.add_props(user, wrapped)
def add_props(cls, user, wrapped): from r2.lib.db import queries #TODO global-ish functions that shouldn't be here? #reset msgtime after this request msgtime = c.have_messages # make sure there is a sr_id set: for w in wrapped: if not hasattr(w, "sr_id"): w.sr_id = None # load the to fields if one exists to_ids = set(w.to_id for w in wrapped if w.to_id is not None) tos = Account._byID(to_ids, True) if to_ids else {} # load the subreddit field if one exists: sr_ids = set(w.sr_id for w in wrapped if w.sr_id is not None) m_subreddits = Subreddit._byID(sr_ids, data=True, return_dict=True) # load the links and their subreddits (if comment-as-message) links = Link._byID( set(l.link_id for l in wrapped if l.was_comment), data=True, return_dict=True) # subreddits of the links (for comment-as-message) l_subreddits = Subreddit._byID( set(l.sr_id for l in links.values()), data=True, return_dict=True) parents = Comment._byID( set(l.parent_id for l in wrapped if l.parent_id and l.was_comment), data=True, return_dict=True) # load the unread list to determine message newness unread = set(queries.get_unread_inbox(user)) msg_srs = set( m_subreddits[x.sr_id] for x in wrapped if x.sr_id is not None and isinstance(x.lookups[0], Message)) # load the unread mod list for the same reason mod_unread = set( queries.merge_results( *[queries.get_unread_subreddit_messages(sr) for sr in msg_srs])) for item in wrapped: item.to = tos.get(item.to_id) if item.sr_id: item.recipient = (item.author_id != c.user._id) else: item.recipient = (item.to_id == c.user._id) # new-ness is stored on the relation if item.author_id == c.user._id: item.new = False elif item._fullname in unread: item.new = True # wipe new messages if preferences say so, and this isn't a feed # and it is in the user's personal inbox if (item.new and c.user.pref_mark_messages_read and c.extension not in ("rss", "xml", "api", "json")): queries.set_unread(item.lookups[0], c.user, False) else: item.new = (item._fullname in mod_unread) item.score_fmt = Score.none item.message_style = "" # comment as message: if item.was_comment: link = links[item.link_id] sr = l_subreddits[link.sr_id] item.to_collapse = False item.author_collapse = False item.link_title = link.title item.permalink = item.lookups[0].make_permalink(link, sr=sr) item.link_permalink = link.make_permalink(sr) if item.parent_id: item.subject = _('comment reply') item.message_style = "comment-reply" parent = parents[item.parent_id] item.parent = parent._fullname item.parent_permalink = parent.make_permalink(link, sr) else: item.subject = _('post reply') item.message_style = "post-reply" elif item.sr_id is not None: item.subreddit = m_subreddits[item.sr_id] if c.user.pref_no_profanity: item.subject = profanity_filter(item.subject) item.is_collapsed = None if not item.new: if item.recipient: item.is_collapsed = item.to_collapse if item.author_id == c.user._id: item.is_collapsed = item.author_collapse if c.user.pref_collapse_read_messages: item.is_collapsed = (item.is_collapsed is not False) # Run this last Printable.add_props(user, wrapped)
def add_props(cls, user, wrapped): #fetch parent links links = Link._byID(set(l.link_id for l in wrapped), True) #get srs for comments that don't have them (old comments) for cm in wrapped: if not hasattr(cm, 'sr_id'): cm.sr_id = links[cm.link_id].sr_id subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped), data=True,return_dict=False) can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) if c.user_is_loggedin: min_score = c.user.pref_min_comment_score else: min_score = g.default_min_comment_score cids = dict((w._id, w) for w in wrapped) for item in wrapped: item.link = links.get(item.link_id) if not hasattr(item, 'subreddit'): item.subreddit = item.subreddit_slow if hasattr(item, 'parent_id'): parent = Comment._byID(item.parent_id, data=True) parent_author = Account._byID(parent.author_id, data=True) item.parent_author = parent_author if not c.full_comment_listing and cids.has_key(item.parent_id): item.parent_permalink = '#' + utils.to36(item.parent_id) else: item.parent_permalink = parent.make_anchored_permalink(item.link, item.subreddit) else: item.parent_permalink = None item.parent_author = None item.can_reply = (item.sr_id in can_reply_srs) # Don't allow users to vote on their own comments # Also require user karma to be >= global threshold as anti-sockpuppet measure item.votable = bool(c.user_is_loggedin and c.user != item.author and not item.retracted and c.user.safe_karma >= g.karma_to_vote) if item.votable and c.profilepage: # Can only vote on profile page under certain conditions item.votable = bool((c.user.safe_karma > g.karma_to_vote_in_overview) and (g.karma_percentage_to_be_voted > item.author.percent_up())) # not deleted on profile pages, # deleted if spam and not author or admin item.deleted = (not c.profilepage and (item._deleted or (item._spam and item.author != c.user and not item.show_spam))) # don't collapse for admins, on profile pages, or if deleted item.collapsed = ((item.score < min_score) and not (c.profilepage or item.deleted or c.user_is_admin)) if not hasattr(item,'editted'): item.editted = False #will get updated in builder item.num_children = 0 item.score_fmt = Score.points item.permalink = item.make_permalink(item.link, item.subreddit) item.can_be_deleted = item.can_delete()
def get_subreddit(self, subreddit_name, *args, **kwargs): """Returns a Subreddit class for the subreddit_name specified.""" return Subreddit(self, subreddit_name, *args, **kwargs)
def main(argv): sub = Subreddit() #sub.getHotTenSubreddit("personalfinance") print(sub.getTopTenSubreddit("personalfinance"))
def add_props(cls, user, wrapped): from r2.lib.template_helpers import add_attr from r2.lib import promote #fetch parent links links = Link._byID(set(l.link_id for l in wrapped), data = True, return_dict = True) #get srs for comments that don't have them (old comments) for cm in wrapped: if not hasattr(cm, 'sr_id'): cm.sr_id = links[cm.link_id].sr_id subreddits = Subreddit._byID(set(cm.sr_id for cm in wrapped), data=True,return_dict=False) cids = dict((w._id, w) for w in wrapped) parent_ids = set(cm.parent_id for cm in wrapped if getattr(cm, 'parent_id', None) and cm.parent_id not in cids) parents = {} if parent_ids: parents = Comment._byID(parent_ids, data=True) can_reply_srs = set(s._id for s in subreddits if s.can_comment(user)) \ if c.user_is_loggedin else set() can_reply_srs.add(promote.get_promote_srid()) min_score = user.pref_min_comment_score profilepage = c.profilepage user_is_admin = c.user_is_admin user_is_loggedin = c.user_is_loggedin focal_comment = c.focal_comment for item in wrapped: # for caching: item.profilepage = c.profilepage item.link = links.get(item.link_id) if (item.link._score <= 1 or item.score < 3 or item.link._spam or item._spam or item.author._spam): item.nofollow = True else: item.nofollow = False if not hasattr(item, 'subreddit'): item.subreddit = item.subreddit_slow if item.author_id == item.link.author_id and not item.link._deleted: add_attr(item.attribs, 'S', link = item.link.make_permalink(item.subreddit)) if not hasattr(item, 'target'): item.target = None if item.parent_id: if item.parent_id in cids: item.parent_permalink = '#' + utils.to36(item.parent_id) else: parent = parents[item.parent_id] item.parent_permalink = parent.make_permalink(item.link, item.subreddit) else: item.parent_permalink = None item.can_reply = False if c.can_reply or (item.sr_id in can_reply_srs): age = c.start_time - item._date if age.days < g.REPLY_AGE_LIMIT: item.can_reply = True # not deleted on profile pages, # deleted if spam and not author or admin item.deleted = (not profilepage and (item._deleted or (item._spam and item.author != user and not item.show_spam))) extra_css = '' if item.deleted: extra_css += "grayed" if not user_is_admin: item.author = DeletedUser() item.body = '[deleted]' if focal_comment == item._id36: extra_css += " border" # don't collapse for admins, on profile pages, or if deleted item.collapsed = ((item.score < min_score) and not (profilepage or item.deleted or user_is_admin)) item.editted = getattr(item, "editted", False) #will get updated in builder item.num_children = 0 item.score_fmt = Score.points item.permalink = item.make_permalink(item.link, item.subreddit) item.is_author = (user == item.author) item.is_focal = (focal_comment == item._id36) #will seem less horrible when add_props is in pages.py from r2.lib.pages import UserText item.usertext = UserText(item, item.body, editable = item.is_author, nofollow = item.nofollow, target = item.target, extra_css = extra_css) # Run this last Printable.add_props(user, wrapped)
def deleted_account_cleanup(data): from r2.models import Subreddit from r2.models.admin_notes import AdminNotesBySystem from r2.models.flair import Flair from r2.models.token import OAuth2Client for account_id36 in data.itervalues(): account = Account._byID36(account_id36, data=True) if not account._deleted: continue # wipe the account's password and email address account.password = "" account.email = "" account.email_verified = False notes = "" # "noisy" rel removals, we'll record all of these in the account's # usernotes in case we need the information later rel_removal_descriptions = { "moderator": "Unmodded", "moderator_invite": "Cancelled mod invite", "contributor": "Removed as contributor", "banned": "Unbanned", "wikibanned": "Un-wikibanned", "wikicontributor": "Removed as wiki contributor", } if account.has_subscribed: rel_removal_descriptions["subscriber"] = "Unsubscribed" for rel_type, description in rel_removal_descriptions.iteritems(): try: ids_fn = getattr(Subreddit, "reverse_%s_ids" % rel_type) sr_ids = ids_fn(account) sr_names = [] srs = Subreddit._byID(sr_ids, data=True, return_dict=False) for subreddit in srs: remove_fn = getattr(subreddit, "remove_" + rel_type) remove_fn(account) sr_names.append(subreddit.name) if description and sr_names: sr_list = ", ".join(sr_names) notes += "* %s from %s\n" % (description, sr_list) except Exception as e: notes += "* Error cleaning up %s rels: %s\n" % (rel_type, e) # silent rel removals, no record left in the usernotes rel_classes = { "flair": Flair, "friend": Friend, "enemy": Friend, } for rel_name, rel_cls in rel_classes.iteritems(): try: rels = rel_cls._query( rel_cls.c._thing2_id == account._id, rel_cls.c._name == rel_name, eager_load=True, ) for rel in rels: remove_fn = getattr(rel._thing1, "remove_" + rel_name) remove_fn(account) except Exception as e: notes += "* Error cleaning up %s rels: %s\n" % (rel_name, e) # add the note with info about the major changes to the account if notes: AdminNotesBySystem.add( system_name="user", subject=account.name, note="Account deletion cleanup summary:\n\n%s" % notes, author="<automated>", when=datetime.now(g.tz), ) account._commit()
import sys from flask import Flask from subreddit import Subreddit app = Flask(__name__) sub = Subreddit() @app.route("/subreddits") def flask_get_subreddits(): return sub.get_subreddits()
def add_props(cls, user, wrapped): from r2.lib.db import queries #TODO global-ish functions that shouldn't be here? #reset msgtime after this request msgtime = c.have_messages # make sure there is a sr_id set: for w in wrapped: if not hasattr(w, "sr_id"): w.sr_id = None # load the to fields if one exists to_ids = set(w.to_id for w in wrapped if w.to_id is not None) tos = Account._byID(to_ids, True) if to_ids else {} # load the subreddit field if one exists: sr_ids = set(w.sr_id for w in wrapped if w.sr_id is not None) m_subreddits = Subreddit._byID(sr_ids, data=True, return_dict=True) # load the links and their subreddits (if comment-as-message) links = Link._byID(set(l.link_id for l in wrapped if l.was_comment), data=True, return_dict=True) # subreddits of the links (for comment-as-message) l_subreddits = Subreddit._byID(set(l.sr_id for l in links.values()), data=True, return_dict=True) parents = Comment._byID(set(l.parent_id for l in wrapped if l.parent_id and l.was_comment), data=True, return_dict=True) # load the unread list to determine message newness unread = set(queries.get_unread_inbox(user)) msg_srs = set( m_subreddits[x.sr_id] for x in wrapped if x.sr_id is not None and isinstance(x.lookups[0], Message)) # load the unread mod list for the same reason mod_unread = set( queries.merge_results( *[queries.get_unread_subreddit_messages(sr) for sr in msg_srs])) for item in wrapped: item.to = tos.get(item.to_id) if item.sr_id: item.recipient = (item.author_id != c.user._id) else: item.recipient = (item.to_id == c.user._id) # new-ness is stored on the relation if item.author_id == c.user._id: item.new = False elif item._fullname in unread: item.new = True # wipe new messages if preferences say so, and this isn't a feed # and it is in the user's personal inbox if (item.new and c.user.pref_mark_messages_read and c.extension not in ("rss", "xml", "api", "json")): queries.set_unread(item.lookups[0], c.user, False) else: item.new = (item._fullname in mod_unread) item.score_fmt = Score.none item.message_style = "" # comment as message: if item.was_comment: link = links[item.link_id] sr = l_subreddits[link.sr_id] item.to_collapse = False item.author_collapse = False item.link_title = link.title item.permalink = item.lookups[0].make_permalink(link, sr=sr) item.link_permalink = link.make_permalink(sr) if item.parent_id: item.subject = _('comment reply') item.message_style = "comment-reply" parent = parents[item.parent_id] item.parent = parent._fullname item.parent_permalink = parent.make_permalink(link, sr) else: item.subject = _('post reply') item.message_style = "post-reply" elif item.sr_id is not None: item.subreddit = m_subreddits[item.sr_id] item.is_collapsed = None if not item.new: if item.recipient: item.is_collapsed = item.to_collapse if item.author_id == c.user._id: item.is_collapsed = item.author_collapse if c.user.pref_collapse_read_messages: item.is_collapsed = (item.is_collapsed is not False) if item.author_id in c.user.enemies and not item.was_comment: item.is_collapsed = True if not c.user_is_admin: item.subject = _('[message from blocked user]') item.body = _('[unblock user to see this message]') # Run this last Printable.add_props(user, wrapped)