def search_haystack(request, species_wid, query): #search if species_wid is None: species_wid = Species.objects.all()[0].wid results = SearchQuerySet().filter(species_wid=species_wid).filter(content=query) #calculate facets facets = results.facet('model_type') tmp = facets.facet_counts()['fields']['model_type'] modelNameFacet = [] objectTypes = getObjectTypes() models = [] for tmp2 in tmp: modelName = objectTypes[objectTypes.index(tmp2[0])] modelNameFacet.append({ 'name':modelName, 'verbose_name': getModel(modelName)._meta.verbose_name, 'count':tmp2[1], }) models.append(getModel(modelName)) modelNameFacet.sort(lambda x, y:cmp(x['verbose_name'], y['verbose_name'])) #narrow search by facets model_type = request.GET.get('model_type', '') if model_type: results = results.models(getModel(model_type)) #order results results = results.order_by('wid') #convert results to query set queryset = EmptyQuerySet() for object in results: tmp = object.model.objects.none() tmp._result_cache.append(object.object) queryset = chain(queryset, tmp) #form response return render_queryset_to_response( species_wid = species_wid, request = request, models = models, queryset = queryset, templateFile = 'public/search.html', data = { 'query': query, 'engine': 'haystack', 'model_type': model_type, 'modelNameFacet': modelNameFacet, })
def search_haystack(request, species_wid, query): #search if species_wid is None: species_wid = Species.objects.all()[0].wid results = SearchQuerySet().filter(species_wid=species_wid).filter( content=query) #calculate facets facets = results.facet('model_type') tmp = facets.facet_counts()['fields']['model_type'] modelNameFacet = [] objectTypes = getObjectTypes() models = [] for tmp2 in tmp: modelName = objectTypes[objectTypes.index(tmp2[0])] modelNameFacet.append({ 'name': modelName, 'verbose_name': getModel(modelName)._meta.verbose_name, 'count': tmp2[1], }) models.append(getModel(modelName)) modelNameFacet.sort(lambda x, y: cmp(x['verbose_name'], y['verbose_name'])) #narrow search by facets model_type = request.GET.get('model_type', '') if model_type: results = results.models(getModel(model_type)) #order results results = results.order_by('wid') #convert results to query set queryset = EmptyQuerySet() for object in results: tmp = object.model.objects.none() tmp._result_cache.append(object.object) queryset = chain(queryset, tmp) #form response return render_queryset_to_response(species_wid=species_wid, request=request, models=models, queryset=queryset, templateFile='public/search.html', data={ 'query': query, 'engine': 'haystack', 'model_type': model_type, 'modelNameFacet': modelNameFacet, })
def exportData(request, species_wid=None): getDict = request.GET.copy() if getDict.get('format', ''): getDict.__setitem__('species', getDict.get('species', species_wid)) form = ExportDataForm(getDict or None) if not form.is_valid(): return render_queryset_to_response( species_wid=species_wid, request=request, templateFile='public/exportDataForm.html', data={'form': form}) else: species = Species.objects.get(wid=form.cleaned_data['species']) queryset = EmptyQuerySet() models = [] if form.cleaned_data['all_model_types'] == 'True': model_types = getObjectTypes() else: model_types = form.cleaned_data['model_type'] for model_type in model_types: model = getModel(model_type) if issubclass(model, SpeciesComponent): queryset = chain( queryset, model.objects.filter( species__id=species.id).select_related(depth=2).all()) else: queryset = chain( queryset, model.objects.select_related(depth=2).filter( id=species.id)) models.append(getModel(model_type)) return render_queryset_to_response( species_wid=species_wid, request=request, queryset=queryset, templateFile='public/exportDataResult.html', models=models)
def exportData(request, species_wid=None): getDict = request.GET.copy() if getDict.get('format', ''): getDict.__setitem__('species', getDict.get('species', species_wid)) form = ExportDataForm(getDict or None) if not form.is_valid(): return render_queryset_to_response( species_wid=species_wid, request = request, templateFile = 'public/exportDataForm.html', data = { 'form': form } ) else: species = Species.objects.get(wid = form.cleaned_data['species']) queryset = EmptyQuerySet() models = [] if form.cleaned_data['all_model_types'] == 'True': model_types = getObjectTypes() else: model_types = form.cleaned_data['model_type'] for model_type in model_types: model = getModel(model_type) if issubclass(model, SpeciesComponent): queryset = chain(queryset, model.objects.filter(species__id=species.id).select_related(depth=2).all()) else: queryset = chain(queryset, model.objects.select_related(depth=2).filter(id=species.id)) models.append(getModel(model_type)) return render_queryset_to_response( species_wid = species_wid, request = request, queryset = queryset, templateFile = 'public/exportDataResult.html', models = models)
def exportDataTemplate(request): queryset = EmptyQuerySet() models = [] for model_type in getObjectTypes(): models.append(getModel(model_type)) #write work book wb = writeExcel( Species(wid = '__NEW__', name = 'New species'), queryset, models, request.user.is_anonymous()) #save to string result = StringIO() wb.save(filename = result) #generate HttpResponse response = HttpResponse( result.getvalue(), mimetype = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", content_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") response['Content-Disposition'] = "attachment; filename=data.xlsx" return response