def global_search(user, search, qs): """ Returns results according to search keyword. It will look in all available fields. """ fks = {'os': 'name', 'site': 'label', 'supplier': 'name', 'type': 'text', 'status': 'name'} # Filtre les foreign key en fonction du niveau de securite. authorized_keys = SearchHostForm.filter_list(user, fks.keys()) fks = dict([(k, v) for k, v in fks.iteritems() if k in authorized_keys]) # Filter local fields fields = SearchHostForm.filter_list(user, SearchHostForm.Meta.fields) qs = qs.filter( ( # Do search on extra_fields Q(additionnalfield__field__fast_search__exact=True) & ~Q(additionnalfield__field__data_type__in=('2', '3', '6')) & Q(additionnalfield__value__icontains=search) ) | ( # Do search only for local fields reduce(ior, (Q(**{"%s__icontains" % (key,): search}) for key in fields if key not in fks.keys())) ) | ( # Do search on filtered foreign keys reduce(ior, (Q(**{"%s__%s__icontains" % (key, value): search}) for key, value in fks.iteritems())) ) ) # Distict is needed because could match 2 fields in the or just above return qs.distinct()
def list_all(request, target_client=None, *args, **kw): """ Vue permettant de lister les machines recherchées. Variables de session utilisées: search_host_form_fields : dernier formulaire de rechere additionnal_field_form_fields : dernier formulaire additionnel sort_adm_list : dernier tri lastpage_clariadmin : dernier numéreau de page """ if request.GET.get("reset", "0") == "1": try: # ordre de nettoyage de session logique. del request.session["lastpage_clariadmin"] del request.session["sort_adm_list"] del request.session["search_host_form_fields"] del request.session["additionnal_field_form_fields"] except KeyError: pass return redirect('list_hosts') if target_client: tmp = request.session.get("search_host_form_fields", {}) tmp["site"] = target_client request.session["search_host_form_fields"] = tmp return redirect('list_hosts') POST = HostForm.filter_querydict(request.user, request.POST) new_search = False form_extra = False sort_default = settings.HOST_DEFAULT_ORDER columns = HostForm.filter_list(request.user, ("hostname", "ip", "site", "type", "os", "model", "status", "additionnal_fields")) if POST: # Init forms form = SearchHostForm(request.user, POST) if form.is_valid(): # récupère les éléments de POST propre à SearchHostForm post_filtred = dict((k, v) for k, v in POST.iteritems() if k in form.cleaned_data.keys()) # si recherche != dernière recherche, # retour page 1 et update session if request.session.get('search_host_form_fields', {}) != post_filtred: new_search = True request.session['search_host_form_fields'] = post_filtred host_type = request.session.get('search_host_form_fields', {}).get('type', False) if host_type: form_extra = AdditionnalFieldForm(POST, host_type=HostType.objects.get(pk=host_type)) # if search != last search => page 1 and update session post_filtred = dict([(k, v) for k, v in POST.iteritems() if k in form_extra.fields.keys()]) if request.session.get('additionnal_field_form_fields', {}) != post_filtred: new_search = True request.session['additionnal_field_form_fields'] = post_filtred form_extra.is_valid() else: host_type = request.session.get('search_host_form_fields', {}).get('type', False) form = SearchHostForm(request.user, request.session.get('search_host_form_fields', {})) if host_type: form_extra = AdditionnalFieldForm( request.session.get('additionnal_field_form_fields', {}), host_type=HostType.objects.get(pk=host_type)) form_extra.is_valid() # filter SearchHostFrom form.fields = SearchHostForm.filter_querydict(request.user, form.fields) # get sorting sorting = sort_default sort_get = request.GET.get('sort', request.session.get("sort_adm_list", sort_default)) if sort_get in columns: sorting = sort_get if sort_get.startswith('-') and sort_get[1:] in columns: sorting = sort_get request.session["sort_adm_list"] = sorting # apply searchs if any. qs = Host.objects.none() if form.is_valid(): search_args = form.cleaned_data if [v for v in search_args.itervalues() if v]: qs = Host.objects.filter_by_user(request.user) if form_extra: qs = filter_hosts(qs, request.user, sorting, form.cleaned_data, form_extra.get_data()) else: qs = filter_hosts(qs, request.user, sorting, form.cleaned_data) form.update(qs) # fill paginator paginator = DiggPaginator(qs, settings.HOSTS_PER_PAGE, body=5, tail=2, padding=2) # get page page_num = 1 page_asked = int(request.GET.get('page', request.session.get('lastpage_clariadmin', 1))) if ((page_asked <= paginator.num_pages) and not new_search): page_num = page_asked request.session["lastpage_clariadmin"] = page_num page = paginator.page(page_num) return render_to_response("clariadmin/list.html", { "page": page, "form": form, "columns": columns, "sorting": sorting, "form_extra": form_extra, }, context_instance=RequestContext(request))