Ejemplo n.º 1
0
def effsearch_csv(request):
    if not request.GET.get('query'):
        return HttpResponseRedirect(reverse('eff_search'))

    q = request.GET.get('query')
    out = StringIO.StringIO()
    cols = [
        'name', 
        'country',
        'area1'
        'area2',
        'axisText',
        'measureText',
        'actionText',
        'projectDescription',
        'amountEuAllocatedEuro',
        'amountEuPaymentEuro',
        'amountTotalAllocatedEuro',
        'amountTotalPaymentEuro',
        'yearAllocated',
        ]
    csv_out = csv.DictWriter(out, cols, extrasaction='ignore')
    backend = SearchBackend()
    query = backend.parse_query(q)
    results = backend.search(query, end_offset=200)
    csv_out.writerow(dict([[c,c] for c in cols]))
    for result in results['results']:
        line_data = {}
        for k,v in result.__dict__.items():
            if not v:
                v = ''
            if type(v) in [unicode, str]:
                v = v.encode('utf8')
            line_data[k] = v

        csv_out.writerow(line_data)
        
    res = HttpResponse(out.getvalue(), content_type="application/csv")
    res['Content-Disposition'] = 'attachment; filename="EFF Data [search].csv"' % q.encode('utf8')
    return res
Ejemplo n.º 2
0
def effsearch(request):
    
    page = totals = results = facets = results_count = sort = q = None
    filter_types = sort_by =['']
    
    if request.GET:
        form = EffSearchForm(request.GET)
        if form.is_valid():

            q = form.cleaned_data['query']

            if request.GET.get('yeara'):
                q = "%s AND yeara:%s" % (q, request.GET.get('yeara'))
                filter_types.append('yeara')
            if request.GET.get('country'):
                q = "%s AND country_exact:%s" % (q, request.GET.get('country').lower())
                filter_types.append('country')

            backend = SearchBackend()
            query = backend.parse_query(q)
            
            sort_by = [request.GET.get('sort', 'name'),]
            if sort_by[0].startswith('-'):
                sort = "asc"
            else:
                sort = "desc"
            
            results = backend.search(query, facets=['country_exact', 'yeara'], sort_by=sort_by)
            # results = backend.search(query)

            results_count = results['hits']
            facets = results['facets']
            amountEuAllocatedEuro = \
            amountEuPaymentEuro = \
            amountTotalAllocatedEuro = \
            amountTotalPaymentEuro = 0.0
            
            totals = {}
            cache_key = "result_cache::%s" % q
            totals = r.hgetall(cache_key)
            
            
            if totals:
                for k,v in totals.items():
                    totals[k] = float(v)
            else:
                for result in results['results']:
                    amountEuAllocatedEuro = amountEuAllocatedEuro + result.amountEuAllocatedEuro
                    amountEuPaymentEuro = amountEuPaymentEuro + result.amountEuPaymentEuro
                    amountTotalAllocatedEuro = amountTotalAllocatedEuro + result.amountTotalAllocatedEuro
                    amountTotalPaymentEuro = amountTotalPaymentEuro + result.amountTotalPaymentEuro
                                        
                    totals['amountEuAllocatedEuro']     = round(amountEuAllocatedEuro)
                    totals['amountEuPaymentEuro']       = round(amountEuPaymentEuro)
                    totals['amountTotalAllocatedEuro']  = round(amountTotalAllocatedEuro)
                    totals['amountTotalPaymentEuro']    = round(amountTotalPaymentEuro)
                for k,v in totals.items():
                    r.hset(cache_key, k, v)
                r.expire(cache_key, 60*60*24*7) # Expire in one week

            page = Paginator(results['results'], 25).page(request.GET.get('page', 1) or 1)

    else: 
        form = EffSearchForm()
    
    try:
        side_bar_help = MultilingualFlatPage.objects.get_or_create(url='/eff/help/', title='EFF Help')[0]
    except MultilingualFlatPage.DoesNotExist:
        side_bar_help = MultilingualFlatPage()

    try:
        intro_text = MultilingualFlatPage.objects.get_or_create(url='/eff/intro/', title='EFF Intro')[0]
    except MultilingualFlatPage.DoesNotExist:
        intro_text = MultilingualFlatPage()

    return render_to_response(
      'eff_search/search.html', 
      {
          'intro_text' : intro_text,
          'query' : q,
          'form' : form,
          'filter_types' : filter_types,
          'results' : page,
          'facets' : facets,
          'totals' : totals,
          'number_of_results' : results_count,
          'side_bar_help' : side_bar_help,
          'sort_by': sort_by[0].replace('-', ''),
          'sort': sort,
      }, 
      context_instance=RequestContext(request)
    )