diff --git a/apps/dynamic_search/__init__.py b/apps/dynamic_search/__init__.py index a4333c28f0..8b13789179 100644 --- a/apps/dynamic_search/__init__.py +++ b/apps/dynamic_search/__init__.py @@ -1,6 +1 @@ -from django.utils.translation import ugettext_lazy as _ -from common.api import register_menu -register_menu([ - {'text':_(u'search'), 'view':'search', 'famfam':'zoom', 'position':2}, -]) diff --git a/apps/dynamic_search/templates/search_results.html b/apps/dynamic_search/templates/search_results.html index 231360a778..8f3d4f3d5b 100644 --- a/apps/dynamic_search/templates/search_results.html +++ b/apps/dynamic_search/templates/search_results.html @@ -1,22 +1,19 @@ {% extends "base.html" %} {% load i18n %} {% block title %} :: {% trans "Search results" %}{% endblock %} + {% block content %} - - {% with "get" as submit_method %} - {% with form_title as title %} - {% include "generic_form_subtemplate.html" %} - {% endwith %} - {% endwith %} - - {% if query_string %} - {% include "generic_list_subtemplate.html" %} + {% if form %} + {% include "search_results_subtemplate.html" %} {% endif %} + {% if query_string %} + {% include "generic_list_subtemplate.html" %} + {% endif %} {% endblock %} + {% block footer %} {% if query_string %} {% blocktrans %}Elapsed time: {{ time_delta }} seconds{% endblocktrans %} {% endif %} {% endblock %} - diff --git a/apps/dynamic_search/templates/search_results_subtemplate.html b/apps/dynamic_search/templates/search_results_subtemplate.html new file mode 100644 index 0000000000..bb8d612210 --- /dev/null +++ b/apps/dynamic_search/templates/search_results_subtemplate.html @@ -0,0 +1,5 @@ +{% with "get" as submit_method %} +{% with form_title as title %} + {% include "generic_form_subtemplate.html" %} +{% endwith %} +{% endwith %} diff --git a/apps/dynamic_search/templatetags/__init__.py b/apps/dynamic_search/templatetags/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/apps/dynamic_search/templatetags/search_tags.py b/apps/dynamic_search/templatetags/search_tags.py new file mode 100644 index 0000000000..22555397e8 --- /dev/null +++ b/apps/dynamic_search/templatetags/search_tags.py @@ -0,0 +1,20 @@ +from django.core.urlresolvers import reverse, NoReverseMatch +from django.template import TemplateSyntaxError, Library, \ + VariableDoesNotExist, Node, Variable +from django.utils.translation import ugettext as _ + +from dynamic_search.forms import SearchForm + + +register = Library() + +@register.inclusion_tag('search_results_subtemplate.html', takes_context=True) +def search_form(context): + context.update({ + 'form':SearchForm(initial={'q':context.get('query_string', '')}), + 'request':context['request'], + 'MEDIA_URL':context['MEDIA_URL'], + 'form_action':reverse('results'), + 'form_title':_(u'Search') + }) + return context diff --git a/apps/dynamic_search/urls.py b/apps/dynamic_search/urls.py index bb7363e7c2..f49f57c594 100644 --- a/apps/dynamic_search/urls.py +++ b/apps/dynamic_search/urls.py @@ -3,6 +3,7 @@ from django.conf.urls.defaults import * urlpatterns = patterns('dynamic_search.views', url(r'^search/$', 'search', (), 'search'), + url(r'^results/$', 'results', (), 'results'), ) diff --git a/apps/dynamic_search/views.py b/apps/dynamic_search/views.py index 1dc23a44e5..799bc81160 100644 --- a/apps/dynamic_search/views.py +++ b/apps/dynamic_search/views.py @@ -10,6 +10,57 @@ from forms import SearchForm from conf.settings import SHOW_OBJECT_TYPE +def results(request, form=None): + query_string = '' + context = {} + + result_count = 0 + if ('q' in request.GET) and request.GET['q'].strip(): + query_string = request.GET['q'] + try: + model_list, flat_list, shown_result_count, total_result_count, elapsed_time = perform_search(query_string) + if shown_result_count != total_result_count: + title = _(u'results with: %s (showing only %s out of %s)') % (query_string, shown_result_count, total_result_count) + else: + title = _(u'results with: %s') % query_string + context.update({ + 'found_entries': model_list, + 'object_list':flat_list, + 'title':title, + 'time_delta':elapsed_time, + }) + + except Exception, e: + if settings.DEBUG: + raise + elif request.user.is_staff or request.user.is_superuser: + messages.error(request, _(u'Search error: %s') % e) + + context.update({ + 'query_string':query_string, + 'form':form, + 'form_title':_(u'Search'), + 'hide_header':True, + }) + + if SHOW_OBJECT_TYPE: + context.update({'extra_columns': + [{'name':_(u'type'), 'attribute':lambda x:x._meta.verbose_name[0].upper() + x._meta.verbose_name[1:]}]}) + + return render_to_response('search_results.html', context, + context_instance=RequestContext(request)) + + + +def search(request): + if ('q' in request.GET) and request.GET['q'].strip(): + query_string = request.GET['q'] + form = SearchForm(initial={'q':query_string}) + return results(request, form=form) + else: + form = SearchForm() + return results(request, form=form) +""" def search(request): query_string = '' context = {} @@ -52,3 +103,4 @@ def search(request): return render_to_response('search_results.html', context, context_instance=RequestContext(request)) +""" diff --git a/apps/main/__init__.py b/apps/main/__init__.py index d899b96145..6b9cdce3f4 100644 --- a/apps/main/__init__.py +++ b/apps/main/__init__.py @@ -7,12 +7,12 @@ from permissions import role_list from documents import document_find_all_duplicates from filesystem_serving import filesystem_serving_recreate_all_links +from main.conf.settings import SIDE_BAR_SEARCH + check_settings = {'text':_(u'settings'), 'view':'check_settings', 'famfam':'cog'} - -register_menu([ +main_menu = [ {'text':_(u'home'), 'view':'home', 'famfam':'house', 'position':0}, - {'text':_(u'tools'), 'view':'tools_menu', 'links': [ document_find_all_duplicates, filesystem_serving_recreate_all_links ],'famfam':'wrench', 'name':'tools','position':7}, @@ -22,4 +22,10 @@ register_menu([ ],'famfam':'cog', 'name':'setup','position':8}, {'text':_(u'about'), 'view':'about', 'position':9}, -]) +] + +if not SIDE_BAR_SEARCH: + main_menu.insert(1, {'text':_(u'search'), 'view':'search', 'famfam':'zoom', 'position':2}) + +register_menu(main_menu) + diff --git a/apps/main/conf/__init__.py b/apps/main/conf/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/apps/main/conf/settings.py b/apps/main/conf/settings.py new file mode 100644 index 0000000000..c5503fa94c --- /dev/null +++ b/apps/main/conf/settings.py @@ -0,0 +1,4 @@ +from django.conf import settings + +SIDE_BAR_SEARCH = getattr(settings, 'MAIN_SIDE_BAR_SEARCH', False) + diff --git a/apps/main/templates/base.html b/apps/main/templates/base.html index 3d9602863d..1dd2df1709 100644 --- a/apps/main/templates/base.html +++ b/apps/main/templates/base.html @@ -3,6 +3,8 @@ {% load project_tags %} {% load navigation %} {% load settings %} +{% load search_tags %} +{% load main_settings_tags %} {% block html_title %}{% project_name %}{% block title %}{% endblock %}{% endblock %} @@ -118,6 +120,13 @@ {% endblock %} {% block web_theme_sidebar %} + {% get_main_setting "SIDE_BAR_SEARCH" as side_bar_search %} + {% if side_bar_search %} + {% with "true" as side_bar %} + {% search_form %} + {% endwith %} + {% endif %} + {% get_object_navigation_links %} {% if object_navigation_links %}