Skip to content
This repository has been archived by the owner on Apr 10, 2019. It is now read-only.

Commit

Permalink
Search basic implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredo committed Mar 26, 2012
1 parent dfc67f4 commit 882a35e
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -22,3 +22,4 @@ locale/*
media/img/uploads/* media/img/uploads/*
notes.rst notes.rst
media/ignite/img/uploads* media/ignite/img/uploads*
whoosh_index/
11 changes: 11 additions & 0 deletions apps/challenges/search_indexes.py
@@ -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
Empty file added apps/search/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions apps/search/forms.py
@@ -0,0 +1,5 @@
from haystack.forms import FacetedSearchForm

class CustomFacetedSearchForm(FacetedSearchForm):
"""Override the results settings"""
pass
3 changes: 3 additions & 0 deletions apps/search/models.py
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
16 changes: 16 additions & 0 deletions apps/search/tests.py
@@ -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)
7 changes: 7 additions & 0 deletions apps/search/urls.py
@@ -0,0 +1,7 @@
from django.conf.urls.defaults import patterns, url
from search.views import CustomSearchView

urlpatterns = patterns(
'',
url(r'^$', CustomSearchView(), name='search'),
)
51 changes: 51 additions & 0 deletions apps/search/views.py
@@ -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
2 changes: 2 additions & 0 deletions requirements/prod.txt
Expand Up @@ -6,3 +6,5 @@
-e git://github.com/brosner/django-voting.git#egg=django-voting -e git://github.com/brosner/django-voting.git#egg=django-voting
markdown==2.0.3 markdown==2.0.3
django-extensions==0.8 django-extensions==0.8
-e git+https://github.com/toastdriven/django-haystack.git@ec1f917588b3be1ce89f077a6c3c72be1551f84d#egg=django_haystack-dev
whoosh==2.3.2
12 changes: 12 additions & 0 deletions settings.py
Expand Up @@ -236,6 +236,7 @@ def JINJA_CONFIG():


# Feature flipping # Feature flipping
'waffle', 'waffle',
'haystack', # search


# Ignite specific # Ignite specific
'innovate', 'innovate',
Expand All @@ -253,6 +254,7 @@ def JINJA_CONFIG():
'badges', 'badges',
'awards', 'awards',
'blogs', 'blogs',
'search',
) )


# Tells the extract script what files to look for L10n in and what function # Tells the extract script what files to look for L10n in and what function
Expand Down Expand Up @@ -376,3 +378,13 @@ def JINJA_CONFIG():
'/admin/', '/admin/',
MEDIA_URL, 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'
8 changes: 4 additions & 4 deletions templates/layout/pagination.html
@@ -1,23 +1,23 @@
{% macro footer_pagination(page) -%} {% macro footer_pagination(page, query=None) -%}
{% if page.paginator.num_pages > 1 %} {% if page.paginator.num_pages > 1 %}
<footer class="pagination"> <footer class="pagination">
<nav> <nav>
{% if page.has_previous() %} {% 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 %} {% endif %}
<ol> <ol>
{% for i in range(page.paginator.num_pages) %} {% for i in range(page.paginator.num_pages) %}
<li> <li>
{% if i + 1 == page.number %} {% if i + 1 == page.number %}
<span>{{ i + 1 }}</span> <span>{{ i + 1 }}</span>
{% else %} {% else %}
<a href="?page={{ i + 1 }}">{{ i + 1 }}</a> <a href="?page={{ i + 1 }}{% if query %}&q={{ query }}{% endif %}">{{ i + 1 }}</a>
{% endif %} {% endif %}
</li> </li>
{% endfor %} {% endfor %}
</ol> </ol>
{% if page.has_next() %} {% 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 %} {% endif %}
</nav> </nav>
</footer> </footer>
Expand Down
2 changes: 1 addition & 1 deletion templates_ignite/challenges/all.html
Expand Up @@ -10,7 +10,7 @@


{% block header %} {% block header %}
<header class="busta"> <header class="busta">
<h1 class="shout">Ideas</h1> <h1 class="shout">{% block title_listing %}Ideas{% endblock %}</h1>
<div class="meta"> <div class="meta">
<p class="intro">You have <em>{{ days_remaining }}</em> days to submit ideas</p> <p class="intro">You have <em>{{ days_remaining }}</em> days to submit ideas</p>
<a class="cta" href="{{ url('create_entry') }}">{{ _('Create submission') }}</a> <a class="cta" href="{{ url('create_entry') }}">{{ _('Create submission') }}</a>
Expand Down
@@ -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 }}
48 changes: 48 additions & 0 deletions templates_ignite/search/search.html
@@ -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 %}
1 change: 1 addition & 0 deletions urls_ignite.py
Expand Up @@ -60,6 +60,7 @@
(r'^award/', include('awards.urls', namespace='awards'), _ignite_kwargs), (r'^award/', include('awards.urls', namespace='awards'), _ignite_kwargs),
(r'^booking/', include('timeslot.urls', namespace='timeslot'),), (r'^booking/', include('timeslot.urls', namespace='timeslot'),),
(r'^webcast/', include('webcast.urls', namespace='webcast'),), (r'^webcast/', include('webcast.urls', namespace='webcast'),),
(r'^search/', include('search.urls', namespace='search'),),
) )




Expand Down

0 comments on commit 882a35e

Please sign in to comment.