Esempio n. 1
0
class ModelLookupView(Select2QuerySetView):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.form = None

    def get_queryset(self):
        self.form = ModelLookupForm(self.request, self.request.GET)
        if self.form.is_valid():
            return self.form.get_queryset()
        else:
            return self.form.model_cls.objects.none()

    def get_results(self, context):
        if self.form and hasattr(self.form, 'cleaned_data'):
            form = self.form
        else:
            form = ModelLookupForm(self.request, self.request.GET)
            if not form.is_valid():
                return []
        out = [{
            'id': self.get_result_value(obj),
            'text': self.get_result_label(obj),
        } for obj in context['object_list']]
        lookup_kwarg = form.cleaned_data['lookup_kwarg']
        params = form.cleaned_data['lookup_params']
        if lookup_kwarg and params:
            params = json.loads(params)
            for obj in out:
                p = params.copy()
                p[lookup_kwarg] = obj['id']
                obj['url'] = '?%s' % urlencode(sorted(p.items()))
        return out
Esempio n. 2
0
def model_lookup_view(request):
    result = {"error": False}

    form = ModelLookupForm(request, request.GET)

    if form.is_valid():
        items, total = form.lookup()
        result["items"] = items
        result["total"] = total
    else:
        result["error"] = True

    return JsonResponse(result)
Esempio n. 3
0
def model_lookup_view(request):
    result = {"error": False}

    form = ModelLookupForm(request.GET)

    if form.is_valid():
        items, total = form.lookup()
        result["items"] = items
        result["total"] = total
    else:
        result["error"] = True

    return JsonResponse(result)
Esempio n. 4
0
def model_lookup_view(request):
    result = {'error': False}

    form = ModelLookupForm(request.GET)

    if form.is_valid():
        items, total = form.lookup()
        result['items'] = items
        result['total'] = total
    else:
        result['error'] = True

    return JsonResponse(result)
Esempio n. 5
0
def model_lookup_view(request):
    result = {'error': False}

    form = ModelLookupForm(request, request.GET)

    if form.is_valid():
        items, total = form.lookup()
        result['items'] = items
        result['total'] = total
    else:
        result['error'] = True

    return JsonResponse(result)
Esempio n. 6
0
def model_lookup_view(request):
    from jet.forms import ModelLookupForm
    from jet.utils import JsonResponse

    result = {
        'error': False
    }

    form = ModelLookupForm(request, request.GET)

    if form.is_valid():
        items, total = form.lookup(request.user)
        result['items'] = items
        result['total'] = total
    else:
        result['error'] = True

    return JsonResponse(result)
Esempio n. 7
0
 def get_results(self, context):
     if self.form and hasattr(self.form, 'cleaned_data'):
         form = self.form
     else:
         form = ModelLookupForm(self.request, self.request.GET)
         if not form.is_valid():
             return []
     out = [{
         'id': self.get_result_value(obj),
         'text': self.get_result_label(obj),
     } for obj in context['object_list']]
     lookup_kwarg = form.cleaned_data['lookup_kwarg']
     params = form.cleaned_data['lookup_params']
     if lookup_kwarg and params:
         params = json.loads(params)
         for obj in out:
             p = params.copy()
             p[lookup_kwarg] = obj['id']
             obj['url'] = '?%s' % urlencode(sorted(p.items()))
     return out