From 2edef55207bfa1034ed159ab367a63c2f9c94589 Mon Sep 17 00:00:00 2001 From: PotatoDumplings Date: Tue, 1 Oct 2013 16:19:37 -0700 Subject: [PATCH 1/5] Descendant karma attribute no longer disappears off python objects. --- r2/r2/lib/db/tdb_sql.py | 86 +++++++++++++++++++++++++++-------------- r2/r2/models/link.py | 32 +++++++++++++-- 2 files changed, 85 insertions(+), 33 deletions(-) diff --git a/r2/r2/lib/db/tdb_sql.py b/r2/r2/lib/db/tdb_sql.py index d8ec2bbc..7e0775d6 100644 --- a/r2/r2/lib/db/tdb_sql.py +++ b/r2/r2/lib/db/tdb_sql.py @@ -129,30 +129,50 @@ def get_rel_type_table(metadata): def get_thing_table(metadata, name): - table = sa.Table(settings.DB_APP_NAME + '_thing_' + name, metadata, - 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)) - if name in ('comment', 'link'): - table.append_column(sa.Column('descendant_karma', - sa.Integer, - default = 0, - nullable = False)) + if name not in ('comment', 'link'): + table = sa.Table(settings.DB_APP_NAME + '_thing_' + name, metadata, + 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)) + else: + table = sa.Table(settings.DB_APP_NAME + '_thing_' + name, metadata, + 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.Column('descendant_karma', + sa.Integer, + default = 0, + nullable = False)) return table @@ -542,11 +562,19 @@ 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) + if type_id in (1, 7): + stor = storage(ups = row.ups, + downs = row.downs, + date = row.date, + deleted = row.deleted, + spam = row.spam, + descendant_karma = row.descendant_karma) + else: + stor = storage(ups = row.ups, + downs = row.downs, + date = row.date, + deleted = row.deleted, + spam = row.spam) if single: res = stor else: diff --git a/r2/r2/models/link.py b/r2/r2/models/link.py index c3eb31fd..958098f4 100644 --- a/r2/r2/models/link.py +++ b/r2/r2/models/link.py @@ -679,6 +679,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. @@ -878,8 +891,7 @@ class Comment(Thing, Printable): banned_before_moderator = False, is_html = False, retracted = False, - show_response_to = False, - _descendant_karma = 0) + show_response_to = False) def _markdown(self): pass @@ -1054,8 +1066,7 @@ 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') + old_val = self._get_item(self._type_id, self._id).descendant_karma comments.append(self._id) @@ -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. From af62cc2ec351eeb798af611f5f4da1bcfc5e2e69 Mon Sep 17 00:00:00 2001 From: PotatoDumplings Date: Fri, 21 Mar 2014 16:41:50 -0700 Subject: [PATCH 2/5] Changed tdb for greater brevity, readablity. --- r2/r2/lib/db/tdb_sql.py | 62 +++++++++++++---------------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/r2/r2/lib/db/tdb_sql.py b/r2/r2/lib/db/tdb_sql.py index 7e0775d6..bbc807a2 100644 --- a/r2/r2/lib/db/tdb_sql.py +++ b/r2/r2/lib/db/tdb_sql.py @@ -129,11 +129,10 @@ def get_rel_type_table(metadata): def get_thing_table(metadata, name): - if name not in ('comment', 'link'): - table = sa.Table(settings.DB_APP_NAME + '_thing_' + name, metadata, - sa.Column('thing_id', BigInteger, primary_key = True), - sa.Column('ups', sa.Integer, default = 0, nullable = False), - sa.Column('downs', + table = sa.Table(settings.DB_APP_NAME + '_thing_' + name, metadata, + 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), @@ -149,30 +148,12 @@ def get_thing_table(metadata, name): sa.DateTime(timezone = True), default = sa.func.now(), nullable = False)) - else: - table = sa.Table(settings.DB_APP_NAME + '_thing_' + name, metadata, - 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.Column('descendant_karma', - sa.Integer, - default = 0, - nullable = False)) + + if name in ('comment', 'link'): + table.append_column(sa.Column('descendant_karma', + sa.Integer, + default = 0, + nullable = False)) return table @@ -562,19 +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: - if type_id in (1, 7): - stor = storage(ups = row.ups, - downs = row.downs, - date = row.date, - deleted = row.deleted, - spam = row.spam, - descendant_karma = row.descendant_karma) - else: - 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: res = stor else: From fd9606a583bff0a6bfcaf924e412a87498153683 Mon Sep 17 00:00:00 2001 From: PotatoDumplings Date: Sun, 23 Mar 2014 19:51:53 -0700 Subject: [PATCH 3/5] Debugging statements for descendant karma --- r2/r2/lib/cache.py | 4 ++-- r2/r2/lib/db/thing.py | 1 + r2/r2/models/link.py | 12 +++++++----- r2/r2/templates/comment.html | 2 ++ 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/r2/r2/lib/cache.py b/r2/r2/lib/cache.py index 98544b84..57214586 100644 --- a/r2/r2/lib/cache.py +++ b/r2/r2/lib/cache.py @@ -72,7 +72,7 @@ def set_multi(self, keys, prefix='', time=0): time = time) def get(self, key, default=None): - r = memcache.Client.get(self, key) + r = None#memcache.Client.get(self, key) if r is None: return default return r @@ -95,7 +95,7 @@ def _check_key(self, key): raise TypeError('Key must be a string.') def get(self, key, default=None): - r = dict.get(self, key) + r = None#dict.get(self, key) if r is None: return default return r diff --git a/r2/r2/lib/db/thing.py b/r2/r2/lib/db/thing.py index 6aacc344..98dc65f9 100644 --- a/r2/r2/lib/db/thing.py +++ b/r2/r2/lib/db/thing.py @@ -263,6 +263,7 @@ def _byID(cls, ids, data=False, return_dict=True, extra_props=None): def items_db(ids): items = cls._get_item(cls._type_id, ids) for i in items.keys(): + print i items[i] = cls._build(i, items[i]) #avoid race condition when incrmenting int props (data int diff --git a/r2/r2/models/link.py b/r2/r2/models/link.py index 958098f4..3d632132 100644 --- a/r2/r2/models/link.py +++ b/r2/r2/models/link.py @@ -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, @@ -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 = '' - def __init__(self, *a, **kw): - Thing.__init__(self, *a, **kw) - @classmethod def by_url_key(cls, url): return base_url(url.lower()).encode('utf8') @@ -689,6 +687,7 @@ def __init__(self, ups = 0, downs = 0, date = None, deleted = False, @classmethod def _build(cls, id, bases): + print "building" return cls(bases.ups, bases.downs, bases.date, bases.deleted, bases.spam, id, bases.descendant_karma) @@ -886,6 +885,8 @@ 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, @@ -1218,6 +1219,7 @@ def __init__(self, ups = 0, downs = 0, date = None, deleted = False, @classmethod def _build(cls, id, bases): + print "building" return cls(bases.ups, bases.downs, bases.date, bases.deleted, bases.spam, id, bases.descendant_karma) diff --git a/r2/r2/templates/comment.html b/r2/r2/templates/comment.html index c25053df..bd6da26d 100644 --- a/r2/r2/templates/comment.html +++ b/r2/r2/templates/comment.html @@ -176,6 +176,8 @@ ## Each comment has a hidden status span that is used to indicate voting errors. %endif + ${thing._get_item(thing._type_id, thing._id)} + ${thing._descendant_karma} ${self.admintagline()} From 1ac42ae4686e36e638227e8fdcabde19e7414e06 Mon Sep 17 00:00:00 2001 From: PotatoDumplings Date: Sun, 23 Mar 2014 20:29:09 -0700 Subject: [PATCH 4/5] removing debugging statements --- r2/r2/lib/cache.py | 4 ++-- r2/r2/lib/db/thing.py | 1 - r2/r2/templates/comment.html | 2 -- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/r2/r2/lib/cache.py b/r2/r2/lib/cache.py index 57214586..98544b84 100644 --- a/r2/r2/lib/cache.py +++ b/r2/r2/lib/cache.py @@ -72,7 +72,7 @@ def set_multi(self, keys, prefix='', time=0): time = time) def get(self, key, default=None): - r = None#memcache.Client.get(self, key) + r = memcache.Client.get(self, key) if r is None: return default return r @@ -95,7 +95,7 @@ def _check_key(self, key): raise TypeError('Key must be a string.') def get(self, key, default=None): - r = None#dict.get(self, key) + r = dict.get(self, key) if r is None: return default return r diff --git a/r2/r2/lib/db/thing.py b/r2/r2/lib/db/thing.py index 98dc65f9..6aacc344 100644 --- a/r2/r2/lib/db/thing.py +++ b/r2/r2/lib/db/thing.py @@ -263,7 +263,6 @@ def _byID(cls, ids, data=False, return_dict=True, extra_props=None): def items_db(ids): items = cls._get_item(cls._type_id, ids) for i in items.keys(): - print i items[i] = cls._build(i, items[i]) #avoid race condition when incrmenting int props (data int diff --git a/r2/r2/templates/comment.html b/r2/r2/templates/comment.html index bd6da26d..c25053df 100644 --- a/r2/r2/templates/comment.html +++ b/r2/r2/templates/comment.html @@ -176,8 +176,6 @@ ## Each comment has a hidden status span that is used to indicate voting errors. %endif - ${thing._get_item(thing._type_id, thing._id)} - ${thing._descendant_karma} ${self.admintagline()} From 636ff181fd20a78d807867f7bc5445eca7a3c593 Mon Sep 17 00:00:00 2001 From: PotatoDumplings Date: Sun, 23 Mar 2014 20:56:45 -0700 Subject: [PATCH 5/5] TDB changed for great readability. Link, comment have descendant karma added to base and int props. --- r2/r2/models/link.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/r2/r2/models/link.py b/r2/r2/models/link.py index 3d632132..c8fc8b5f 100644 --- a/r2/r2/models/link.py +++ b/r2/r2/models/link.py @@ -687,7 +687,6 @@ def __init__(self, ups = 0, downs = 0, date = None, deleted = False, @classmethod def _build(cls, id, bases): - print "building" return cls(bases.ups, bases.downs, bases.date, bases.deleted, bases.spam, id, bases.descendant_karma) @@ -1067,7 +1066,7 @@ 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 = self._get_item(self._type_id, self._id).descendant_karma + old_val = getattr(self, '_descendant_karma') comments.append(self._id) @@ -1219,7 +1218,6 @@ def __init__(self, ups = 0, downs = 0, date = None, deleted = False, @classmethod def _build(cls, id, bases): - print "building" return cls(bases.ups, bases.downs, bases.date, bases.deleted, bases.spam, id, bases.descendant_karma)