Skip to content
This repository was archived by the owner on Apr 17, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 26 additions & 20 deletions r2/r2/lib/db/tdb_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,22 @@ def get_thing_table(metadata, name):
sa.Column('thing_id', BigInteger, primary_key = True),
sa.Column('ups', sa.Integer, default = 0, nullable = False),
sa.Column('downs',
sa.Integer,
default = 0,
nullable = False),
sa.Column('deleted',
sa.Boolean,
default = False,
nullable = False),
sa.Column('spam',
sa.Boolean,
default = False,
nullable = False),
sa.Column('date',
sa.DateTime(timezone = True),
default = sa.func.now(),
nullable = False))
sa.Integer,
default = 0,
nullable = False),
sa.Column('deleted',
sa.Boolean,
default = False,
nullable = False),
sa.Column('spam',
sa.Boolean,
default = False,
nullable = False),
sa.Column('date',
sa.DateTime(timezone = True),
default = sa.func.now(),
nullable = False))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did these shift all shift four spaces in? I can't see any signs of a tab/space mixup or anything, but it'd be good to have them lined up neatly. Besides, it'll keep the diff of the branch as a whole tidy,

if name in ('comment', 'link'):
table.append_column(sa.Column('descendant_karma',
sa.Integer,
Expand Down Expand Up @@ -542,11 +543,16 @@ def get_thing(type_id, thing_id):
#if single, only return one storage, otherwise make a dict
res = {} if not single else None
for row in r:
stor = storage(ups = row.ups,
downs = row.downs,
date = row.date,
deleted = row.deleted,
spam = row.spam)
kwargs = { 'ups': row.ups,
'downs': row.downs,
'date': row.date,
'deleted': row.deleted,
'spam': row.spam }
if type_id in (types_name["link"].type_id, types_name["comment"].type_id):
kwargs['descendant_karma'] = row.descendant_karma

stor = storage(**kwargs)

if single:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can cut down the duplication here by building a dict and passing it in:

kwargs = { 'ups': row.ups,
           'downs': row.downs,
           'date': row.date,
           'deleted': row.deleted,
           'spam': row.spam }
if type_id in (1, 7): # you said you'd found a better way that doesn't involve magic numbers?
    kwargs['descendant_karma'] = row.descendant_karma

stor = storage(**kwargs)

res = stor
else:
Expand Down
40 changes: 32 additions & 8 deletions r2/r2/models/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class LinkExists(Exception): pass
# defining types
class Link(Thing, Printable, ImageHolder):
_data_int_props = Thing._data_int_props + ('num_comments', 'reported')
_base_props = Thing._base_props + ('_descendant_karma',)
_int_props = Thing._int_props + ('_descendant_karma',)
_defaults = dict(is_self = False,
reported = 0, num_comments = 0,
moderator_banned = False,
Expand All @@ -70,15 +72,11 @@ class Link(Thing, Printable, ImageHolder):
blessed = False,
comments_enabled = True,
notify_on_comment = False,
cc_licensed = False,
_descendant_karma = 0)
cc_licensed = False)

_only_whitespace = re.compile('^\s*$', re.UNICODE)
_more_marker = '<a id="more"></a>'

def __init__(self, *a, **kw):
Thing.__init__(self, *a, **kw)

@classmethod
def by_url_key(cls, url):
return base_url(url.lower()).encode('utf8')
Expand Down Expand Up @@ -679,6 +677,19 @@ def prev_link(self):
q = self._link_nav_query(sort = operators.desc('_date'))
return self._link_for_query(q)

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

@classmethod
def _build(cls, id, bases):
return cls(bases.ups, bases.downs, bases.date,
bases.deleted, bases.spam, id, bases.descendant_karma)

def _commit(self, *a, **kw):
"""Detect when we need to invalidate the sidebar recent posts.

Expand Down Expand Up @@ -873,13 +884,14 @@ class LinkTag(Relation(Link, Tag)):

class Comment(Thing, Printable):
_data_int_props = Thing._data_int_props + ('reported',)
_base_props = Thing._base_props + ('_descendant_karma',)
_int_props = Thing._int_props + ('_descendant_karma',)
_defaults = dict(reported = 0,
moderator_banned = False,
banned_before_moderator = False,
is_html = False,
retracted = False,
show_response_to = False,
_descendant_karma = 0)
show_response_to = False)

def _markdown(self):
pass
Expand Down Expand Up @@ -1054,7 +1066,6 @@ def reply_costs_karma(self):
return self.try_parent(lambda p: p.reply_costs_karma, False)

def incr_descendant_karma(self, comments, amount):

old_val = getattr(self, '_descendant_karma')

comments.append(self._id)
Expand Down Expand Up @@ -1197,6 +1208,19 @@ def add_props(cls, user, wrapped):
item.permalink = item.make_permalink(item.link, item.subreddit)
item.can_be_deleted = item.can_delete()

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

@classmethod
def _build(cls, id, bases):
return cls(bases.ups, bases.downs, bases.date,
bases.deleted, bases.spam, id, bases.descendant_karma)

def _commit(self, *a, **kw):
"""Detect when we need to invalidate the sidebar recent comments.

Expand Down