Skip to content

Commit fbc6d2e

Browse files
committed
made demo app searchable; narrow results according to current project and user role;
1 parent bd34696 commit fbc6d2e

File tree

8 files changed

+70
-12
lines changed

8 files changed

+70
-12
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
Haystack indexes definitions to make models from application {application_name} searchable.
3+
4+
See Haystack documentation for information to write these indexes: http://docs.haystacksearch.org/dev/searchindex_api.html
5+
"""
6+
7+
from haystack import indexes
8+
9+
from {package_name}.models import {class_name}
10+
11+
class {class_name}Index(indexes.RealTimeSearchIndex, indexes.Indexable):
12+
text = indexes.CharField(document=True, model_attr='message')
13+
14+
# The following is necessary so that djity search engine shall be able to narrow results by projects and according to users roles.
15+
project = indexes.IntegerField(model_attr='project__id')
16+
status = indexes.MultiValueField()
17+
18+
def prepare_status(self,obj):
19+
return range(obj.status+1)
20+
21+
22+
def get_model(self):
23+
return {class_name}
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{{% load highlight %}}
2+
3+
<a href="{{{{ result_url }}}}">{{{{ result.object.name }}}}</a><br>
4+
{{% highlight result.text with query %}}
5+
6+

djity/project_skeleton/settings.py_tmpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ STATUS_PERMISSIONS = {{
162162
# list of permissions to check when updating djity's context
163163
PERMISSIONS = ['view','edit','upload','manage']
164164

165+
# List of visible statuses according to role (used to filter search results)
166+
LOWER_VISIBLE_STATUS = {{
167+
ANONYMOUS:PUBLIC,
168+
AWAITING:PUBLIC,
169+
USER:PRIVATE,
170+
CONTRIBUTOR:DRAFT,
171+
MANAGER:DRAFT,
172+
}}
173+
165174
# Status display in user interface
166175
STATUS_DISPLAY = [[DRAFT,'Draft'],[PRIVATE,'Private'],[PUBLIC,'Public']]
167176

djity/search/templatetags/search_result.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,9 @@ def search_result(context, result):
77
"""
88
Will render a search result for haystack by using the appropriate template for each model type
99
"""
10-
return template.loader.render_to_string("djity/%s/%s_search_result.html" % (result.app_label,result.model_name), {'result_url':result.object.djity_url(context)}, context)
10+
if result.app_label.startswith("djity_"):
11+
template_path = "%s/%s_search_result.html" % (result.app_label,result.model_name)
12+
else:
13+
template_path = "djity/%s/%s_search_result.html" % (result.app_label,result.model_name)
14+
return template.loader.render_to_string(template_path, {'result_url':result.object.djity_url(context)}, context)
1115

djity/search/views.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
from django.conf import settings
2+
13
from haystack.views import basic_search
2-
from haystack.forms import ModelSearchForm
34
from haystack.query import SearchQuerySet
5+
46
from djity.utils.decorators import djity_view
57
from djity.utils.context import DjityContext
68

79
@djity_view(perm='view')
810
def project_search(request,context=None):
911
"""
10-
Override haystack basic_view for customization
11-
"""
12-
sqs = SearchQuerySet().filter(project=context['project'])
13-
#sqs = SearchQuerySet()
12+
Override haystack basic_view for customization.
13+
"""
14+
sqs = SearchQuerySet().narrow("project:%s"% context['project'].id).narrow("status:%s" % settings.LOWER_VISIBLE_STATUS[context['role']])
15+
#for hidden_status in settings.HIDDEN_STATUSES[context['role']]:
16+
# sqs = sqs.exclude(status=hidden_status)
17+
print sqs
1418
return basic_search(request, template='djity/search/search.html', searchqueryset=sqs, extra_context=context, context_class=DjityContext)

djity/simplepage/search_indexes.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
from django.conf import settings
2+
13
from haystack import indexes
24

35
from djity.simplepage.models import SimplePage
46

5-
class SimplePageIndex(indexes.SearchIndex, indexes.Indexable):
7+
class SimplePageIndex(indexes.RealTimeSearchIndex, indexes.Indexable):
68
text = indexes.CharField(document=True, model_attr='content')
7-
project = indexes.CharField(model_attr='project')
9+
project = indexes.IntegerField(model_attr='project__id')
10+
status = indexes.MultiValueField()
11+
12+
def prepare_status(self,obj):
13+
return range(obj.status+1)
814

915
def get_model(self):
1016
return SimplePage

djity/static/djity/js/djity.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,8 @@ function widgetify() {
164164
}
165165
);
166166

167-
168167
$('#messages').notify();
169168

170-
171169

172170
if(dj.context.perm.manage){
173171
$("#project_title").editable({simple:true,save_function:'djity.project.save_project_title'});
@@ -195,13 +193,13 @@ function toolbar() {
195193
$("#toolbar").buttonset();
196194
$("#toolbar a")
197195
.button()
198-
.addClass('dj.context.mini-button');
196+
.addClass('dj-mini-button');
199197
};
200198

201199
function paginator() {
202200
$('#paginator a')
203201
.button()
204-
.addClass('dj.context.mini-button');
202+
.addClass('dj-mini-button');
205203

206204
$('#paginator .off')
207205
.button('option','disabled','true');

djity/templates/djity/base.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@
124124
{% endif %}
125125
</div>
126126
</div>
127+
128+
<div id="search_form">
129+
<form method="get" action="{% djiurl project %}/search/">
130+
<input id="search_query" name='q' type="text"></input>
131+
<input type="submit" id="search_button" value="{% trans "Search" %}" />
132+
</form>
133+
</div>
127134

128135
<nav id="portal_parameters" class="ui-helper-hidden">
129136
</nav>

0 commit comments

Comments
 (0)