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 __init__(self, *args, **kwargs): super(ExportDataForm, self).__init__(*args, **kwargs) choices = [] for species in Species.objects.values('wid', 'name').all(): choices.append((species['wid'], species['name'], )) self.fields['species'].choices = choices model_types = getObjectTypes() models = getModels() choices = [] for model_type in model_types: choices.append((model_type, models[model_type]._meta.verbose_name_plural, )) self.fields['model_type'].choices = choices self.fields['model_type'].initial = model_types
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 __init__(self, *args, **kwargs): super(ExportDataForm, self).__init__(*args, **kwargs) choices = [] for species in Species.objects.values('wid', 'name').all(): choices.append(( species['wid'], species['name'], )) self.fields['species'].choices = choices model_types = getObjectTypes() models = getModels() choices = [] for model_type in model_types: choices.append(( model_type, models[model_type]._meta.verbose_name_plural, )) self.fields['model_type'].choices = choices self.fields['model_type'].initial = model_types
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
def list(request, species_wid, model_type): try: getObjectTypes().index(model_type) except ValueError: raise Http404 species = Species.objects.get(wid=species_wid) model = getModel(model_type) objects = model.objects.all().filter(species__id=species.id) facet_fields = [] for field_full_name in model._meta.facet_fields: #facet field_names = str(field_full_name).split('__') tmp_model = model field_verbose_name = [] for field_name in field_names: field = tmp_model._meta.get_field_by_name(field_name)[0] field_verbose_name.append(field.verbose_name) if isinstance(field, (ForeignKey, ManyToManyField)): tmp_model = field.rel.to field_verbose_name = ' › '.join(field_verbose_name) if isinstance(field, (ForeignKey, ManyToManyField)) and not issubclass(field.rel.to, Entry): continue if isinstance(field, (ForeignKey, ManyToManyField)): tmp = model.objects.filter(species__id=species.id).order_by(field_full_name + '__name').values(field_full_name).annotate(count=Count(field_full_name)) else: tmp = model.objects.filter(species__id=species.id).order_by(field_full_name).values(field_full_name).annotate(count=Count(field_full_name)) facets = [] for facet in tmp: value = facet[field_full_name] if value is None or unicode(value) == '': continue if isinstance(field, (ForeignKey, ManyToManyField)): tmp2 = tmp_model.objects.values('wid', 'name').get(id=value) id = tmp2['wid'] name = capfirst(tmp2['name']) elif (field.choices is not None) and (len(field.choices) > 0) and (not isinstance(field, (BooleanField, NullBooleanField))): id = value choices = [x[0] for x in field.choices] if id in choices: name = field.choices[choices.index(id)][1] else: name = capfirst(value) else: id = value name = capfirst(value) if value is not None and unicode(value) != '': facets.append({ 'id': unicode(id), 'name': unicode(name), 'count': facet['count']}) if len(facets) > 1: facet_fields.append({ 'name': field_full_name, 'verbose_name': field_verbose_name, 'facets': facets, }) #filter val = request.GET.get(field_full_name) if val: if isinstance(field, (ForeignKey, ManyToManyField)): kwargs = {field_full_name + '__wid': val} elif isinstance(field, (BooleanField, NullBooleanField)): kwargs = {field_full_name: val == 'True'} elif isinstance(field, (AutoField, BigIntegerField, DecimalField, FloatField, IntegerField, PositiveIntegerField, PositiveSmallIntegerField, SmallIntegerField)): kwargs = {field_full_name: float(val)} else: kwargs = {field_full_name: val} objects = objects.filter(**kwargs) return render_queryset_to_response( species_wid = species_wid, request = request, models = [model], queryset = objects, templateFile = 'public/list.html', data = { 'model_type': model_type, 'model_verbose_name': model._meta.verbose_name, 'model_verbose_name_plural': model._meta.verbose_name_plural, 'facet_fields': facet_fields, })
def list(request, species_wid, model_type): try: getObjectTypes().index(model_type) except ValueError: raise Http404 species = Species.objects.get(wid=species_wid) model = getModel(model_type) objects = model.objects.all().filter(species__id=species.id) facet_fields = [] for field_full_name in model._meta.facet_fields: #facet field_names = str(field_full_name).split('__') tmp_model = model field_verbose_name = [] for field_name in field_names: field = tmp_model._meta.get_field_by_name(field_name)[0] field_verbose_name.append(field.verbose_name) if isinstance(field, (ForeignKey, ManyToManyField)): tmp_model = field.rel.to field_verbose_name = ' › '.join(field_verbose_name) if isinstance(field, (ForeignKey, ManyToManyField)) and not issubclass( field.rel.to, Entry): continue if isinstance(field, (ForeignKey, ManyToManyField)): tmp = model.objects.filter(species__id=species.id).order_by( field_full_name + '__name').values(field_full_name).annotate( count=Count(field_full_name)) else: tmp = model.objects.filter(species__id=species.id).order_by( field_full_name).values(field_full_name).annotate( count=Count(field_full_name)) facets = [] for facet in tmp: value = facet[field_full_name] if value is None or unicode(value) == '': continue if isinstance(field, (ForeignKey, ManyToManyField)): tmp2 = tmp_model.objects.values('wid', 'name').get(id=value) id = tmp2['wid'] name = capfirst(tmp2['name']) elif (field.choices is not None) and (len(field.choices) > 0) and ( not isinstance(field, (BooleanField, NullBooleanField))): id = value choices = [x[0] for x in field.choices] if id in choices: name = field.choices[choices.index(id)][1] else: name = capfirst(value) else: id = value name = capfirst(value) if value is not None and unicode(value) != '': facets.append({ 'id': unicode(id), 'name': unicode(name), 'count': facet['count'] }) if len(facets) > 1: facet_fields.append({ 'name': field_full_name, 'verbose_name': field_verbose_name, 'facets': facets, }) #filter val = request.GET.get(field_full_name) if val: if isinstance(field, (ForeignKey, ManyToManyField)): kwargs = {field_full_name + '__wid': val} elif isinstance(field, (BooleanField, NullBooleanField)): kwargs = {field_full_name: val == 'True'} elif isinstance(field, (AutoField, BigIntegerField, DecimalField, FloatField, IntegerField, PositiveIntegerField, PositiveSmallIntegerField, SmallIntegerField)): kwargs = {field_full_name: float(val)} else: kwargs = {field_full_name: val} objects = objects.filter(**kwargs) return render_queryset_to_response(species_wid=species_wid, request=request, models=[model], queryset=objects, templateFile='public/list.html', data={ 'model_type': model_type, 'model_verbose_name': model._meta.verbose_name, 'model_verbose_name_plural': model._meta.verbose_name_plural, 'facet_fields': facet_fields, })