<commit_before>from django.contrib.auth.models import User, Group
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseForbidden
from django.template.context import RequestContext
from django.db.models.query_utils import Q
from django.shortcuts import render_to_response, get_object_or_404

from object_log.models import LogItem


def list_for_object(request, obj):
    """
    Lists all actions that involve a given object.  This will check
    LogItem.object1, LogItem.object2, and LogItem.object3.

    This view does not
    include any permission checks as it is intend

    @param request: http request
    @param obj: object to retrieve log items for
    """
    content_type = ContentType.objects.get_for_model(obj)

    q = Q(object_type1=content_type, object_id1=obj.pk) \
        | Q(object_type2=content_type, object_id2=obj.pk) \
        | Q(object_type3=content_type, object_id3=obj.pk) \

    log = LogItem.objects.filter(q).distinct()

    return render_to_response('object_log/log.html',
        {'log':log,
         'context':{'user':request.user}
         },
        context_instance=RequestContext(request))


@login_required
def list_for_user(request, pk):
    """
    Provided view for listing actions performed on a user.

    This may only be used by superusers
    """
    if not request.user.is_superuser:
        return HttpResponseForbidden('You are not authorized to view this page')

    user = get_object_or_404(User, pk=pk)
    return list_for_object(request, user)


@login_required
def list_for_group(request, pk):
    """
    Provided view for listing actions performed on a group.

    This may only be used by superusers
    """
    if not request.user.is_superuser:
        return HttpResponseForbidden('You are not authorized to view this page')

    group = get_object_or_404(Group, pk=pk)
    return list_for_object(request, group)


@login_required
def list_user_actions(request, pk):
    """
    List all actions a user has performed.  This view can only be used by
    superusers.

    @param request: HttpRequest
    @param pk: Primary Key of User to get log for.
    """
    if not request.user.is_superuser:
        return HttpResponseForbidden('You are not authorized to view this page')

    user = get_object_or_404(User, pk=pk)
    log_items = LogItem.objects.filter(user=user)

    return render_to_response('object_log/log.html',
        {'log':log_items, 'context':{'user':request.user}},
        context_instance=RequestContext(request))<commit_msg>views for retrieving lists of entries now .select_related('user') so that numerous extra queries are not executed<commit_after>from django.contrib.auth.models import User, Group
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseForbidden
from django.template.context import RequestContext
from django.db.models.query_utils import Q
from django.shortcuts import render_to_response, get_object_or_404

from object_log.models import LogItem


def list_for_object(request, obj):
    """
    Lists all actions that involve a given object.  This will check
    LogItem.object1, LogItem.object2, and LogItem.object3.

    This view does not
    include any permission checks as it is intend

    @param request: http request
    @param obj: object to retrieve log items for
    """
    content_type = ContentType.objects.get_for_model(obj)

    q = Q(object_type1=content_type, object_id1=obj.pk) \
        | Q(object_type2=content_type, object_id2=obj.pk) \
        | Q(object_type3=content_type, object_id3=obj.pk) \

    log = LogItem.objects.filter(q).select_related('user').distinct()

    return render_to_response('object_log/log.html',
        {'log':log,
         'context':{'user':request.user}
         },
        context_instance=RequestContext(request))


@login_required
def list_for_user(request, pk):
    """
    Provided view for listing actions performed on a user.

    This may only be used by superusers
    """
    if not request.user.is_superuser:
        return HttpResponseForbidden('You are not authorized to view this page')

    user = get_object_or_404(User, pk=pk)
    return list_for_object(request, user)


@login_required
def list_for_group(request, pk):
    """
    Provided view for listing actions performed on a group.

    This may only be used by superusers
    """
    if not request.user.is_superuser:
        return HttpResponseForbidden('You are not authorized to view this page')

    group = get_object_or_404(Group, pk=pk)
    return list_for_object(request, group)


@login_required
def list_user_actions(request, pk):
    """
    List all actions a user has performed.  This view can only be used by
    superusers.

    @param request: HttpRequest
    @param pk: Primary Key of User to get log for.
    """
    if not request.user.is_superuser:
        return HttpResponseForbidden('You are not authorized to view this page')

    user = get_object_or_404(User, pk=pk)
    log_items = LogItem.objects.filter(user=user).select_related('user')

    return render_to_response('object_log/log.html',
        {'log':log_items, 'context':{'user':request.user}},
        context_instance=RequestContext(request))
{'fraction_non_alphanumeric': 0.08573029510646246, 'fraction_numerical': 0.007844602166604408, 'mean_word_length': 3.776984834968778, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '615090', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1571, 'n_words': 458}