Added user attributes. I'll be optimistic and not call this "User Attributes Checkin #1"

This commit is contained in:
Mike
2009-07-14 11:16:06 -07:00
parent 09865a2f2b
commit f37cdeaeed
10 changed files with 130 additions and 28 deletions

View File

@@ -307,3 +307,20 @@ def choose_width(link, width):
def panel_size(state):
"the frame.cols of the reddit-toolbar's inner frame"
return '400px, 100%' if state =='expanded' else '0px, 100%x'
def find_author_class(thing, attribs, gray):
#assume attribs is sorted
author = thing.author
author_cls = "author"
extra_class = ''
attribs.sort()
if gray:
author_cls += " gray"
elif attribs:
#the last element in attribs will be the highest priority
extra_class = attribs[-1][2]
author_cls += " " + extra_class
return author_cls

View File

@@ -239,7 +239,6 @@ class Account(Thing):
class FakeAccount(Account):
_nodb = True

View File

@@ -23,9 +23,10 @@ from account import *
from link import *
from vote import *
from report import *
from subreddit import SRMember
from subreddit import SRMember, FakeSubreddit
from listing import Listing
from pylons import i18n, request, g
from pylons.i18n import _
import subreddit
@@ -44,6 +45,36 @@ from admintools import compute_votes, admintools
EXTRA_FACTOR = 1.5
MAX_RECURSION = 10
# Appends to the list "attrs" a tuple of:
# <priority (higher trumps lower), letter,
# css class, i18n'ed mouseover label, hyperlink (or None)>
def add_attr(attrs, code, label=None, link=None):
if code == 'F':
priority = 1
cssclass = 'friend'
if not label:
label = _('friend')
if not link:
link = '/prefs/friends'
elif code == 'M':
priority = 2
cssclass = 'moderator'
if not label:
raise ValueError ("Need a label")
if not link:
raise ValueError ("Need a link")
elif code == 'S':
priority = 3
cssclass = 'submitter'
if not label:
label = _('submitter')
if not link:
raise ValueError ("Need a link")
else:
raise ValueError ("Got weird code [%s]" % code)
attrs.append( (priority, code, cssclass, label, link) )
class Builder(object):
def __init__(self, wrap = Wrapped, keep_fn = None):
self.wrap = wrap
@@ -98,6 +129,21 @@ class Builder(object):
types = {}
wrapped = []
count = 0
if isinstance(c.site, FakeSubreddit):
mods = []
else:
mods = c.site.moderators
modlink = ''
if c.cname:
modlink = '/about/moderators'
else:
modlink = '/r/%s/about/moderators' % c.site.name
modlabel = (_('moderator of /r/%(reddit)s') %
dict(reddit = c.site.name) )
for item in items:
w = self.wrap(item)
wrapped.append(w)
@@ -108,12 +154,26 @@ class Builder(object):
#TODO pull the author stuff into add_props for links and
#comments and messages?
w.author = None
w.friend = False
# List of tuples <priority (higher trumps lower), letter,
# css class, i18n'ed mouseover label, hyperlink (or None)>
w.attribs = []
try:
w.author = authors.get(item.author_id)
w.friend = item.author_id in user.friends if user else False
if user and item.author_id in user.friends:
# deprecated old way:
w.friend = True
# new way:
add_attr(w.attribs, 'F')
except AttributeError:
w.author = None
w.friend = False
pass
if getattr(item, "author_id", None) in mods:
add_attr(w.attribs, 'M', label=modlabel, link=modlink)
if hasattr(item, "sr_id"):
w.subreddit = subreddits[item.sr_id]

View File

