mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-04-27 03:00:12 -04:00
Refactor distinguish.
Replace detailed calls to `add_attr` with specialized methods for each type of distinguish.
This commit is contained in:
@@ -27,6 +27,7 @@ import urllib
|
||||
from r2.config import feature
|
||||
from r2.models import *
|
||||
from filters import (
|
||||
_force_unicode,
|
||||
_force_utf8,
|
||||
conditional_websafe,
|
||||
keep_space,
|
||||
@@ -614,6 +615,45 @@ def add_attr(attrs, kind, label=None, link=None, cssclass=None, symbol=None):
|
||||
attrs.append( (priority, symbol, cssclass, label, link) )
|
||||
|
||||
|
||||
def add_admin_distinguish(distinguish_attribs_list):
|
||||
add_attr(distinguish_attribs_list, 'A')
|
||||
|
||||
|
||||
def add_moderator_distinguish(distinguish_attribs_list, subreddit):
|
||||
link = '/r/%s/about/moderators' % subreddit.name
|
||||
label = _('moderator of /r/%(reddit)s, speaking officially')
|
||||
label %= {'reddit': subreddit.name}
|
||||
add_attr(distinguish_attribs_list, 'M', label=label, link=link)
|
||||
|
||||
|
||||
def add_friend_distinguish(distinguish_attribs_list, note=None):
|
||||
if note:
|
||||
label = u"%s (%s)" % (_("friend"), _force_unicode(note))
|
||||
else:
|
||||
label = None
|
||||
add_attr(distinguish_attribs_list, 'F', label)
|
||||
|
||||
|
||||
def add_cakeday_distinguish(distinguish_attribs_list, user):
|
||||
label = _("%(user)s just celebrated a reddit birthday!")
|
||||
label %= {"user": user.name}
|
||||
link = "/user/%s" % user.name
|
||||
add_attr(distinguish_attribs_list, kind="cake", label=label, link=link)
|
||||
|
||||
|
||||
def add_special_distinguish(distinguish_attribs_list, user):
|
||||
args = user.special_distinguish()
|
||||
args.pop('name')
|
||||
if not args.get('kind'):
|
||||
args['kind'] = 'special'
|
||||
add_attr(distinguish_attribs_list, **args)
|
||||
|
||||
|
||||
def add_submitter_distinguish(distinguish_attribs_list, link, subreddit):
|
||||
permalink = link.make_permalink(subreddit)
|
||||
add_attr(distinguish_attribs_list, 'S', link=permalink)
|
||||
|
||||
|
||||
def search_url(query, subreddit, restrict_sr="off", sort=None, recent=None, ref=None):
|
||||
import urllib
|
||||
query = _force_utf8(query)
|
||||
|
||||
@@ -91,7 +91,13 @@ class Builder(object):
|
||||
|
||||
def wrap_items(self, items):
|
||||
from r2.lib.db import queries
|
||||
from r2.lib.template_helpers import add_attr
|
||||
from r2.lib.template_helpers import (
|
||||
add_friend_distinguish,
|
||||
add_admin_distinguish,
|
||||
add_moderator_distinguish,
|
||||
add_cakeday_distinguish,
|
||||
add_special_distinguish,
|
||||
)
|
||||
|
||||
user = c.user if c.user_is_loggedin else None
|
||||
aids = set(l.author_id for l in items if hasattr(l, 'author_id')
|
||||
@@ -121,13 +127,6 @@ class Builder(object):
|
||||
types = {}
|
||||
wrapped = []
|
||||
|
||||
modlink = {}
|
||||
modlabel = {}
|
||||
for s in subreddits.values():
|
||||
modlink[s._id] = '/r/%s/about/moderators' % s.name
|
||||
modlabel[s._id] = (_('moderator of /r/%(reddit)s, '
|
||||
'speaking officially') % {'reddit': s.name})
|
||||
|
||||
for item in items:
|
||||
w = self.wrap(item)
|
||||
wrapped.append(w)
|
||||
@@ -139,9 +138,6 @@ class Builder(object):
|
||||
w.author = None
|
||||
w.friend = False
|
||||
|
||||
# List of tuples (see add_attr() for details)
|
||||
w.attribs = []
|
||||
|
||||
w.distinguished = None
|
||||
if hasattr(item, "distinguished"):
|
||||
if item.distinguished == 'yes':
|
||||
@@ -150,60 +146,43 @@ class Builder(object):
|
||||
'gold', 'gold-auto'):
|
||||
w.distinguished = item.distinguished
|
||||
|
||||
try:
|
||||
if getattr(item, "author_id", None):
|
||||
w.author = authors.get(item.author_id)
|
||||
author_id = item.author_id
|
||||
|
||||
# if display_author exists, then author_id is unknown to the
|
||||
# receiver, so don't display friend relationship details
|
||||
if hasattr(item, 'display_author') and item.display_author:
|
||||
author_id = item.display_author
|
||||
if user and author_id in user.friends:
|
||||
# deprecated old way:
|
||||
w.friend = True
|
||||
|
||||
# new way:
|
||||
label = None
|
||||
if friend_rels:
|
||||
rel = friend_rels[author_id]
|
||||
note = getattr(rel, "note", None)
|
||||
if note:
|
||||
label = u"%s (%s)" % (_("friend"),
|
||||
_force_unicode(note))
|
||||
add_attr(w.attribs, 'F', label)
|
||||
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
if (w.distinguished == 'admin' and w.author):
|
||||
add_attr(w.attribs, 'A')
|
||||
|
||||
if w.distinguished == 'moderator':
|
||||
add_attr(w.attribs, 'M', label=modlabel[item.sr_id],
|
||||
link=modlink[item.sr_id])
|
||||
|
||||
if w.distinguished == 'special':
|
||||
args = w.author.special_distinguish()
|
||||
args.pop('name')
|
||||
if not args.get('kind'):
|
||||
args['kind'] = 'special'
|
||||
add_attr(w.attribs, **args)
|
||||
|
||||
# if display_author exists, then author_id is unknown to the
|
||||
# receiver, so don't display the cake day
|
||||
if (not hasattr(item, 'display_author') and
|
||||
w.author and w.author._id in cakes and not c.profilepage):
|
||||
add_attr(
|
||||
w.attribs,
|
||||
kind="cake",
|
||||
label=(_("%(user)s just celebrated a reddit birthday!") %
|
||||
{"user": w.author.name}),
|
||||
link="/user/%s" % w.author.name,
|
||||
)
|
||||
|
||||
if hasattr(item, "sr_id") and item.sr_id is not None:
|
||||
w.subreddit = subreddits[item.sr_id]
|
||||
|
||||
distinguish_attribs_list = []
|
||||
|
||||
# if display_author exists, then w.author is unknown to the
|
||||
# receiver, so we can't check for friend or cakeday
|
||||
author_is_hidden = hasattr(item, 'display_author')
|
||||
|
||||
if (not author_is_hidden and
|
||||
user and w.author and w.author._id in user.friends):
|
||||
w.friend = True # TODO: deprecated?
|
||||
|
||||
if item.author_id in friend_rels:
|
||||
note = getattr(friend_rels[w.author._id], "note", None)
|
||||
else:
|
||||
note = None
|
||||
add_friend_distinguish(distinguish_attribs_list, note)
|
||||
|
||||
if (w.distinguished == 'admin' and w.author):
|
||||
add_admin_distinguish(distinguish_attribs_list)
|
||||
|
||||
if w.distinguished == 'moderator':
|
||||
add_moderator_distinguish(distinguish_attribs_list, w.subreddit)
|
||||
|
||||
if w.distinguished == 'special':
|
||||
add_special_distinguish(distinguish_attribs_list, w.author)
|
||||
|
||||
if (not author_is_hidden and
|
||||
w.author and w.author._id in cakes and not c.profilepage):
|
||||
add_cakeday_distinguish(distinguish_attribs_list, w.author)
|
||||
|
||||
w.attribs = distinguish_attribs_list
|
||||
|
||||
user_vote_dir = likes.get((user, item))
|
||||
|
||||
if user_vote_dir == Vote.DIRECTIONS.up:
|
||||
@@ -1444,7 +1423,6 @@ class UserListBuilder(QueryBuilder):
|
||||
|
||||
class SavedBuilder(IDBuilder):
|
||||
def wrap_items(self, items):
|
||||
from r2.lib.template_helpers import add_attr
|
||||
categories = LinkSavesByAccount.fast_query(c.user, items).items()
|
||||
categories += CommentSavesByAccount.fast_query(c.user, items).items()
|
||||
categories = {item[1]._id: category for item, category in categories if category}
|
||||
|
||||
@@ -1398,7 +1398,7 @@ class Comment(Thing, Printable):
|
||||
|
||||
@classmethod
|
||||
def add_props(cls, user, wrapped):
|
||||
from r2.lib.template_helpers import add_attr, get_domain
|
||||
from r2.lib.template_helpers import add_submitter_distinguish, get_domain
|
||||
from r2.lib.utils import timeago
|
||||
from r2.lib.wrapped import CachedVariable
|
||||
from r2.lib.pages import WrappedUser
|
||||
@@ -1479,9 +1479,10 @@ class Comment(Thing, Printable):
|
||||
|
||||
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))
|
||||
add_submitter_distinguish(item.attribs, item.link, item.subreddit)
|
||||
|
||||
if not hasattr(item, 'target'):
|
||||
item.target = "_top" if cname else None
|
||||
|
||||
|
||||
@@ -24,7 +24,11 @@
|
||||
from r2.lib.filters import safemarkdown, websafe, conditional_websafe
|
||||
from r2.lib.pages.things import MessageButtons
|
||||
from r2.lib.pages import WrappedUser
|
||||
from r2.lib.template_helpers import static, add_attr, format_html
|
||||
from r2.lib.template_helpers import static, format_html
|
||||
from r2.lib.template_helpers import (
|
||||
add_admin_distinguish,
|
||||
add_moderator_distinguish,
|
||||
)
|
||||
from r2.models import Account
|
||||
%>
|
||||
|
||||
@@ -91,13 +95,9 @@
|
||||
to_attribs = []
|
||||
if thing.sr_id and not thing.was_comment:
|
||||
if thing.to.name in g.admins:
|
||||
# distinguish admins in admin modmail
|
||||
add_attr(to_attribs, 'A')
|
||||
add_admin_distinguish(to_attribs)
|
||||
elif thing.to_is_moderator:
|
||||
link = '/r/%s/about/moderators' % thing.subreddit.name
|
||||
label = _('moderator of /r/%(reddit)s, speaking officially')
|
||||
label %= {'reddit': thing.subreddit.name}
|
||||
add_attr(to_attribs, 'M', label=label, link=link)
|
||||
add_moderator_distinguish(to_attribs, thing.subreddit)
|
||||
substitutions['dest'] = format_html(u'<span class="recipient">%s</span>', WrappedUser(thing.to, to_attribs, thing))
|
||||
elif thing.sr_id:
|
||||
substitutions['dest'] = format_html(u'<span class="recipient subreddit"><a href="%(path)s">%(path)s</a></span>', path=thing.subreddit.path)
|
||||
|
||||
Reference in New Issue
Block a user