<commit_before>from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden, Http404
from django.views.decorators.cache import cache_page
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
from django.contrib.admin.views.decorators import staff_member_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.safestring import mark_safe

import simplejson

from positions.models import Position, PositionContent
from positions.forms import PositionContentOrderForm
from positions import settings as position_settings

def get_admin_url(obj):
    """
    Returns the admin URL for the specified object, so that add and remove
    requests may redirect back to the object for which the operation was
    performed.
    """
    return "/%s/%s/%s/%s" % (position_settings.ADMIN_URL, obj._meta.app_label,
            obj._meta.module_name, obj.id)
 

def widget_data(request, content_type_id, object_id):
    c = ContentType.objects.get(id=content_type_id)
    obj = c.get_object_for_this_type(id=object_id)
    
    positions = Position.objects.get_applicable(obj)
    
    
    data = [pos.name for pos in positions]
    
    return HttpResponse(simplejson.dumps(data),
                                    mimetype='application/json')
    
 
def add(request, position_name, type, id):
    try:
        position = Position.objects.get(name__iexact=position_name)
    except Position.DoesNotExist:
        raise Http404
        
    c = ContentType.objects.get(id=type)
    obj = c.get_object_for_this_type(id=id)
    if position not in Position.objects.get_applicable(obj):
        return HttpResponseForbidden("The object cannot be added to this position.")
    
    Position.objects.add_object(position, obj)
    
    return HttpResponseRedirect(get_admin_url(obj))
add = staff_member_required(add)


def remove(request, position_name, type, id):
    """
    Resolves the object by the specified content type and id, and removes it
    from the specified Position if it has already been added.
    """<commit_msg>Added filter for the query when retrieving the widget data<commit_after>from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden, Http404
from django.views.decorators.cache import cache_page
from django.contrib.contenttypes.models import ContentType
from django.conf import settings
from django.contrib.admin.views.decorators import staff_member_required
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.safestring import mark_safe

import simplejson

from positions.models import Position, PositionContent
from positions.forms import PositionContentOrderForm
from positions import settings as position_settings

def get_admin_url(obj):
    """
    Returns the admin URL for the specified object, so that add and remove
    requests may redirect back to the object for which the operation was
    performed.
    """
    return "/%s/%s/%s/%s" % (position_settings.ADMIN_URL, obj._meta.app_label,
            obj._meta.module_name, obj.id)
 

def widget_data(request, content_type_id, object_id):
    c = ContentType.objects.get(id=content_type_id)
    obj = c.get_object_for_this_type(id=object_id)
    
    positions = Position.objects.get_applicable(obj)
    
    data = [{"id":pos.pk,"name":pos.name,"value":pos.name} for pos in positions if request.GET.get('term', "") in pos.name]
    
    return HttpResponse(simplejson.dumps(data),
                                    mimetype='application/json')
    
 
def add(request, position_name, type, id):
    try:
        position = Position.objects.get(name__iexact=position_name)
    except Position.DoesNotExist:
        raise Http404
        
    c = ContentType.objects.get(id=type)
    obj = c.get_object_for_this_type(id=id)
    if position not in Position.objects.get_applicable(obj):
        return HttpResponseForbidden("The object cannot be added to this position.")
    
    Position.objects.add_object(position, obj)
    
    return HttpResponseRedirect(get_admin_url(obj))
add = staff_member_required(add)


def remove(request, position_name, type, id):
    """
    Resolves the object by the specified content type and id, and removes it
    from the specified Position if it has already been added.
    """
{'fraction_non_alphanumeric': 0.07632718159033948, 'fraction_numerical': 0.002734107997265892, 'mean_word_length': 3.8615725359911406, 'pattern_counts': {'":': 3, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '763548', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1215, 'n_words': 353}