<gh_stars>1-10
from operator import itemgetter

from django.shortcuts import render
from django.contrib.postgres.search import SearchVector

from activities.models import Activity, MetadataOption,ActivityTranslation
from .forms import SearchForm


def _pimp_facets(facets):

    # create a cache of MetadataOption
    options = {}
    for obj in MetadataOption.objects.all():
        options[(obj.code, obj.group)] = (obj.code, obj.title, obj.position)

    # go through the search result facets, to sort them and add the title
    for facet, values in facets['fields'].items():
        if facet != 'keywords':
            new_values = []
            for code, count in values:
                # obj = MetadataOption.objects.get(code=code, group=facet)
                # new_values.append((code, obj.title, count, obj.position))
                opt = options[(code, facet)]
                new_values.append((code, opt[1], count, opt[2]))
            # sort by the position field
            facets['fields'][facet] = sorted(new_values, key=itemgetter(3))

    return facets

def simplesearch(request):
    form = SearchForm(request.GET)
    if form.is_valid():
        search_query = form.cleaned_data['q']
        search_result = ActivityTranslation.objects.annotate(search=SearchVector('title','description', 'keywords')).filter(search=search_query)
        context = {
            'query': search_query,
            'page': {'object_list': search_result},
            'request': request,
            'form': form,
        }
    else:
        context = {
            'request': request,
            'form': form,
        }

    if 'page' not in context or not context['page']['object_list']:
        context['featured'] = Activity.objects.featured().active_translations()[0:3]

    return render(request, 'search/simplesearch.html', context)

def search(request):
    form = SearchForm(request.GET)
    if form.is_valid():
        search_query = form.cleaned_data['q']
        search_result = whoosh_utils.search(search_query, request.LANGUAGE_CODE, queryfacets=form.cleaned_data)
        context = {
            'query': search_query,
            'facets': _pimp_facets(search_result['facets']),
            'page': {'object_list': search_result['results']},
            'request': request,
            'form': form,
        }
    else:
        context = {
            'request': request,
            'form': form,
        }

    if 'page' not in context or not context['page']['object_list']:
        context['featured'] = Activity.objects.featured().active_translations()[0:3]

    return render(request, 'search/search.html', context)

from operator import itemgetter

from django.shortcuts import render
from django.contrib.postgres.search import SearchVector

from activities.models import Activity, MetadataOption,ActivityTranslation
from .forms import SearchForm


def _pimp_facets(facets):

    # create a cache of MetadataOption
    options = {}
    for obj in MetadataOption.objects.all():
        options[(obj.code, obj.group)] = (obj.code, obj.title, obj.position)

    # go through the search result facets, to sort them and add the title
    for facet, values in facets['fields'].items():
        if facet != 'keywords':
            new_values = []
            for code, count in values:
                # obj = MetadataOption.objects.get(code=code, group=facet)
                # new_values.append((code, obj.title, count, obj.position))
                opt = options[(code, facet)]
                new_values.append((code, opt[1], count, opt[2]))
            # sort by the position field
            facets['fields'][facet] = sorted(new_values, key=itemgetter(3))

    return facets

def simplesearch(request):
    form = SearchForm(request.GET)
    if form.is_valid():
        search_query = form.cleaned_data['q']
        search_result = ActivityTranslation.objects.annotate(search=SearchVector('title','description', 'keywords')).filter(search=search_query)
        context = {
            'query': search_query,
            'page': {'object_list': search_result},
            'request': request,
            'form': form,
        }
    else:
        context = {
            'request': request,
            'form': form,
        }

    if 'page' not in context or not context['page']['object_list']:
        context['featured'] = Activity.objects.featured().active_translations()[0:3]

    return render(request, 'search/simplesearch.html', context)

def search(request):
    form = SearchForm(request.GET)
    if form.is_valid():
        search_query = form.cleaned_data['q']
        search_result = whoosh_utils.search(search_query, request.LANGUAGE_CODE, queryfacets=form.cleaned_data)
        context = {
            'query': search_query,
            'facets': _pimp_facets(search_result['facets']),
            'page': {'object_list': search_result['results']},
            'request': request,
            'form': form,
        }
    else:
        context = {
            'request': request,
            'form': form,
        }

    if 'page' not in context or not context['page']['object_list']:
        context['featured'] = Activity.objects.featured().active_translations()[0:3]

    return render(request, 'search/search.html', context)
# create a cache of MetadataOption # go through the search result facets, to sort them and add the title # obj = MetadataOption.objects.get(code=code, group=facet) # new_values.append((code, obj.title, count, obj.position)) # sort by the position field