本文整理汇总了Python中r2.lib.db.thing.Thing类的典型用法代码示例。如果您正苦于以下问题:Python Thing类的具体用法?Python Thing怎么用?Python Thing使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Thing类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, ups = 0, downs = 0, date = None, deleted = False,
spam = False, id = None, descendant_karma = 0, **attrs):
Thing.__init__(self, ups, downs, date, deleted, spam, id, **attrs)
with self.safe_set_attr:
self._descendant_karma = descendant_karma
示例2: _commit
def _commit(self, *a, **kw):
"""Detect when we need to invalidate the sidebar recent posts.
Whenever a post is created we need to invalidate. Also invalidate when
various post attributes are changed (such as moving to a different
subreddit). If the post cache is invalidated the comment one is too.
This is primarily for when a post is banned so that its comments
dissapear from the sidebar too.
"""
should_invalidate = (not self._created or
frozenset(('title', 'sr_id', '_deleted', '_spam')) & frozenset(self._dirties.keys()))
Thing._commit(self, *a, **kw)
if should_invalidate:
g.rendercache.delete('side-posts' + '-' + c.site.name)
g.rendercache.delete('side-comments' + '-' + c.site.name)
tags = self.tag_names()
if 'open_thread' in tags:
g.rendercache.delete('side-open' + '-' + c.site.name)
if 'quotes' in tags:
g.rendercache.delete('side-quote' + '-' + c.site.name)
if 'group_rationality_diary' in tags:
g.rendercache.delete('side-diary' + '-' + c.site.name)
示例3: _commit
def _commit(self, *a, **kw):
"""Detect when we need to invalidate the sidebar recent comments.
Whenever a comment is created we need to invalidate. Also
invalidate when various comment attributes are changed.
"""
should_invalidate = (not self._created or
frozenset(('body', '_deleted', '_spam')) & frozenset(self._dirties.keys()))
Thing._commit(self, *a, **kw)
if should_invalidate:
g.rendercache.delete('side-comments' + '-' + c.site.name)
示例4: _commit
def _commit(self, *a, **kw):
"""Detect when we need to invalidate the sidebar recent posts.
Whenever a post is created we need to invalidate. Also
invalidate when various post attributes are changed (such as
moving to a different subreddit).
"""
should_invalidate = (not self._created or
frozenset(('title', 'sr_id', '_deleted', '_spam')) & frozenset(self._dirties.keys()))
Thing._commit(self, *a, **kw)
if should_invalidate:
g.rendercache.delete('side-posts')
示例5: __getattr__
def __getattr__(self, attr):
val = Thing.__getattr__(self, attr)
if attr in ('start_date', 'end_date'):
val = to_datetime(val)
if not val.tzinfo:
val = val.replace(tzinfo=g.tz)
return val
示例6: _handle_vote
def _handle_vote(msgs, chan):
comments = []
for msg in msgs:
tag = msg.delivery_tag
r = pickle.loads(msg.body)
uid, tid, dir, ip, organic, cheater = r
voter = Account._byID(uid, data=True)
votee = Thing._by_fullname(tid, data = True)
if isinstance(votee, Comment):
comments.append(votee)
if not isinstance(votee, (Link, Comment)):
# I don't know how, but somebody is sneaking in votes
# for subreddits
continue
print (voter, votee, dir, ip, organic, cheater)
try:
handle_vote(voter, votee, dir, ip, organic,
cheater=cheater, foreground=False)
except Exception, e:
print 'Rejecting %r:%r because of %r' % (msg.delivery_tag, r,e)
chan.basic_reject(msg.delivery_tag, requeue=True)
示例7: _handle_vote
def _handle_vote(msg):
r = pickle.loads(msg.body)
uid, tid, dir, ip, organic, cheater = r
voter = Account._byID(uid, data=True)
votee = Thing._by_fullname(tid, data = True)
print (voter, votee, dir, ip, organic, cheater)
handle_vote(voter, votee, dir, ip, organic,
cheater = cheater)
示例8: _handle_vote
def _handle_vote(msgs, chan):
assert(len(msgs) == 1)
msg = msgs[0]
r = pickle.loads(msg.body)
uid, tid, dir, ip, organic, cheater = r
voter = Account._byID(uid, data=True)
votee = Thing._by_fullname(tid, data = True)
print (voter, votee, dir, ip, organic, cheater)
handle_vote(voter, votee, dir, ip, organic,
cheater = cheater)
示例9: _handle_vote
def _handle_vote(msg):
# assert(len(msgs) == 1)
r = pickle.loads(msg.body)
uid, tid, dir, ip, organic, cheater = r
voter = Account._byID(uid, data=True)
votee = Thing._by_fullname(tid, data=True)
if isinstance(votee, Comment):
update_comment_votes([votee])
# I don't know how, but somebody is sneaking in votes
# for subreddits
if isinstance(votee, (Link, Comment)):
print (voter, votee, dir, ip, organic, cheater)
handle_vote(voter, votee, dir, ip, organic, cheater=cheater, foreground=True)
示例10: _handle_vote
def _handle_vote(msgs, chan):
# assert(len(msgs) == 1)
comments = []
for msg in msgs:
r = pickle.loads(msg.body)
uid, tid, dir, ip, organic, cheater = r
voter = Account._byID(uid, data=True)
votee = Thing._by_fullname(tid, data=True)
if isinstance(votee, Comment):
comments.append(votee)
print (voter, votee, dir, ip, organic, cheater)
handle_vote(voter, votee, dir, ip, organic, cheater=cheater)
update_comment_votes(comments)
示例11: from_queue
def from_queue(self, max_date, batch_limit = 50, kind = None):
from r2.models import is_banned_IP, Account, Thing
keep_trying = True
min_id = None
s = self.queue_table
while keep_trying:
where = [s.c.date < max_date]
if min_id:
where.append(s.c.uid > min_id)
if kind:
where.append(s.c.kind == kind)
res = sa.select([s.c.to_addr, s.c.account_id,
s.c.from_name, s.c.fullname, s.c.body,
s.c.kind, s.c.ip, s.c.date, s.c.uid,
s.c.msg_hash, s.c.fr_addr, s.c.reply_to],
sa.and_(*where),
order_by = s.c.uid, limit = batch_limit).execute()
res = res.fetchall()
if not res: break
# batch load user accounts
aids = [x[1] for x in res if x[1] > 0]
accts = Account._byID(aids, data = True,
return_dict = True) if aids else {}
# batch load things
tids = [x[3] for x in res if x[3]]
things = Thing._by_fullname(tids, data = True,
return_dict = True) if tids else {}
# make sure no IPs have been banned in the mean time
ips = set(x[6] for x in res)
ips = dict((ip, is_banned_IP(ip)) for ip in ips)
# get the lower bound date for next iteration
min_id = max(x[8] for x in res)
# did we not fetch them all?
keep_trying = (len(res) == batch_limit)
for (addr, acct, fname, fulln, body, kind, ip, date, uid,
msg_hash, fr_addr, reply_to) in res:
yield (accts.get(acct), things.get(fulln), addr,
fname, date, ip, ips[ip], kind, msg_hash, body,
fr_addr, reply_to)
示例12: _handle_votes
def _handle_votes(msgs, chan):
to_do = []
uids = set()
tids = set()
for x in msgs:
r = pickle.loads(x.body)
uid, tid, dir, ip, organic, cheater = r
print (uid, tid, dir, ip, organic, cheater)
uids.add(uid)
tids.add(tid)
to_do.append((uid, tid, dir, ip, organic, cheater))
users = Account._byID(uids, data=True, return_dict=True)
things = Thing._by_fullname(tids, data=True, return_dict=True)
for uid, tid, dir, ip, organic, cheater in to_do:
handle_vote(users[uid], things[tid], dir, ip, organic, cheater=cheater)
示例13: quota_baskets
def quota_baskets(self, kind):
from r2.models.admintools import filter_quotas
key = self.quota_key(kind)
fnames = g.hardcache.get(key)
if not fnames:
return None
unfiltered = Thing._by_fullname(fnames, data=True, return_dict=False)
baskets, new_quotas = filter_quotas(unfiltered)
if new_quotas is None:
pass
elif new_quotas == []:
g.hardcache.delete(key)
else:
g.hardcache.set(key, new_quotas, 86400 * 30)
return baskets
示例14: _handle_vote
def _handle_vote(msg):
timer = stats.get_timer("service_time." + qname)
timer.start()
# assert(len(msgs) == 1)
r = pickle.loads(msg.body)
uid, tid, dir, ip, organic, cheater = r
voter = Account._byID(uid, data=True)
votee = Thing._by_fullname(tid, data=True)
timer.intermediate("preamble")
if isinstance(votee, Comment):
update_comment_votes([votee])
timer.intermediate("update_comment_votes")
# I don't know how, but somebody is sneaking in votes
# for subreddits
if isinstance(votee, (Link, Comment)):
print (voter, votee, dir, ip, organic, cheater)
handle_vote(voter, votee, dir, ip, organic, cheater=cheater, foreground=True, timer=timer)
示例15: update_flairs
def update_flairs(msg):
"""Add non presser flair to posters/commenters in thebutton SR"""
fullname = msg.body
thing = Thing._by_fullname(fullname)
if (not isinstance(thing, (Comment, Link)) or
thing.sr_id != g.live_config["thebutton_srid"]):
return
author = thing.author_slow
sr = thing.subreddit_slow
if not author.flair_css_class(sr._id):
if author._date < ACCOUNT_CREATION_CUTOFF:
flair_class = g.live_config["thebutton_nopress_flair_class"]
flair_text = g.live_config["thebutton_nopress_flair_text"]
else:
flair_class = g.live_config["thebutton_cantpress_flair_class"]
flair_text = g.live_config["thebutton_cantpress_flair_text"]
if flair_class:
author.set_flair(sr, css_class=flair_class, text=flair_text)