Permalink
Browse files

Search basic implementation

  • Loading branch information...
1 parent dfc67f4 commit 882a35e6ce82a9408926e218c9566c6c0adf40aa @alfredo alfredo committed Mar 26, 2012
View
@@ -22,3 +22,4 @@ locale/*
media/img/uploads/*
notes.rst
media/ignite/img/uploads*
+whoosh_index/
@@ -0,0 +1,11 @@
+from challenges.models import SubmissionParent
+from haystack import indexes
+
+class SubmissionParentIndex(indexes.SearchIndex, indexes.Indexable):
+ text = indexes.CharField(document=True, use_template=True)
+ user = indexes.CharField(model_attr='submission__created_by__name')
+ category = indexes.CharField(model_attr='submission__category__name',
+ faceted=True)
+
+ def get_model(self):
+ return SubmissionParent
View
No changes.
View
@@ -0,0 +1,5 @@
+from haystack.forms import FacetedSearchForm
+
+class CustomFacetedSearchForm(FacetedSearchForm):
+ """Override the results settings"""
+ pass
View
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
View
@@ -0,0 +1,16 @@
+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+
+class SimpleTest(TestCase):
+ def test_basic_addition(self):
+ """
+ Tests that 1 + 1 always equals 2.
+ """
+ self.assertEqual(1 + 1, 2)
View
@@ -0,0 +1,7 @@
+from django.conf.urls.defaults import patterns, url
+from search.views import CustomSearchView
+
+urlpatterns = patterns(
+ '',
+ url(r'^$', CustomSearchView(), name='search'),
+ )
View
@@ -0,0 +1,51 @@
+import jingo
+
+from django.http import Http404
+from django.views.decorators.http import require_GET
+from haystack.query import SearchQuerySet
+from haystack.views import FacetedSearchView
+from search.forms import CustomFacetedSearchForm
+
+# @require_GET
+# def search(request):
+# """Performs the search on the ``Submissions``"""
+# form = SearchForm(request.GET)
+# if not form.is_valid():
+# raise Http404
+# query = form.cleaned_data['q']
+# sqs = SearchQuerySet().auto_query(query)
+# context = {
+# 'results': sqs,
+# 'form': form,
+# }
+# return jingo.render(request, 'search/search.html', context)
+
+
+class CustomSearchView(FacetedSearchView):
+ """Uses jingo to render the pages"""
+
+ def __init__(self, *args, **kwargs):
+ # Needed to switch out the default form class.
+ if kwargs.get('form_class') is None:
+ kwargs['form_class'] = CustomFacetedSearchForm
+ super(FacetedSearchView, self).__init__(*args, **kwargs)
+
+ def create_response(self):
+ """Generates the actual HttpResponse to send back to the user."""
+ (paginator, page) = self.build_page()
+ context = {
+ 'query': self.query,
+ 'form': self.form,
+ 'page': page,
+ 'paginator': paginator,
+ 'suggestion': None,
+ }
+ if self.results and hasattr(self.results, 'query') and self.results.query.backend.include_spelling:
+ context['suggestion'] = self.form.get_suggestion()
+ context.update(self.extra_context())
+ return jingo.render(self.request, self.template, context)
+
+ def extra_context(self):
+ extra = super(CustomSearchView, self).extra_context()
+ extra['days_remaining'] = self.request.phase['days_remaining']
+ return extra
View
@@ -6,3 +6,5 @@
-e git://github.com/brosner/django-voting.git#egg=django-voting
markdown==2.0.3
django-extensions==0.8
+-e git+https://github.com/toastdriven/django-haystack.git@ec1f917588b3be1ce89f077a6c3c72be1551f84d#egg=django_haystack-dev
+whoosh==2.3.2
View
@@ -236,6 +236,7 @@ def JINJA_CONFIG():
# Feature flipping
'waffle',
+ 'haystack', # search
# Ignite specific
'innovate',
@@ -253,6 +254,7 @@ def JINJA_CONFIG():
'badges',
'awards',
'blogs',
+ 'search',
)
# Tells the extract script what files to look for L10n in and what function
@@ -376,3 +378,13 @@ def JINJA_CONFIG():
'/admin/',
MEDIA_URL,
]
+
+HAYSTACK_CONNECTIONS = {
+ 'default': {
+ 'ENGINE': 'haystack.backends.whoosh_backend.WhooshEngine',
+ 'PATH': path('whoosh_index'),
+ },
+}
+# High number since we don't want pagination
+HAYSTACK_SEARCH_RESULTS_PER_PAGE = 6
+HAYSTACK_DEFAULT_OPERATOR = 'AND'
@@ -1,23 +1,23 @@
-{% macro footer_pagination(page) -%}
+{% macro footer_pagination(page, query=None) -%}
{% if page.paginator.num_pages > 1 %}
<footer class="pagination">
<nav>
{% if page.has_previous() %}
- <a href="?page={{ page.previous_page_number() }}" class="btn">prev</a>
+ <a href="?page={{ page.previous_page_number() }}{% if query %}&q={{ query }}{% endif %}" class="btn">prev</a>
{% endif %}
<ol>
{% for i in range(page.paginator.num_pages) %}
<li>
{% if i + 1 == page.number %}
<span>{{ i + 1 }}</span>
{% else %}
- <a href="?page={{ i + 1 }}">{{ i + 1 }}</a>
+ <a href="?page={{ i + 1 }}{% if query %}&q={{ query }}{% endif %}">{{ i + 1 }}</a>
{% endif %}
</li>
{% endfor %}
</ol>
{% if page.has_next() %}
- <a href="?page={{ page.next_page_number() }}" class="btn">next</a>
+ <a href="?page={{ page.next_page_number() }}{% if query %}&q={{ query }}{% endif %}" class="btn">next</a>
{% endif %}
</nav>
</footer>
@@ -10,7 +10,7 @@
{% block header %}
<header class="busta">
-<h1 class="shout">Ideas</h1>
+<h1 class="shout">{% block title_listing %}Ideas{% endblock %}</h1>
<div class="meta">
<p class="intro">You have <em>{{ days_remaining }}</em> days to submit ideas</p>
<a class="cta" href="{{ url('create_entry') }}">{{ _('Create submission') }}</a>
@@ -0,0 +1,5 @@
+{{ object.submission.title|safe }}
+{{ object.submission.brief_description|safe }}
+{{ object.submission.description|safe }}
+{{ object.submission.category.name|safe }}
+{{ object.submission.created_by.name|safe }}
@@ -0,0 +1,48 @@
+{% extends "challenges/all.html" %}
+
+{% block title_listing %}Search Ideas{% endblock title_listing %}
+
+{% block content %}
+<div class="content_columns columns with_nav">
+
+<nav class="tags col box nav">
+ <form method="get" id="form-search-id" action="{{ url('search:search') }}">
+ <ul>
+ {{ form.as_ul() }}
+ <li>
+ <input class="cta" type="submit" value="Search">
+ </li>
+ </ul>
+ </form>
+</nav>
+
+ <div class="highlight col box main">
+ <ol class="submissions columns">
+ {% for result in page.object_list %}
+ <li class="box col">
+ <article>
+ <a class="title" href="{{ result.object.get_absolute_url() }}">
+ <h3 class="title wimper">
+ <div class="frame box">
+ <img src="http://src.sencha.io/200/{{ app_url }}{{ result.object.submission.get_image_src() }}" alt="">
+ </div>
+ {{ result.object.submission.title }}
+ </h3>
+ </a>
+ <p>{{ result.object.submission.brief_description }}</p>
+ <address>{{ created_by(result.object.submission, footer=False) }}</address>
+ </article>
+ </li>
+ {% endfor %}
+ </ol>
+
+ {% if query %}
+ {{ footer_pagination(page, query) }}
+ {% endif %}
+
+ </div>
+</div>
+</div>
+</div>
+</section>
+{% endblock %}
View
@@ -60,6 +60,7 @@
(r'^award/', include('awards.urls', namespace='awards'), _ignite_kwargs),
(r'^booking/', include('timeslot.urls', namespace='timeslot'),),
(r'^webcast/', include('webcast.urls', namespace='webcast'),),
+ (r'^search/', include('search.urls', namespace='search'),),
)

0 comments on commit 882a35e

Please sign in to comment.