def cast_vote(user, thing, direction, **data): """Register a vote and queue it for processing.""" update_vote_lookups(user, thing, direction) vote_data = { "user_id": user._id, "thing_fullname": thing._fullname, "direction": direction, "date": int(epoch_timestamp(datetime.now(g.tz))), } data['ip'] = getattr(request, "ip", None) if data['ip'] is not None: data['org'] = organization_by_ips(data['ip']) vote_data['data'] = data hooks.get_hook("vote.get_vote_data").call( data=vote_data["data"], user=user, thing=thing, request=request, context=c, ) # The vote event will actually be sent from an async queue processor, so # we need to pull out the context data at this point if not g.running_as_script: vote_data["event_data"] = { "context": Event.get_context_data(request, c), "sensitive": Event.get_sensitive_context_data(request, c), } amqp.add_item(thing.vote_queue_name, json.dumps(vote_data))
def cast_vote(user, thing, direction, **data): """Register a vote and queue it for processing.""" if not isinstance(thing, (Link, Comment)): return update_vote_lookups(user, thing, direction) vote_data = { "user_id": user._id, "thing_fullname": thing._fullname, "direction": direction, "date": int(epoch_timestamp(datetime.now(g.tz))), } data['ip'] = getattr(request, "ip", None) if data['ip'] is not None: data['org'] = organization_by_ips(data['ip']) vote_data['data'] = data hooks.get_hook("vote.get_vote_data").call( data=vote_data["data"], user=user, thing=thing, request=request, context=c, ) # The vote event will actually be sent from an async queue processor, so # we need to pull out the context data at this point if not g.running_as_script: vote_data["event_data"] = { "context": Event.get_context_data(request, c), "sensitive": Event.get_sensitive_context_data(request, c), } try: vote_dump = json.dumps(vote_data) except UnicodeDecodeError: g.log.error("Got weird unicode in the vote data: %r", vote_data) return if isinstance(thing, Link): queue = "vote_link_q" elif isinstance(thing, Comment): queue = "vote_comment_q" amqp.add_item(queue, vote_dump)
def cast_vote(user, thing, direction, **data): """Register a vote and queue it for processing.""" if not isinstance(thing, (Link, Comment)): return # CUSTOM: voting model, validate direction if direction not in (Vote.DIRECTIONS.up, Vote.DIRECTIONS.down, Vote.DIRECTIONS.unup, Vote.DIRECTIONS.undown): g.log.warning("!!! cast_vote() discarding vote with dir: %s" % direction) return # CUSTOM: voting model, use direction as state # NOTE: vote_direction is tracked in addition to direction for easy updating of _likes, _dislikes, and karma in Vote._commit() vote_direction = direction previous_vote = VoteDetailsByThing.get_vote(user, thing) if previous_vote: if direction == Vote.DIRECTIONS.up: # interesting/liked if previous_vote.is_offonvote: direction = Vote.DIRECTIONS.onon elif previous_vote.is_offoffvote: direction = Vote.DIRECTIONS.onoff # backward compatibility elif previous_vote.is_downvote: direction = Vote.DIRECTIONS.onon else: g.log.warning("!!! cast_vote() up, discarding vote with dir: %s prev dir: %s" % (direction, previous_vote.direction)) return elif direction == Vote.DIRECTIONS.down: # funny/disliked if previous_vote.is_onoffvote: direction = Vote.DIRECTIONS.onon elif previous_vote.is_offoffvote: direction = Vote.DIRECTIONS.offon elif previous_vote.is_offoffvote: direction = Vote.DIRECTIONS.offon # backward compatibility elif previous_vote.is_upvote: direction = Vote.DIRECTIONS.onon else: g.log.warning("!!! cast_vote() down, discarding vote with dir: %s prev dir: %s" % (direction, previous_vote.direction)) return elif direction == Vote.DIRECTIONS.unup: # un-interesting / unliked if previous_vote.is_ononvote: direction = Vote.DIRECTIONS.offon elif previous_vote.is_onoffvote: direction = Vote.DIRECTIONS.offoff # backward compatibility elif previous_vote.is_upvote: direction = Vote.DIRECTIONS.offoff else: g.log.warning("!!! cast_vote() unup, discarding vote with dir: %s prev dir: %s" % (direction, previous_vote.direction)) return elif direction == Vote.DIRECTIONS.undown: # un-funny / undisliked if previous_vote.is_ononvote: direction = Vote.DIRECTIONS.onoff elif previous_vote.is_offonvote: direction = Vote.DIRECTIONS.offoff # backward compatibility elif previous_vote.is_downvote: direction = Vote.DIRECTIONS.offoff else: g.log.warning("!!! cast_vote() undown, discarding vote with dir: %s prev dir: %s" % (direction, previous_vote.direction)) return # first vote else: if direction == Vote.DIRECTIONS.up: direction = Vote.DIRECTIONS.onoff elif direction == Vote.DIRECTIONS.down: direction = Vote.DIRECTIONS.offon else: return # g.log.warning("!!! cast_vote() new Vote with dir: %s vote_dir: %s" % (direction, vote_direction)) update_vote_lookups(user, thing, direction) vote_data = { "user_id": user._id, "thing_fullname": thing._fullname, "direction": direction, "date": int(epoch_timestamp(datetime.now(g.tz))), # CUSTOM: voting model "vote_direction": vote_direction, } data['ip'] = getattr(request, "ip", None) if data['ip'] is not None: data['org'] = organization_by_ips(data['ip']) vote_data['data'] = data hooks.get_hook("vote.get_vote_data").call( data=vote_data["data"], user=user, thing=thing, request=request, context=c, ) # The vote event will actually be sent from an async queue processor, so # we need to pull out the context data at this point if not g.running_as_script: vote_data["event_data"] = { "context": Event.get_context_data(request, c), "sensitive": Event.get_sensitive_context_data(request, c), } try: vote_dump = json.dumps(vote_data) except UnicodeDecodeError: g.log.error("Got weird unicode in the vote data: %r", vote_data) return if isinstance(thing, Link): queue = "vote_link_q" elif isinstance(thing, Comment): queue = "vote_comment_q" amqp.add_item(queue, vote_dump)