From 834e2702bfec89e0b66b66968d68a6acaca9cc6b Mon Sep 17 00:00:00 2001 From: Jason Harvey Date: Tue, 7 May 2013 08:09:43 -0700 Subject: [PATCH] Use the amqp message timestamp to set the Vote date. --- r2/r2/lib/db/queries.py | 14 +++++++++++--- r2/r2/models/vote.py | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/r2/r2/lib/db/queries.py b/r2/r2/lib/db/queries.py index 86133264c..7da4ae658 100755 --- a/r2/r2/lib/db/queries.py +++ b/r2/r2/lib/db/queries.py @@ -47,6 +47,8 @@ from r2.lib.utils import SimpleSillyStub import cPickle as pickle from datetime import datetime +from time import mktime +import pytz import itertools import collections from copy import deepcopy @@ -1454,7 +1456,7 @@ def get_likes(user, items): return res def handle_vote(user, thing, dir, ip, organic, - cheater=False, foreground=False, timer=None): + cheater=False, foreground=False, timer=None, date=None): if timer is None: timer = SimpleSillyStub() @@ -1462,7 +1464,7 @@ def handle_vote(user, thing, dir, ip, organic, from sqlalchemy.exc import IntegrityError try: v = Vote.vote(user, thing, dir, ip, organic, cheater = cheater, - timer=timer) + timer=timer, date=date) except (tdb_sql.CreationError, IntegrityError): g.log.error("duplicate vote for: %s" % str((user, thing, dir))) return @@ -1520,12 +1522,18 @@ def process_votes(qname, limit=0): votee = Thing._by_fullname(tid, data = True) timer.intermediate("preamble") + # Convert the naive timestamp we got from amqplib to a + # timezone aware one. + tt = mktime(msg.timestamp.timetuple()) + date = datetime.utcfromtimestamp(tt).replace(tzinfo=pytz.UTC) + # 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) + cheater = cheater, foreground=True, timer=timer, + date=date) if isinstance(votee, Comment): update_comment_votes([votee]) diff --git a/r2/r2/models/vote.py b/r2/r2/models/vote.py index 7b57acc02..77d6d017c 100644 --- a/r2/r2/models/vote.py +++ b/r2/r2/models/vote.py @@ -153,7 +153,7 @@ class Vote(MultiRelation('vote', @classmethod def vote(cls, sub, obj, dir, ip, organic = False, cheater = False, - timer=None): + timer=None, date=None): from admintools import valid_user, valid_thing, update_score from r2.lib.count import incr_sr_count from r2.lib.db import queries @@ -195,7 +195,7 @@ class Vote(MultiRelation('vote', else: is_new = True oldamount = 0 - v = rel(sub, obj, str(amount)) + v = rel(sub, obj, str(amount), date=date) v.ip = ip old_valid_thing = v.valid_thing = valid_thing(v, karma, cheater = cheater) v.valid_user = (v.valid_thing and valid_user(v, sr, karma)