Exemplo n.º 1
0
def check_orgsub(request):
    request_orgsub, is_orgsub_ip, is_orgsub_member = check_request_for_orgsub(request)

    return {
        'request_orgsub': request_orgsub,
        'is_orgsub_ip': is_orgsub_ip,
        'is_orgsub_member': is_orgsub_member,
    }
Exemplo n.º 2
0
def check_orgsub(request):
    request_orgsub, is_orgsub_ip, is_orgsub_member = check_request_for_orgsub(
        request)

    return {
        'request_orgsub': request_orgsub,
        'is_orgsub_ip': is_orgsub_ip,
        'is_orgsub_member': is_orgsub_member,
    }
Exemplo n.º 3
0
def _update_hit_count(request, hitcount):
    '''
    Evaluates a request's Hit and corresponding HitCount object and,
    after a bit of clever logic, either ignores the request or registers
    a new Hit.

    This is NOT a view!  But should be used within a view ...

    Returns True if the request was considered a Hit; returns False if not.i

    NOTE: modified to track orgsubs for rhizome -nh
    '''
    user = request.user
    orgsub, ip_access, member_access = check_request_for_orgsub(request)
    session_key = request.session.session_key
    ip = get_ip(request)
    user_agent = request.META.get('HTTP_USER_AGENT', '')[:255]
    hits_per_ip_limit = getattr(settings, 'HITCOUNT_HITS_PER_IP_LIMIT', 0)
    exclude_user_group = getattr(settings, 
                            'HITCOUNT_EXCLUDE_USER_GROUP', None)

    # first, check our request against the blacklists before continuing
    if BlacklistIP.objects.filter(ip__exact=ip) or \
            BlacklistUserAgent.objects.filter(user_agent__exact=user_agent):
        return False

    # second, see if we are excluding a specific user group or not
    if exclude_user_group and user.is_authenticated():
        if user.groups.filter(name__in=exclude_user_group):
            return False

    #start with a fresh active query set (HITCOUNT_KEEP_HIT_ACTIVE )
    qs = Hit.objects.filter_active() 

    # check limit on hits from a unique ip address (HITCOUNT_HITS_PER_IP_LIMIT)
    if hits_per_ip_limit:
        if qs.filter(ip__exact=ip).count() > hits_per_ip_limit:
            return False

    # create a generic Hit object with request data
    hit = Hit(  session=session_key,
                hitcount=hitcount,
                ip=get_ip(request),
                user_agent=request.META.get('HTTP_USER_AGENT', '')[:255],)

    # add in orgsub information
    hit.orgsub = orgsub
    hit.orgsub_ip = ip_access
    hit.orgsub_member = member_access

    # first, use a user's authentication to see if they made an earlier hit
    if user.is_authenticated():
        if not qs.filter(user=user,hitcount=hitcount):
            hit.user = user #associate this hit with a user
            hit.save()
            return True

    # if not authenticated, see if we have a repeat session
    else:
        if not qs.filter(session=session_key,hitcount=hitcount):
            hit.save()

            # forces a save on this anonymous users session
            request.session.modified = True

            return True

    return False