Ejemplo n.º 1
0
    def get(self, request):
        # pylint: disable=E1101
        #         Instance of <class> has no <member>
        # pylint: disable=W0142
        #         Used * or ** magic

        # import from selectable locally so it's not a hard dependency
        from selectable.forms import AutoCompleteWidget
        from selectable.registry import registry

        data = request.GET
        model_class = get_model(data['app_label'], data['model_name'])
        if not model_class:
            raise Http404('"{0}" model not found'.format(data['model_name']))

        lookup_name = data.get('lookup', None)
        if lookup_name:
            lookup_class = registry.get(lookup_name)
        else:
            lookup_class_name = u'Flyedit{app_label}{model_name}Lookup'.format(
                app_label=data['app_label'].title(),
                model_name=model_class._meta.object_name)
            lookup_name = ('flyedit-{0}'.format(lookup_class_name.lower()))

            lookup_class = registry.get(lookup_name)
            if not lookup_class:
                # create a default lookup class on the fly
                from selectable.base import ModelLookup
                first_charfield = (f for f in model_class._meta.fields
                                   if isinstance(f, CharField)).next()
                search_field = '{0}__icontains'.format(first_charfield.name)
                lookup_class = type(
                    lookup_class_name.encode('ascii'),
                    (ModelLookup,),
                    {'model': model_class,
                     'search_fields': [search_field]})

                registry.register(lookup_class)

        print lookup_class.model
        print lookup_class.search_fields

        # Use a dummy form class to render just the auto-complete field
        class Form(forms.Form):
            autocomplete = forms.CharField(
                label=u'',
                widget=AutoCompleteWidget(lookup_class))
        return HttpResponse(Form()._html_output(
            normal_row='<li class="new item">%(field)s</li>',
            error_row='',
            row_ender='',
            help_text_html='',
            errors_on_separate_row=False))
Ejemplo n.º 2
0
    def get(self, request):
        # pylint: disable=E1101
        #         Instance of <class> has no <member>
        # pylint: disable=W0142
        #         Used * or ** magic

        # import from selectable locally so it's not a hard dependency
        from selectable.forms import AutoCompleteWidget
        from selectable.registry import registry

        data = request.GET
        model_class = get_model(data['app_label'], data['model_name'])
        if not model_class:
            raise Http404('"{0}" model not found'.format(data['model_name']))

        lookup_name = data.get('lookup', None)
        if lookup_name:
            lookup_class = registry.get(lookup_name)
        else:
            lookup_class_name = u'Flyedit{app_label}{model_name}Lookup'.format(
                app_label=data['app_label'].title(),
                model_name=model_class._meta.object_name)
            lookup_name = ('flyedit-{0}'.format(lookup_class_name.lower()))

            lookup_class = registry.get(lookup_name)
            if not lookup_class:
                # create a default lookup class on the fly
                from selectable.base import ModelLookup
                first_charfield = (f for f in model_class._meta.fields
                                   if isinstance(f, CharField)).next()
                search_field = '{0}__icontains'.format(first_charfield.name)
                lookup_class = type(lookup_class_name.encode('ascii'),
                                    (ModelLookup, ), {
                                        'model': model_class,
                                        'search_fields': [search_field]
                                    })

                registry.register(lookup_class)

        print lookup_class.model
        print lookup_class.search_fields

        # Use a dummy form class to render just the auto-complete field
        class Form(forms.Form):
            autocomplete = forms.CharField(
                label=u'', widget=AutoCompleteWidget(lookup_class))

        return HttpResponse(Form()._html_output(
            normal_row='<li class="new item">%(field)s</li>',
            error_row='',
            row_ender='',
            help_text_html='',
            errors_on_separate_row=False))
Ejemplo n.º 3
0
def get_lookup(request, lookup_name):

    lookup_cls = registry.get(lookup_name)
    if lookup_cls is None:
        raise Http404(u'Lookup %s not found' % lookup_name)

    lookup = lookup_cls()
    return lookup.results(request)
Ejemplo n.º 4
0
def get_lookup(request, lookup_name):

    lookup_cls = registry.get(lookup_name)
    if lookup_cls is None:
        raise Http404('Lookup %s not found' % lookup_name)

    lookup = lookup_cls()
    return lookup.results(request)