<commit_before>"Base classes for lookup creation."

import operator
import re

from django.conf import settings
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse
from django.db.models import Q
from django.utils import simplejson as json
from django.utils.encoding import smart_unicode
from django.utils.translation import ugettext as _

from selectable.forms import BaseLookupForm


__all__ = (
    'LookupBase',
    'ModelLookup',
)


class LookupBase(object):
    "Base class for all django-selectable lookups."

    form = BaseLookupForm

    def _name(cls):
        app_name = cls.__module__.split('.')[-2].lower()
        class_name = cls.__name__.lower()
        name = u'%s-%s' % (app_name, class_name)       
        return name
    name = classmethod(_name)

    def _url(cls):
        return reverse('selectable-lookup', args=[cls.name()])
    url = classmethod(_url)

    def get_query(self, request, term):
        return []

    def get_item_label(self, item):
        return smart_unicode(item)

    def get_item_id(self, item):
        return smart_unicode(item)

    def get_item_value(self, item):
        return smart_unicode(item)

    def get_item(self, value):
        return value

    def create_item(self, value):
        raise NotImplemented()

    def format_item(self, item):
         return {
            'id': self.get_item_id(item),
            'value': self.get_item_value(item),
            'label': self.get_item_label(item)
        }

    def paginate_results(self, request, results, limit):
        paginator = Paginator(results, limit)
        try:
            page = int(request.GET.get('page', '1'))
        except ValueError:
            page = 1
        try:<commit_msg>Conditionally escape results of get_item_id, get_item_value and get_item_label. Fixes #63.<commit_after>"Base classes for lookup creation."

import operator
import re

from django.conf import settings
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse
from django.db.models import Q
from django.utils import simplejson as json
from django.utils.encoding import smart_unicode
from django.utils.html import conditional_escape
from django.utils.translation import ugettext as _

from selectable.forms import BaseLookupForm


__all__ = (
    'LookupBase',
    'ModelLookup',
)


class LookupBase(object):
    "Base class for all django-selectable lookups."

    form = BaseLookupForm

    def _name(cls):
        app_name = cls.__module__.split('.')[-2].lower()
        class_name = cls.__name__.lower()
        name = u'%s-%s' % (app_name, class_name)       
        return name
    name = classmethod(_name)

    def _url(cls):
        return reverse('selectable-lookup', args=[cls.name()])
    url = classmethod(_url)

    def get_query(self, request, term):
        return []

    def get_item_label(self, item):
        return smart_unicode(item)

    def get_item_id(self, item):
        return smart_unicode(item)

    def get_item_value(self, item):
        return smart_unicode(item)

    def get_item(self, value):
        return value

    def create_item(self, value):
        raise NotImplemented()

    def format_item(self, item):
         return {
            'id': conditional_escape(self.get_item_id(item)),
            'value': conditional_escape(self.get_item_value(item)),
            'label': conditional_escape(self.get_item_label(item))
        }

    def paginate_results(self, request, results, limit):
        paginator = Paginator(results, limit)
        try:
            page = int(request.GET.get('page', '1'))
        except ValueError:
            page = 1
        try:
{'fraction_non_alphanumeric': 0.09582059123343527, 'fraction_numerical': 0.0020387359836901123, 'mean_word_length': 2.9969450101832993, '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': '612608', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1125, 'n_words': 302}