@@ -501,6 +501,8 @@ class Comment(Thing, Printable):
@classmethod
def add_props(cls, user, wrapped):
from r2.models.builder import add_attr
#fetch parent links
links = Link._byID(set(l.link_id for l in wrapped), data = True,
@@ -539,6 +541,9 @@ class Comment(Thing, Printable):
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'):

View File

@@ -483,7 +483,9 @@ before enabling */
.tagline { color:#888; font-size:x-small;
}
.tagline a {color: #369; text-decoration: none; }
.tagline a.friend {color: orangered }
.tagline .friend { color: orangered }
.tagline .submitter { color: #0055df }
.tagline .moderator { color: #338833 }
.tagline a:hover { text-decoration: underline }
.media-button .option { color: red; }
@@ -1242,7 +1244,7 @@ textarea.gray { color: gray; }
.vote-note {
padding-left: 3px;
max-width: 170px;
max-width: 150px;
}
.vote-a-notes {
color: red;

View File

@@ -69,7 +69,7 @@ ${parent.collapsed()}
%if thing.deleted:
<em>${_("deleted comment from")}</em>&#32;
%endif
${unsafe(self.author(friend=thing.friend, gray = collapse))}&#32;
${unsafe(self.author(attribs=thing.attribs, gray = collapse))}&#32;
%else:
<em>${_("[deleted]")}</em>&#32;
%endif

View File

@@ -175,7 +175,7 @@
${unsafe(taglinetext % dict(reddit = self.subreddit(),
when = thing.timesince,
author= self.author(friend = thing.friend)))}
author= self.author(attribs = thing.attribs)))}
</%def>
<%def name="child()">

View File

@@ -69,5 +69,5 @@
${unsafe(taglinetext % dict(reddit = self.subreddit(),
domain = self.domain(),
when = thing.timesince,
author= self.author(friend = thing.friend)))}
author= self.author()))}
</%def>

View File

@@ -49,7 +49,7 @@
taglinetext = taglinetext.replace(' ', '&nbsp;')
%>
${unsafe(taglinetext % dict(when = thing.timesince,
author= u"<b>%s</b>" % self.author(thing.friend),
author= u"<b>%s</b>" % self.author(thing.attribs),
dest = u"<b>%s</b>" % thing.to.name))}
</span>
%if c.user_is_admin:

View File

@@ -21,7 +21,7 @@
################################################################################
<%!
from r2.lib.template_helpers import add_sr
from r2.lib.template_helpers import add_sr, find_author_class
from r2.lib.strings import strings
from r2.lib.pages.things import BanButtons
%>
@@ -115,33 +115,52 @@ thing id-${what._fullname}
</div>
</%def>
<%def name="author(friend = False, gray = False)" buffered="True">
<%def name="author(attribs = None, gray = False)" buffered="True">
%if thing.deleted and not c.user_is_admin:
[deleted]
%else:
<%
target = None
if hasattr(thing, "target"):
target = thing.target
attribs.sort()
author_cls = find_author_class(thing, attribs, gray)
target = getattr(thing, "target", None)
disp_name = websafe(thing.author.name)
karma = ""
if c.user_is_admin:
karma = " (%d)" % (thing.author.link_karma)
%>
%if thing.author._deleted:
<span>[deleted]</span>
%else:
<%
author = thing.author
author_cls = "author"
if friend:
author_cls += " friend"
elif gray:
author_cls += " gray"
disp_name = websafe(author.name)
if c.user_is_admin:
disp_name += " (%d)" % (author.link_karma)
%>
${plain_link(disp_name, '/user/%s' % websafe(author.name),
${plain_link(disp_name + karma, "/user/%s" % disp_name,
_class = author_cls, _sr_path = False, target=target)}
%if attribs:
<span class="userattrs">
&#32;[
%for priority, abbv, css_class, label, attr_link in attribs:
%if attr_link:
<a class="${css_class}" title="${label}"
%if target:
target="${target}"
%endif
href="${attr_link}">${abbv}</a>
%else:
<span class="${css_class}" title="${label}">${abbv}</span>
%endif
## author_cls contains the final css_class, so this is a hack
## that will print a comma after all but the final attr
%if css_class not in author_cls:
,
%endif
%endfor
]
</span>
%endif
%endif
%endif
%if c.user_is_admin and hasattr(thing, 'ip') and thing.ip:
<span class="ip" style="display: none;">${thing.ip}</span>
%endif