From: Marek Stępniowski Date: Fri, 22 Aug 2008 14:26:54 +0000 (+0200) Subject: Moved all searching to search view. X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/068c98b54439d470e060aa6543e09f31e51d0370?hp=c1c87f44a4f252023c1e7cfa6f03aaa0f2b596a3 Moved all searching to search view. --- diff --git a/catalogue/forms.py b/catalogue/forms.py index 7472eb420..51d7cbad9 100644 --- a/catalogue/forms.py +++ b/catalogue/forms.py @@ -8,11 +8,13 @@ from catalogue.lib.slughifi import slughifi class SearchForm(forms.Form): q = JQueryAutoCompleteField('/katalog/tags/', {'minChars': 2, 'selectFirst': True, 'cacheLength': 50}) - tags = forms.CharField(widget=forms.HiddenInput) + tags = forms.CharField(widget=forms.HiddenInput, required=False) def __init__(self, *args, **kwargs): + tags = kwargs.pop('tags', []) super(SearchForm, self).__init__(*args, **kwargs) self.fields['q'].widget.attrs['title'] = u'tytuł utworu, motyw lub kategoria' + self.fields['tags'].initial = '/'.join(tag.slug for tag in Tag.get_tag_list(tags)) class UserSetsForm(forms.Form): diff --git a/catalogue/urls.py b/catalogue/urls.py index e56d82241..5827285d3 100644 --- a/catalogue/urls.py +++ b/catalogue/urls.py @@ -9,6 +9,7 @@ urlpatterns = patterns('catalogue.views', url(r'^zestawy/nowy/$', 'new_set'), url(r'^lektura/(?P[a-zA-Z0-9-]+)/$', 'book_detail'), url(r'^tags/$', 'tags_starting_with', name='hint'), + url(r'^szukaj/$', 'search', name='search'), url(r'^(?P[a-zA-Z-/]+)/$', 'tagged_book_list', name='tagged_book_list'), ) diff --git a/catalogue/views.py b/catalogue/views.py index d5b4f69d1..0166133a7 100644 --- a/catalogue/views.py +++ b/catalogue/views.py @@ -26,20 +26,26 @@ class LazyEncoder(simplejson.JSONEncoder): return obj -def catalogue_redirect(request, tags=''): - if len(request.GET['q']) > 0: +def search(request): + query = request.GET.get('q', '') + tags = request.GET.get('tags', '') + if tags == '': + tags = [] + + try: + tag_list = models.Tag.get_tag_list(tags) + tag = models.Tag.objects.get(name=query) + except models.Tag.DoesNotExist: + print "%r, %r" % (query, tags) try: - tag = models.Tag.objects.get(name=request.GET['q']) - if len(tags): - tags += '/' - tags = tags + tag.slug - except models.Tag.DoesNotExist: - book = get_object_or_404(models.Book, title=request.GET['q']) - return HttpResponseRedirect(book.get_absolute_url()) - if len(tags) > 0: - return HttpResponseRedirect(reverse('catalogue.views.tagged_book_list', kwargs=dict(tags=tags))) + book = models.Book.objects.get(title=query) + except models.Book.DoesNotExist: + return HttpResponseRedirect(reverse('catalogue.views.main_page')) else: - return HttpResponseRedirect(reverse('catalogue.views.main_page')) + tag_list.append(tag) + return HttpResponseRedirect(reverse('catalogue.views.tagged_book_list', + kwargs={'tags': '/'.join(tag.slug for tag in tag_list)} + )) def tags_starting_with(request): @@ -63,10 +69,7 @@ def tags_starting_with(request): return HttpResponse('') -def main_page(request): - if 'q' in request.GET: - return catalogue_redirect(request) - +def main_page(request): if request.user.is_authenticated(): extra_where = '(NOT catalogue_tag.category = "set" OR catalogue_tag.user_id = %d)' % request.user.id else: @@ -80,9 +83,6 @@ def main_page(request): def book_list(request): - if 'q' in request.GET: - return catalogue_redirect(request) - books = models.Book.objects.all() form = forms.SearchForm() @@ -95,9 +95,6 @@ def book_list(request): def tagged_book_list(request, tags=''): - if 'q' in request.GET: - return catalogue_redirect(request, tags) - try: tags = models.Tag.get_tag_list(tags) except models.Tag.DoesNotExist: @@ -116,7 +113,7 @@ def tagged_book_list(request, tags=''): queryset_or_model=models.Book, tags=tags, template_name='catalogue/tagged_book_list.html', - extra_context = {'categories': categories, 'form': forms.SearchForm() }, + extra_context = {'categories': categories }, ) @@ -124,7 +121,6 @@ def book_detail(request, slug): book = get_object_or_404(models.Book, slug=slug) tags = list(book.tags.filter(~Q(category='set'))) categories = split_tags(tags) - search_form = forms.SearchForm() return render_to_response('catalogue/book_detail.html', locals(), context_instance=RequestContext(request)) diff --git a/templates/catalogue/book_list.html b/templates/catalogue/book_list.html index f6c3b75fa..f4f575c7f 100644 --- a/templates/catalogue/book_list.html +++ b/templates/catalogue/book_list.html @@ -16,7 +16,7 @@ {% block body %}

Alfabetyczny spis utworów

-
+

{{ form.q }} lub wróć do strony głównej

diff --git a/templates/catalogue/main_page.html b/templates/catalogue/main_page.html index 8fe16d1cb..507fa3a95 100644 --- a/templates/catalogue/main_page.html +++ b/templates/catalogue/main_page.html @@ -17,8 +17,8 @@ {% chunk "site-description" %}
-
-

{{ form.q }} lub zobacz spis utworów w naszym zbiorze

+ +

{{ form.q }} {{ form.tags }} lub zobacz spis utworów w naszym zbiorze

diff --git a/templates/catalogue/tagged_book_list.html b/templates/catalogue/tagged_book_list.html index 695a49240..68253af58 100644 --- a/templates/catalogue/tagged_book_list.html +++ b/templates/catalogue/tagged_book_list.html @@ -54,14 +54,15 @@ {% block body %}

{% title_from_tags tags %}

-
-
    - {% for tag in tags %} -
  1. {{ tag }} | x
  2. - {% endfor %} -
  3. {{ form.q }}
  4. -
-
+ {% breadcrumbs tags %} + {#
#} + {#
    #} + {# {% for tag in tags %} #} + {#
  1. {{ tag }} | x
  2. #} + {# {% endfor %} #} + {#
  3. {{ form.q }}
  4. #} + {#
#} + {#
#} {% autopaginate object_list 10 %}