# HG changeset patch # User Harris Baptiste # Date 1470990491 -7200 # Node ID 233bda6f28655216c143bf960793402da264bddb # Parent eeb3541b343b8785674449e866773f36f6143bd2 iconolab search diff -r eeb3541b343b -r 233bda6f2865 src/iconolab/models.py --- a/src/iconolab/models.py Fri Aug 05 15:32:49 2016 +0200 +++ b/src/iconolab/models.py Fri Aug 12 10:28:11 2016 +0200 @@ -94,6 +94,18 @@ def __str__(self): return self.name + + @property + def collection(self): + return self.item.collection.name + + @property + def title(self): + return self.item.metadatas.title + + @property + def description(self): + return self.item.metadatas.description class AnnotationManager(models.Manager): @@ -191,6 +203,10 @@ def awaiting_revisions_count(self): return self.revisions.filter(state=AnnotationRevision.AWAITING).distinct().count() + @property + def tags(self): + return [tag.label for tag in self.current_revision.tags.all()] + # Call to create a new revision, possibly from a merge @transaction.atomic diff -r eeb3541b343b -r 233bda6f2865 src/iconolab/search_indexes.py --- a/src/iconolab/search_indexes.py Fri Aug 05 15:32:49 2016 +0200 +++ b/src/iconolab/search_indexes.py Fri Aug 12 10:28:11 2016 +0200 @@ -10,15 +10,11 @@ title = indexes.CharField(model_attr='current_revision__title') description = indexes.CharField(model_attr='current_revision__description') - tags = indexes.MultiValueField() + tags = indexes.MultiValueField(model_attr='tags') ## tags def get_model(self): return Annotation - def prepare_tags(self, annotation): - return ["toto", "titi", "sesl"] - - def index_queryset(self, using=None): - return self.get_model().objects.filter(created__lte=datetime.datetime.now()).distinct('current_revision_id'); \ No newline at end of file + return self.get_model().objects.filter(created__lte=datetime.datetime.now()) \ No newline at end of file diff -r eeb3541b343b -r 233bda6f2865 src/iconolab/search_indexes/__init__.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/iconolab/search_indexes/__init__.py Fri Aug 12 10:28:11 2016 +0200 @@ -0,0 +1,2 @@ +from .indexes import AnnotationIndex, ImageIndex +__all__ = ['AnnotationIndex', 'ImageIndex'] \ No newline at end of file diff -r eeb3541b343b -r 233bda6f2865 src/iconolab/search_indexes/forms.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/iconolab/search_indexes/forms.py Fri Aug 12 10:28:11 2016 +0200 @@ -0,0 +1,47 @@ +from django import forms +from haystack.forms import SearchForm +from iconolab.models import Image, Annotation + + + + +#def get_available_choices(): + +def get_selected_model(type): + + available_models = { + 'image': Image, + 'annotation': Annotation + } + + return available_models[type] + + +class IconolabSearchForm(SearchForm): + + realm = forms.ChoiceField(required=False, choices=(("image","Image"), ("annotation","Annotation")) ) + + def __init__(self, *args, **kwargs): + + super(IconolabSearchForm, self).__init__(*args, **kwargs) + + def get_realm_queryset(self, qs, realm): + + if realm == 'image': + qs = qs.models(Image).load_all_queryset(Image, Image.objects.select_related('item', 'item__metadatas')) + qs = qs.filter(collection='stdie') + if realm == 'annotation': + qs = qs.models(Annotation).load_all_queryset(Annotation, Annotation.objects.select_related('image', 'stats', 'current_revision', 'author')) + return qs + + def search(self): + + selected_realm = self.cleaned_data.get("realm") + selected_model = get_selected_model(selected_realm) + #load all if q empty + qs = super(IconolabSearchForm, self).search() + if qs.count() == 0: + return qs + else: + qs = self.get_realm_queryset(qs, selected_realm).load_all() + return qs \ No newline at end of file diff -r eeb3541b343b -r 233bda6f2865 src/iconolab/search_indexes/indexes.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/iconolab/search_indexes/indexes.py Fri Aug 12 10:28:11 2016 +0200 @@ -0,0 +1,37 @@ +import datetime +from haystack import indexes +from iconolab.models import Annotation, Image + +class ImageIndex(indexes.SearchIndex, indexes.Indexable): + text = indexes.CharField(document=True, use_template=True) + + title = indexes.CharField(model_attr='item__metadatas__title') + description = indexes.CharField(model_attr='item__metadatas__description') + collection = indexes.CharField(model_attr='item__collection__name') + tags = indexes.MultiValueField() + + def get_model(self): + return Image + + def prepare_tags(self, object): + return ["radical", "you better", "yes"] + + def index_queryset(self, using=None): + return self.get_model().objects.filter(created__lte=datetime.datetime.now()) + + +class AnnotationIndex(indexes.SearchIndex, indexes.Indexable): + + ##indexed field + text = indexes.CharField(document=True, use_template=True) + + title = indexes.CharField(model_attr='current_revision__title') + description = indexes.CharField(model_attr='current_revision__description') + tags = indexes.MultiValueField(model_attr='tags') + + ## tags + def get_model(self): + return Annotation + + def index_queryset(self, using=None): + return self.get_model().objects.filter(created__lte=datetime.datetime.now()) \ No newline at end of file diff -r eeb3541b343b -r 233bda6f2865 src/iconolab/search_indexes/query.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/iconolab/search_indexes/query.py Fri Aug 12 10:28:11 2016 +0200 @@ -0,0 +1,18 @@ +from haystack.query import RelatedSearchQuerySet +from iconolab.models import Annotation, Image +from pprint import pprint + +class IconolabRelatedQuerySet(RelatedSearchQuerySet): + def __init__(self, using=None, query=None): + super(IconolabRelatedQuerySet, self).__init__(using=using, query=query) + + #def in_bulk(self, ids): + # results = {} + # int_ids = [ int(id) for id in ids] + # # Ne garder que les images + # annotations = Image.objects.filter(pk__in = int_ids) + # + # for annotation in annotations: + # results[annotation.pk] = annotation + # + # return results diff -r eeb3541b343b -r 233bda6f2865 src/iconolab/search_indexes/views.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/iconolab/search_indexes/views.py Fri Aug 12 10:28:11 2016 +0200 @@ -0,0 +1,41 @@ +from haystack.generic_views import SearchView +from iconolab.search_indexes.forms import IconolabSearchForm +from iconolab.search_indexes.query import IconolabRelatedQuerySet +from django.shortcuts import HttpResponse + +from pprint import pprint + +#override Search and Related QuerySet here +class IconolabSearchView(SearchView): + form_class = IconolabSearchForm + queryset = IconolabRelatedQuerySet() + template_name = "search/default_search.html" + paginate_by = 10 + load_all = True + + templates_map = { + "image": "search/image_search.html", + "annotation": "search/annotation_search.html" + } + + def get(self, request, *args, **kwargs): + self.current_realm = request.GET['realm'] + return super(IconolabSearchView, self).get(request,*args, **kwargs) + + def get_queryset(self): + qs = super(IconolabSearchView, self).get_queryset() + return qs + + def get_template_names(self): + template = IconolabSearchView.templates_map[self.current_realm] + if template is None: + template = IconolabSearchView.template_name + + return [template] + + def get_context_data(self, *args, **kwargs): + print("inside get_context_data", kwargs.get("q")) + print(kwargs.items()) + context = super(IconolabSearchView, self).get_context_data(*args, **kwargs) + context['collection_name'] = self.kwargs.get('collection_name', '') + return context \ No newline at end of file diff -r eeb3541b343b -r 233bda6f2865 src/iconolab/templates/iconolab/collection_home.html --- a/src/iconolab/templates/iconolab/collection_home.html Fri Aug 05 15:32:49 2016 +0200 +++ b/src/iconolab/templates/iconolab/collection_home.html Fri Aug 12 10:28:11 2016 +0200 @@ -11,7 +11,7 @@ - - + {% include "partials/header_search_form.html"%} +