Пример #1
0
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))
Пример #2
0
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)
Пример #3
0
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)
Пример #4
0
def ips_by_account_id(account_id, limit=None):
    ips = IPsByAccount.get(account_id, column_count=limit or 1000)
    flattened_ips = [j for i in ips for j in i.iteritems()]
    locations = location_by_ips(set(ip for _, ip in flattened_ips))
    orgs = organization_by_ips(set(ip for _, ip in flattened_ips))

    # Deduplicate and summarize total usage time
    counts = Counter((ip for _, ip in flattened_ips))
    seen = set()
    results = []
    for visit_time, ip in flattened_ips:
        if ip in seen:
            continue
        results.append(
            (ip, visit_time, locations.get(ip) or {},
                orgs.get(ip), counts.get(ip))
        )
        seen.add(ip)
    return results
Пример #5
0
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)